1

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.

Praveen
  • 55,303
  • 33
  • 133
  • 164
  • Possible duplicate of http://stackoverflow.com/questions/2404247/datetime-to-javascript-date – Slate Jan 07 '14 at 11:14
  • 2
    Not duplicate of above... – Pranav Singh Jan 07 '14 at 11:20
  • Have a look at this: http://stackoverflow.com/questions/439630/how-do-you-create-a-javascript-date-object-with-a-set-timezone-without-using-a-s They recommend some straightforward ways like setUTCHour() or Date.UTC(). But can you please clarify the question, it's not clear whether you want the input date as UTC or the output date as UTC. – Sandman Jan 09 '14 at 17:15

2 Answers2

1

You can use following method for getting UTC.

var dat = new Date();
dat.toUTCString();
Venkatesh K
  • 4,364
  • 4
  • 18
  • 26
1

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"
Bergi
  • 630,263
  • 148
  • 957
  • 1,375