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

Javascript DATE and C# date - what is the best solution? - Stack Overflow

programmeradmin3浏览0评论

I am getting a date from server side C# using the following code:

DateTime d1 = new DateTime(1970, 1, 1);
DateTime d2 = (DateTime)cdTimestamp2;
long x = new TimeSpan(d2.Ticks - d1.Ticks).TotalMilliseconds;

When I get my code on the javascript side:

function (timestamp) {
    alert("testing :" + new Date(timestamp))
}

This gives me a fully formatted date but it does not bring the time of my timezone since if it is 17.15 here, it provides me with 19.15 GMT +2 !

At first I simply tried to pass my c# timestamp, without any of the code above and found this question: How do I format a Microsoft JSON date? But I have no idea what JSON is and I couldn't derive what I can do! Is it easier to use JSON? If so can anyone guide me? Thank you very much


Edit: The Solution - I did not use universal time on the server side. I left server side code as it is. All I did is this:

new Date(timestamp).toUTCString()

I am getting a date from server side C# using the following code:

DateTime d1 = new DateTime(1970, 1, 1);
DateTime d2 = (DateTime)c.ccdTimestamp2;
long x = new TimeSpan(d2.Ticks - d1.Ticks).TotalMilliseconds;

When I get my code on the javascript side:

function (timestamp) {
    alert("testing :" + new Date(timestamp))
}

This gives me a fully formatted date but it does not bring the time of my timezone since if it is 17.15 here, it provides me with 19.15 GMT +2 !

At first I simply tried to pass my c# timestamp, without any of the code above and found this question: How do I format a Microsoft JSON date? But I have no idea what JSON is and I couldn't derive what I can do! Is it easier to use JSON? If so can anyone guide me? Thank you very much


Edit: The Solution - I did not use universal time on the server side. I left server side code as it is. All I did is this:

new Date(timestamp).toUTCString()
Share Improve this question edited May 23, 2017 at 12:24 CommunityBot 11 silver badge asked Apr 11, 2012 at 15:18 testtest 2,6185 gold badges38 silver badges53 bronze badges 1
  • 1 Are the server and the client located in different timezones? – Joshua Drake Commented Apr 11, 2012 at 15:26
Add a comment  | 

3 Answers 3

Reset to default 11

What you should do is:

  • Always use UTC times on the server
  • Send UTC times to the browser as unit time stamps as you do now
  • Convert the time stamp to local time in the browser

The timestamp used represents: 2012-04-11T15:46:29+00:00:

var d = new Date ( 1334159189000 );
// gives you back 2012-04-11T15:46:29+00:00 in a slightly different format, but the timezone info matches UTC/GMT+0
d.toUTCString();
// gives you back your local time
d.toLocaleString();

Just created a jsfiddle to show that it does what it is supposed to:
http://jsfiddle.net/t8hNs/1/

use

var currentDate = new Date();
//get off set from your browser
var offset = Date.getTimezoneOffset();

you can use JavaScriptSerializer

string json = new JavaScriptSerializer().Serialize(DateTime.Now);
发布评论

评论列表(0)

  1. 暂无评论