开发者

json dates and timezones

开发者 https://www.devze.com 2023-03-24 04:58 出处:网络
I have the following code: $(function () { var thedate = \"/Date(1198908717056)/\"; var thedate2 = ProcessDate(thedate)

I have the following code:

$(function () {

    var thedate = "/Date(1198908717056)/";
    var thedate2 = ProcessDate(thedate)
    alert(thedate2);

});

function ProcessDate(DateString) {

    var TheDate = eval(DateString.replace(/\/Date\((\d+)\)\//gi, "new Date($1)"));
    return TheDate;
}

When it runs, it returns a开发者_如何学Gon alert with December 29 and the time is showing as Eastern Time. When I change the timezone on my computer, it's still showing the date in the Eastern timezone.

My question is this: does the string "/Date(1198908717056)/" contain the timezone information or is the timezone displayed in the alert the result of the browser determining my timezone?

Thanks.


JSON doesn't have dates at all (it's one of JSON's flaws). Those strings are just strings.

Some frameworks, like ASP.Net, use that syntax to indicate dates. What timezone they're in will be dictated by the framework. I believe the dates are in UTC and so you can just use the new Date(Number) constructor to create them (more in this other answer). That creates the date by directly setting its internal "milliseconds since The Epoch UTC" value, more in section 15.9 of the specification. Mind you, that only works if, in fact, whatever it is creating these pseudo-date strings is using UTC.


Update: Looking at your code, although it works, this line:

var TheDate = eval(DateString.replace(/\/Date\((\d+)\)\//gi, "new Date($1)"));

...is an abuse of eval. eval should be avoided whenever possible. Instead, if you want to keep it as a one-liner:

var TheDate = new Date(Number(DateString.replace(/\/Date\((\d+)\)\//gi, "$1")));

...or somewhat more readably:

var Match = /\/Date\((\d+)\)\//gi.exec(DateString);
var TheDate;
if (Match) {
  TheDate = new Date(Number(Match[1]));
}

In all of those cases, the Date will be initialized with the UTC time value embedded in the date string. But then when you ask JavaScript to format the date (for instance, via toString), it will use your local timezone to do that. You haven't shown how you're outputting the date, so I can't tell you why the timezone seems not to change if you change your timezone (perhaps the browser didn't pick up the change?). When I do it, if I output the date as a string, it shows it in British Summer Time (which is my current timezone) if I use toString, or UTC if I use toUTCString. Here's a live example using both your original date, and a date (today's date, as I write this) that's in daylight savings time so even in the UK you can see the difference between UTC and local time.


Off-topic: In JavaScript, the overwhelming convention is to use camelCased names starting with a lower-case letter for both local variables and function names. So, theDate rather than TheDate. Initial caps are reserved for constructor functions (like Date). You're free to ignore the convention, of course, but it will tend to make it difficult for others to read your code.


The timezone is taken from your current system setting. Have a look at the Date class.

The given value is in milliseconds and does not contain a timezone. The constructor of Date() expects the milliseconds to be given in UTC. If you have values with a known timezone, you should use the dateString constructor version.

However, as far as I know, there is no way convert between timezones in JavaScript, except for UTC and the local system timezone.

0

精彩评论

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

关注公众号