Is it possible in JavaScript to keep Date objects in different time zones, e.g.:
date1.toString();
>>> 2012-02-16T14:00+02:00
date2.toString();
>>> 2012-03-16T13:00+01:00
i.e. I have two date objects they reflect the same moment of the time but keep their information in different time zones.
Is it possible in JavaScript to keep Date objects in different time zones, e.g.:
date1.toString();
>>> 2012-02-16T14:00+02:00
date2.toString();
>>> 2012-03-16T13:00+01:00
i.e. I have two date objects they reflect the same moment of the time but keep their information in different time zones.
Share Improve this question asked Feb 16, 2012 at 19:58 samsam 1,2512 gold badges13 silver badges15 bronze badges 1- 2 Internally, the dates/times are kept independent of the timezones. Timezones are only applied on output. – Jonathan M Commented Feb 16, 2012 at 20:01
3 Answers
Reset to default 7No. Dates in JavaScript represent a moment in time; they do not store time zone information. You can then choose to display what time that represents in a particular timezone. (See the various methods like getHours()
—current local time zone—versus getUTCHours()
.)
To display a time in a timezone other than UTC or the local you need to write (or use) a function that does a bit of math:
function offsetDate( date, hours ){
return date.setUTCHours( date.getUTCHours() + hours );
}
Edit: You can choose to store a custom offset along with a date (as you can add custom properties to any JS object):
Date.prototype.withZone = function(){
var o = new Date(this.getTime()); // Make a copy for mutating
o.setUTCHours(o.getUTCHours() + (this.tz || 0)); // Move the UTC time
// Return a custom formatted version of the date
var offset = this.tz ? (this.tz<0 ? this.tz : ('+'+this.tz)) : 'Z';
return o.customFormat('#YYYY#-#MMM#-#D# @ #h#:#mm##ampm# ('+offset+')');
}
// http://phrogz/JS/FormatDateTime_js.txt
Date.prototype.customFormat = function(formatString){
var YYYY,YY,MMMM,MMM,MM,M,DDDD,DDD,DD,D,hhh,hh,h,mm,m,ss,s,ampm,AMPM,dMod,th;
YY = ((YYYY=this.getUTCFullYear())+"").slice(-2);
MM = (M=this.getUTCMonth()+1)<10?('0'+M):M;
MMM = (MMMM=["January","February","March","April","May","June","July","August","September","October","November","December"][M-1]).substring(0,3);
DD = (D=this.getUTCDate())<10?('0'+D):D;
DDD = (DDDD=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"][this.getUTCDay()]).substring(0,3);
th=(D>=10&&D<=20)?'th':((dMod=D%10)==1)?'st':(dMod==2)?'nd':(dMod==3)?'rd':'th';
formatString = formatString.replace("#YYYY#",YYYY).replace("#YY#",YY).replace("#MMMM#",MMMM).replace("#MMM#",MMM).replace("#MM#",MM).replace("#M#",M).replace("#DDDD#",DDDD).replace("#DDD#",DDD).replace("#DD#",DD).replace("#D#",D).replace("#th#",th);
h=(hhh=this.getUTCHours());
if (h==0) h=24; if (h>12) h-=12;
hh = h<10?('0'+h):h;
AMPM=(ampm=hhh<12?'am':'pm').toUpperCase();
mm=(m=this.getUTCMinutes())<10?('0'+m):m;
ss=(s=this.getUTCSeconds())<10?('0'+s):s;
return formatString.replace("#hhh#",hhh).replace("#hh#",hh).replace("#h#",h).replace("#mm#",mm).replace("#m#",m).replace("#ss#",ss).replace("#s#",s).replace("#ampm#",ampm).replace("#AMPM#",AMPM);
}
var now = new Date; // Make a plain date
console.log( now.withZone() ); //-> 2012-Feb-16 @ 9:37pm (Z)
now.tz = -7; // Add a custom property for our method to use
console.log( now.withZone() ); //-> 2012-Feb-16 @ 2:37pm (-7)
As far as I can tell, you can't set it, but u can get it: Here is a ref: http://www.w3schools./jsref/jsref_gettimezoneoffset.asp
var d = new Date()
var gmtHours = -d.getTimezoneOffset()/60;
document.write("The local time zone is: GMT " + gmtHours);
Most browsers support toISOString(). Converting back to Date is not supported afaik. You can find a working solution here for cross-browser support.