?
Solved

var strDate = '<%= System.DateTime.Now.ToString(\'yyyy-MM-dd\'); %>';

Posted on 2009-04-10
8
Medium Priority
?
3,902 Views
Last Modified: 2013-12-17
Hi

I write asp.net, c# and javascript

In the js, I need to get the system datetime and in the format yyyy-MM-dd

However, it has runtime error.
var strDate = '<%= System.DateTime.Now.ToString(\'yyyy-MM-dd\'); %>';
 
or
 
var strDate = '<%= System.DateTime.Now.ToString(\"yyyy-MM-dd\"); %>';

Open in new window

0
Comment
Question by:techques
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 39

Expert Comment

by:abel
ID: 24115077
Try:

'<%= System.DateTime.Now.ToString("yyyy-MM-dd"); %>';

I don't know why you put in the backslashes there, but you are not inside a string, so they are not necessary.
0
 

Author Comment

by:techques
ID: 24115116
It has compile error:

CS1026: ) expected


0
 
LVL 53

Accepted Solution

by:
Ryan Chong earned 150 total points
ID: 24115148
You can always do that in Pure Javascript, you can try download this Date Parse Library:

http://www.javascripttoolbox.com/lib/date/source.php

and then try something like this:
<script type="text/javascript" src="js/date.js"></script>
<script language="javascript">
 
var strDate = new Date().format("yyyy-MM-dd");
 
document.write(strDate);
 
</script>

Open in new window

0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 39

Assisted Solution

by:abel
abel earned 150 total points
ID: 24115228
> CS1026: ) expected

the ";" is still there, wrong copy / paste, apologies. If you paste the following, and move with your mouse over the "move mouse" text, you will see the current date (name of the page Q_24312175.aspx, remove the first line(s) if you want to use it in your page, or add a form by that name).



<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Q_24312175.aspx.cs" Inherits="WebApplication1.Q_24312175" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
    <script language="javascript" type="text/javascript">
        var strDate = '<%= System.DateTime.Now.ToString("yyyy-MM-dd") %>';
        function getDate() { return strDate; }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <p onmousemove="this.innerHTML = getDate()">move mouse</p>
    </div>
    </form>
</body>
</html>

Open in new window

0
 
LVL 65

Assisted Solution

by:rockiroads
rockiroads earned 150 total points
ID: 24115272
I was going to suggest just get the data from Javascript like Ryan did. I didnt know there was a library involved though. Ryan, is that just for formatting?

Only methods I know is getyear(), getMonth() getDay(). Now these dont return leading zeros, so if u want that, you need to add it

eg

var currentDate = new Date();
var dStr = currentDate.getYear() + "/" + leadingZero(currentDate.getMonth()) + "/" +

leadingZero(currentDate.getDay());

alert(dStr);


and the leading zero function

function leadingZero(n)
{
      if (n < 10) n = "0" + n;
      return n;
}

0
 
LVL 53

Expert Comment

by:Ryan Chong
ID: 24115519
>>is that just for formatting?
yup, with some other features like doing validation and parsing for date string, etc. cheers
0
 

Author Comment

by:techques
ID: 24115832
Thanks for all help. I add more points for all experts.
0
 
LVL 39

Expert Comment

by:abel
ID: 24118682
> Thanks for all help.

you're welcome, glad we could have been of some help.

However, as a long-time regular to EE I have to point you to the fact that it is best to just split the points and not except your own comment (which is not an answer) as answer. That way, people visiting this question get the correct answer and the PAQ archive of EE stays proper.

-- Abel --
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question