Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do the getUTC* methods on the date object do?

What does it mean when you get or create a date in UTC format in JavaScript?

like image 648
ScottKoon Avatar asked Aug 18 '08 22:08

ScottKoon


People also ask

What is the best method to check if an object is a date object?

Method 1: Using instanceof operator: The instanceof operator checks whether the prototype property of a constructor appears anywhere in the prototype chain of an object. In this case, it is used to check whether the object is an instance of the Date object or not.


1 Answers

A date represents a specific point in time. This point in time will be called differently in different places. As I write this, it's 00:27 on Tuesday in Germany, 23:27 on Monday in the UK and 18:27 on Monday in New York.

To take an example method: getDay returns the day of the week in the local timezone. Right now, for a user in Germany, it would return 2. For a user in the UK or US, it would return 1. In an hour's time, it will return 2 for the user in the UK (because it will then be 00:27 on Tuesday there).

The ..UTC.. methods deal with the representation of the time in UTC (also known as GMT). In winter, this is the same timezone as the UK, in summer it's an hour behind the time in the UK.

It's summer as I write this. getUTCDay will return 1 (Monday), getUTCHours will return 22, getUTCMinutes will return 27. So it's 22:27 on Monday in the UTC timezone. Whereas the plain get... functions will return different values depending on where the user is, the getUTC.. functions will return those same values no matter where the user is.

like image 85
Jon Bright Avatar answered Oct 05 '22 21:10

Jon Bright