• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3925
  • Last Modified:

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


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\'); %>';
var strDate = '<%= System.DateTime.Now.ToString(\"yyyy-MM-dd\"); %>';

Open in new window

  • 3
  • 2
  • 2
  • +1
3 Solutions

'<%= 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.
techquesAuthor Commented:
It has compile error:

CS1026: ) expected

Ryan ChongCommented:
You can always do that in Pure Javascript, you can try download this Date Parse Library:


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");

Open in new window

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

> 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">
    <script language="javascript" type="text/javascript">
        var strDate = '<%= System.DateTime.Now.ToString("yyyy-MM-dd") %>';
        function getDate() { return strDate; }
    <form id="form1" runat="server">
        <p onmousemove="this.innerHTML = getDate()">move mouse</p>

Open in new window

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


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



and the leading zero function

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

Ryan ChongCommented:
>>is that just for formatting?
yup, with some other features like doing validation and parsing for date string, etc. cheers
techquesAuthor Commented:
Thanks for all help. I add more points for all experts.
> 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 --
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now