0
a = new Date();
Sun Apr 08 2012 16:58:03 GMT+0530 (IST)

Is there any way i can get the current UTC time? I thought of getting an offset doing maths:

b = a.getTimezoneOffset()
-330

then subtract, get the value:

c = a - b
1333884483552

but again getting c as a to look is difficult. So the question: How can i get the current UTC time, in javascript?

whatf
  • 6,378
  • 14
  • 49
  • 78

3 Answers3

3

First of all, Date object in JavaScript is timezone-independent. It only stores the number of milliseconds since epoch. Furthermore it always uses browser time zone for toString() and get*() methods. You cannot create Date instance in a different time zone.

Thus simply use getUTC*() family of methods:

new Date().getUTCHours()
new Date().getUTCMinutes()
//...

to obtain time in UTC.

Last but not least - your code is broken. a variable represents Date and is casted to milliseconds here: c = a - b. However b is equal to a.getTimezoneOffset(). The time zone offset is in minutes. You are subtracting minutes from milliseconds...

See also

Community
  • 1
  • 1
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • please mention the script that i would write so as to get the current utc time, as seen in the variable a. – whatf Apr 08 '12 at 11:44
  • @whatf huh? You cannot create `Date` in UTC. Period. But you *can* create `Date` and then ask what time does it represent in UTC (as opposed to time in browser/OS time zone). What kind of code do you want? – Tomasz Nurkiewicz Apr 08 '12 at 11:46
1

You can use the toUTCString function if you need the string.

new Date().toUTCString()
Wouter Dorgelo
  • 11,770
  • 11
  • 62
  • 80
pbfy0
  • 616
  • 4
  • 13
0

There is a plugin called jstimezonedetect which you can use to detect the timezone. You can find it here

Or use date's UTC methods like

var now = new Date(); 
var now_utc = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(),  now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds());

Demo

Outputs

Date {Sun Apr 08 2012 18:31:50 GMT+0545 (Nepal Standard Time)}

Date {Sun Apr 08 2012 12:46:50 GMT+0545 (Nepal Standard Time)}

Starx
  • 77,474
  • 47
  • 185
  • 261
  • This is **not** a correct solution, although it looks like one... My local time is now `14:45:34 GMT+0200 (CEST)`, your code returns `12:45:34 GMT+0200 (CEST)`. It didn't return my current time in UTC. Instead it returns `Date` 2 hours in the past, still in my local time zone `GMT+0200 (CEST)`. – Tomasz Nurkiewicz Apr 08 '12 at 12:47
  • No, I am not ;-). And your demo support me. `Date` represents point in time, irrespective to time zone. Your code should return `12:46:50 GMT+0000` for `18:31:50 GMT+0545` - these represent **the same point in time**. However your code returns two different points in time, both in `GMT+0545`. See: http://www.odi.ch/prog/design/datetime.php - it is about Java, but `Date` in JS is 1:1 to `Date` in Java. In other words - two same dates in different time zones should have the same number of millis since epoch (they represent same point in time). Try calling `+now` and `+now_utc` on your code... – Tomasz Nurkiewicz Apr 08 '12 at 13:05
  • @TomaszNurkiewicz, Ok, I am confused – Starx Apr 08 '12 at 13:11