javascript日期和C#日期 - 什么是最好的解决方案? [英] Javascript DATE and C# date - what is the best solution?
问题描述
我使用下面的代码获得从服务器端的C#日期:
DateTime的D1 =新的日期时间(1970年1 ,1);
的DateTime D2 =(DateTime的)c.ccdTimestamp2;
长×=新的时间跨度(d2.Ticks - d1.Ticks).TotalMilliseconds;
当我得到的JavaScript一边我的代码:
功能(时间戳){
警报(测试+新的日期(时间戳))
}
这给了我一个完全格式化的日期,但它并没有带给我的时区的时间,因为如果是17.15这里,它为我提供了19.15 GMT + !2
起初我只是试图通过我的C#的时间戳,没有任何上面的代码,发现了这个问题:的如何格式化JSON的日期?但我不知道JSON是什么,我无法得到我能做些什么!它是更容易使用JSON?如果是这样任何人都可以指导我?非常感谢你。
编辑:解决方案
- 我没有在服务器端使用通用的时间。我离开了服务器端代码,因为它是。我所做的一切是这样的:
新的日期(时间戳).toUTCString()
你应该做的是:
- 始终在服务器上使用UTC时间
- 发送UTC时间,以浏览器作为单位时间戳记,你现在要做的
- 转换时间戳为本地时间在浏览器
所使用的时间戳表示: 2012-04-11T15:46 :29 + 00:00
:
变种D =新的日期(1334159189000);
//给你回2012-04-11T15:46:29 + 00:在一个稍微不同的格式00,但时区信息UTC / GMT + 0
d.toUTCString匹配();
//给你回你的本地时间
d.toLocaleString();
刚刚创建的jsfiddle表明,它做什么,它应该是:结果
http://jsfiddle.net/t8hNs/1/
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 to format a 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()
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/
这篇关于javascript日期和C#日期 - 什么是最好的解决方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!