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

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

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
techques
Asked:
techques
  • 3
  • 2
  • 2
  • +1
3 Solutions
 
abelCommented:
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
 
techquesAuthor Commented:
It has compile error:

CS1026: ) expected


0
 
Ryan ChongCommented:
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
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

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

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

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