最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

DateTime JavaScript vs C# - Stack Overflow

programmeradmin3浏览0评论

Regardless of many posts I've read the magic still in my code. I have DateTime value in db ('2011-03-30 00:00:00.000') that I retrieve for asp/mvc page where some javascript needs to read it and pare. The magic in the following:

<% 
DateTime unixTimeOffset = new DateTime(1970, 1, 1, 0, 0, 0, 0);
DateTime testDate = new DateTime(2011,03,30,0,0,0,0);
%>
<%= (testDate - unixTimeOffset).TotalMilliseconds %>
...

Last string of the code gives me this value: 1301443200000 When I try to read it in JavaScript I have:

val myDate = new Date(1301443200000);

And myDate is Tue Mar 29 2011 20:00:00 GMT-0400 (Eastern Daylight Time) {} But not a March 30th as it should be.

I understand it provides date referencing to local time, GMT-4 but what is the solution to get it independent? Any ideas? Thanks.

Regardless of many posts I've read the magic still in my code. I have DateTime value in db ('2011-03-30 00:00:00.000') that I retrieve for asp/mvc page where some javascript needs to read it and pare. The magic in the following:

<% 
DateTime unixTimeOffset = new DateTime(1970, 1, 1, 0, 0, 0, 0);
DateTime testDate = new DateTime(2011,03,30,0,0,0,0);
%>
<%= (testDate - unixTimeOffset).TotalMilliseconds %>
...

Last string of the code gives me this value: 1301443200000 When I try to read it in JavaScript I have:

val myDate = new Date(1301443200000);

And myDate is Tue Mar 29 2011 20:00:00 GMT-0400 (Eastern Daylight Time) {} But not a March 30th as it should be.

I understand it provides date referencing to local time, GMT-4 but what is the solution to get it independent? Any ideas? Thanks.

Share Improve this question edited Oct 30, 2012 at 17:48 Maxim asked Mar 28, 2011 at 19:41 MaximMaxim 4,2348 gold badges52 silver badges77 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

For C# Related Dates:

You may want to consider using DateTime.ToUniversalTime() and .UTCNow(), which will allow you to keep all days independent of time-zones.

 DateTime date = DateTime.UTCNow();

For Javascript-Related Dates:

Javascript features a UTC Method that should get you the Universal Time as well.

//Declaration
var date = new Date(Date.UTC(YYYY,MM,DD));

//Use
var newDate = date.toUTCString();

Hope this helps :)

Get the timezone offset in the client and add that to the value from the server. Like this:

var serverOffset = 1301443200000;
var localOffset = myDate.getTimezoneOffset() * 60 * 1000;
var myDate = new Date(serverOffset + localOffset);
console.log(myDate); // Wed Mar 30 2011 00:00:00

Please note that Tue Mar 29 2011 20:00:00 GMT-0400 (Eastern Daylight Time) is the same point in time as your original date in UTC. This method creates a new date that represents a new point in time. However, if you're just trying to display the same date value, then this method does the trick.

Alternatively, you can use Date's UTC methods to print your original date in UTC:

function pad(num) {
    return ("0" + num).slice(-2);
}

function formatDate(d) {
    return [d.getUTCFullYear(), 
            pad(d.getUTCMonth() + 1), 
            pad(d.getUTCDate())].join("-") + "T" + 
           [pad(d.getUTCHours()), 
            pad(d.getUTCMinutes()), 
            pad(d.getUTCSeconds())].join(":") + "Z";
}

formatDate(new Date(1301443200000));

Output:

"2011-03-30T00:00:00Z"

Your code is correct. The myDate variable holds the correct date since Tue Mar 29 2011 20:00:00 GMT-0400 is the same point in time as Wed Mar 30 2011 00:00:00 GMT+0000. My guess is that you see the former since that is your puters time zone. Use myDate.toUTCString() to see the date as UTC.

发布评论

评论列表(0)

  1. 暂无评论