简体   繁体   English

具有特定时区的Javascript整数到日期时间

[英]Javascript integer to datetime with specific timezone

I have the following datetime saved in an SQL Server 2005 database: 2012-12-06 16:20:11.010 . 我在SQL Server 2005数据库中保存了以下日期时间: 2012-12-06 16:20:11.010

I want to display this datetime using javascript on the client side, so I serialize it (as part of a larger object) using System.Web.Helpers.Json.Encode , which runs the following code: 我想在客户端使用javascript显示这个日期时间,所以我使用System.Web.Helpers.Json.Encode序列化它(作为更大对象的一部分),它运行以下代码:

internal static readonly long DatetimeMinTimeTicks = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).Ticks;

private static void SerializeDateTime(DateTime datetime, StringBuilder sb, JavaScriptSerializer.SerializationFormat serializationFormat)
{
    if (serializationFormat == JavaScriptSerializer.SerializationFormat.JSON)
    {
        sb.Append("\"\\/Date(");
        sb.Append((datetime.ToUniversalTime().Ticks - JavaScriptSerializer.DatetimeMinTimeTicks) / 10000L);
        sb.Append(")\\/\"");
        return;
    }
    sb.Append("new Date(");
    sb.Append((datetime.ToUniversalTime().Ticks - JavaScriptSerializer.DatetimeMinTimeTicks) / 10000L);
    sb.Append(")");
}

On the client side, I convert it into a datetime object like so: ( JSFiddle ) 在客户端,我将它转换为日期时间对象,如下所示:( JSFiddle

var strDate = "/Date(1354828811010)/"; //value returned by above function
var re = /-?\d+/;
var m = re.exec(strDate);            
var date = new Date(parseInt(m[0]));

On a PC with the timezone set to Eastern Standard, this time appears correctly as Thu Dec 06 2012 16:20:11 GMT-0500 (Eastern Standard Time) . 在时区设置为东部标准的PC上,此时间正确显示为Thu Dec 06 2012 16:20:11 GMT-0500 (Eastern Standard Time) However, on a pc with the timezone as GMT-6, it returns as Thu Dec 06 2012 15:20:11 GMT-0600 (Central Standard Time) . 但是,在时区为GMT-6的电脑上,它将返回Thu Dec 06 2012 15:20:11 GMT-0600 (Central Standard Time)

This time is correct as Thu Dec 06 2012 16:20:11 GMT-0600 (Central Standard Time) , but I'm not sure how to best display it correctly. 这个时间是正确的,如Thu Dec 06 2012 16:20:11 GMT-0600 (Central Standard Time) ,但我不确定如何最好地正确显示它。 I don't need to actually display the timezone, I just need it to show up as 16:20:11 regardless of the user's local timezone settings. 我不需要实际显示时区,我只需要它显示为16:20:11,无论用户的本地时区设置如何。

Is there a mechanism in javascript to let me force a date to a specific timezone? 是否有javascript中的机制让我强制约会到特定的时区? The research I've done on it suggests there isn't. 我所做的研究表明没有。

Otherwise, if I need to make a change server side, what's my best bet? 否则,如果我需要改变服务器端,那么我最好的选择是什么?

I believe what you're looking for is related to what is described in Moment.js issue 611 . 我相信你所寻找的东西与Moment.js第611期中描述的内容有关。 If so, would you please add your feedback to this issue? 如果是这样,请您在此问题上添加您的反馈意见吗? It is certainly a showstopper for me. 对我来说,这无疑是一个障碍。

BTW - I looked at DateJS and XDate also, and as far as I can tell, none have this functionality. BTW - 我也看了DateJS和XDate,据我所知,没有人有这个功能。 JavaScript seems locked in at either UTC or the local timezone of the browser. JavaScript似乎锁定在UTC或浏览器的本地时区。

Also, Moment.js already handles the Microsoft date format that JavaScriptSerializer emits, so you can use it directly. 此外,Moment.js已经处理了JavaScriptSerializer发出的Microsoft日期格式,因此您可以直接使用它。 However, I would strongly recommend using JSON.Net instead. 但是,我强烈建议使用JSON.Net Even Microsoft is using this now. 甚至微软现在都在使用它。 It will return dates in ISO format by default. 默认情况下,它将以ISO格式返回日期。

Take a look at Moments.js , it is one of a number of javascript date libraries that can help you with your date/time requirements. 看看Moments.js ,它是众多javascript日期库中的一个,可以帮助您满足日期/时间要求。

others include datejs and Xdate 其他包括datejsXdate

DateTime.ToUniversalTime() is going to include the timezone in the serialized value and javascript is going to read it. DateTime.ToUniversalTime()将在序列化值中包含时区,javascript将读取它。 If you don't want that to happen don't serialize it with the timezone. 如果您不希望发生这种情况,请不要使用时区对其进行序列化。 Just use the DateTime.toString() method and give it a simple format string like: 只需使用DateTime.toString()方法并为其提供一个简单的格式字符串,如:

datetime.toString("o");

which will output something like 这将输出类似的东西

2008-06-15T21:15:07.0000000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM