Solved

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

Posted on 2009-04-10
8
3,836 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
  • 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 49

Accepted Solution

by:
Ryan Chong earned 50 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
 
LVL 39

Assisted Solution

by:abel
abel earned 50 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 65

Assisted Solution

by:rockiroads
rockiroads earned 50 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 49

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

920 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now