开发者

injecting today's date in javascript

When the page loads, I want a variable inside my JavaScript to hold today's date. So far I have this:

<script type="text/javascript">
    var TodayDate = <%Eval('System.DateTime.Now') %>;
</script>

It m开发者_StackOverflow社区assively bugs. Any suggestions?


Just use a javascript date object.

var today = new Date();

Or if you need the server time

var today = new Date('<%= DateTime.Now.ToString() %>');


is there anything in particular wrong with

<script type="text/javascript">
    var TodayDate = new Date();
</script>

?


Why don't u use Javascript Date() object directly?

<script type="text/javascript">
    var TodayDate = Date();
</script>


If you want to have a variable containing the date on the server, your way of doing is probably good (except for the missing apostrophes and the Date constructor). So it should be:

var today = new Date('<%= DateTime.Now.ToString() %>'); 
//this assumes that the server has such a locale that DateTime.ToString can be
//parsed by the javascript interpreter

But, if you need to have the date of the client:

var today = new Date();


You can use the previous people's answers of setting the date from within Javascript.

The problem you're seeing is that ASP is putting the string representing the current Date directly into the JavaScript source code. You need to put quotes around it so JavaScript sees a string and can parse it corretly.

Something like:

<script type="text/javascript">
    var TodayDate = "<%Eval('System.DateTime.Now') %>";
</script>

which sets TodayDate to be a string. Are you going to use it as such or as a Date object?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜