Solved

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

Posted on 2009-04-10
8
3,858 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 50

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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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
 
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 50

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

790 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