Here is a UTC dateTime string as

Jan 7, 2014 10:37:42 AM  //parsed UTC date time

When I parsed this string to date like

var date = new Date("Jan 7, 2014 10:37:42 AM")

This returns the dateTime in local format like

Tue Jan 07 2014 05:07:42 GMT+0530 (India Standard Time) 

How can I say that this date is already in UTC?

Here are my tries:
1. Appending UTC to dateTime as

var now = new Date('Jan 7, 2014 10:37:42 AM UTC');
//returns Tue Jan 07 2014 16:07:42 GMT+0530 (India Standard Time)

Update: I think I got it

var now = new Date('Jan 7, 2014 10:37:42 AM');
var UTC = new Date(now.toUTCString())
console.log(UTC) // returns Tue Jan 7 10:37:42 UTC+0530 2014 

But not sure whether this is right or wrong.

有帮助吗?

解决方案

When I parsed this string to date like

var date = new Date("Jan 7, 2014 10:37:42 AM")

This returns the dateTime in local format like Tue Jan 07 2014 05:07:42 GMT+0530 (India Standard Time)

How can I say that this date is already in UTC?

No, it does not return a dateTime in any format. It does return a Date object with the internal value of 1389091062000 milliseconds since epoch.

Only when you output it (e.g. when logging it to the console, or casting it to a string with .toString) it will be represented in your locale's timezone.

If you want to control the output, you would indeed use the toUTCString method:

var date = new Date("Jan 7, 2014 10:37:42 AM")
date.toUTCString(); // "Tue, 07 Jan 2014 10:37:42 GMT"

其他提示

You can use following method for getting UTC.

var dat = new Date();
dat.toUTCString();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top