Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date.UTC() for years between 0-100

Tags:

javascript

Running into a problem utilizing Date.UTC() because Date.UTC() automatically converts any year between 0-99 into 1900-1999 when I need the function to maintain the original year passed to it. I have tried passing a string for the year with leading 0's but I am still having the same problem.

UPDATE: Thanks for the replies and after reading the MDN doc, I resorted to a work-around which works for me in Chrome & Firefox.

function getUTC(date) {
  var year = date.getUTCFullYear();
  if (year >= 0 && year < 100) {
    //calculate the difference in ms between UTC time and local time
    var diff = (new Date(0,0,1,0,0,0,0)).getTime() - Date.UTC(0,0,1,0,0,0,0);
    //return the local time plus the calculated difference
    return date.getTime() + diff;
  }
  return Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds(), date.getUTCMilliseconds());
}

An easier way of getting the UTC time is to assign the return value of .setUTCFullYear(1200,0,1) or whichever .setUTC______() method to a variable.

var date = new Date();
var time = date.setUTCFullYear(1200,0,1)
// time equals -24298834086121
like image 668
b_d Avatar asked Feb 22 '23 16:02

b_d


2 Answers

According to MDN, the UTC() method does not support years before 100:

If a year between 0 and 99 is specified, the method converts the year to a year in the 20th century (1900 + year); for example, if you specify 95, the year 1995 is used.

You'll probably have to specify the individual components so you can use setUTCFullYear().

var d = new Date(Date.UTC(95, 0, 31, 23, 59, 59));
console.log(d);
d.setFullYear(95);
console.log(d);
like image 51
Álvaro González Avatar answered Feb 25 '23 05:02

Álvaro González


Although it can be hard to create a date for a year before AD 100, it is easy to set an existing Date object to any year, including negative numbers for BC years.

Note that only IE includes 'BC' in the returned string, and correctly sets BC years. You can set the year to 0, which cannot really exist, in non-IE browsers, and they include the nonexistant year when adding or subtracting years that span AD and BC dates.

IE sets a year assigned as 0 to the first possible year before 1AD, which is 1 BC.

one BC is only 1 year from one AD, not 2. 5 years before one AD is five BC, not four BC.

function bc(){
    var D= new Date(), A= [].slice.call(arguments, 0);
    D.setFullYear.apply(D, A);
    if(A[0]<= 0 &&!document.documentMode){
        D.setFullYear(D.getFullYear()-1);
    }
    return D;
}
bc(50, 0, 1, 0, 0, 0, 0).toUTCString()+'\n\t'+
bc(-50, 0, 1, 0, 0, 0, 0).toUTCString()

/*
MSIE 8.0 returns:
    Sat, 1 Jan 50 13:41:00 UTC
    Sun, 1 Jan 51 B.C. 13:41:00 UTC

Firefox 7.0.1 returns:
    Sat, 01 Jan 0050 13:41:35 GMT
    Sat, 01 Jan -0051 13:41:35 GMT

Opera 11.51 returns:
    Sat, 01 Jan 0050 13:42:11 GMT
    Sat, 01 Jan -051 13:42:11 GMT

MSIE 10.0 returns:
    Sat, 1 Jan 50 13:42:36 UTC
    Sun, 1 Jan 51 B.C. 13:42:36 UTC

Chrome 14.0.835.163 returns:
    Sat, 01 Jan 50 13:43:37 GMT
    Sat, 01 Jan -51 13:43:37 GMT

Safari 5.1 returns:
    Sat, 01 Jan 0050 13:44:07 GMT
    Sat, 01 Jan -051 13:44:07 GMT
*/
like image 32
kennebec Avatar answered Feb 25 '23 05:02

kennebec