javascript - Get local time representation of Date in arbitrary timezone in arbitrary day - considering Daylight Savings Time -


for sure, there lot of questions date objects , timezones many of them converting current time timezone, , others not clear want do.

i want display day, hour, minute etc. in arbitrary timezone, in arbitrary day. example, function f(t, s) that:

  • given timestamp 1357041600 (which 2013/1/1 12:00:00 utc) , string "america/los angeles", satisfy comparison below:

    f(1357041600, "america/los angeles") == "2013/01/01 04:00:00" 
  • given timestamp 1372680000 (2013/07/01 12:00:00 utc), satisfy comparison below:

    f(1357041600, "america/los angeles") == "2013/07/01 05:00:00" 
  • will behave way if timezone in browser is, let "europe/london" or "america/são paulo".

  • will behave way if time in browser is, let 2014/02/05 19:32, or 2002/08/04 07:12; and

  • as final restriction, not request server side (because i'm doing myself :) )

is possible?

given timestamp 1357041600 (which 2013/1/1 12:00:00 utc)

that appears seconds since unix epoch (1970-01-01t00:00:00z). javascript uses same epoch, in milliseconds create suitable date object:

var d = new date(timestamp * 1000); 

that create date object suitable time value. need determine time zone offset using iana time zone database. can applied date object using utc methods. e.g. resolve offset minutes, use:

d.setutcminutes(d.getutcminutes() + offset) 

utc methods can used adjusted date , time values create string in whatever format require:

var datestring = d.getutcfullyear() + '/' + pad(d.getutcmonth() + 1) + '/' ... 

where pad function add leading 0 single digit values. using utc methods avoids impact of local time zone offsets , daylight saving variances.

there libraries timezone.js can used determine offset, have not used them no endorsement implied.


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -