javascript - moment-timezone parsing the given time zone -


i have issue when parsing date object using moment-timezone

the problem

i creating nodejs app should check new date() object arbitrary time database (and act accordingly). time, , timezone persisted in database.

in example

time | timezone 11:00| us/eastern 

when rest call comes, have take new date() object , transform given timezone , see whether current time later 9am. servers timezone , persisted timezone not same.

the issue

i create todays date string

function gettodaysdate() {     var today = new date(),         dd = today.getdate(),         mm = today.getmonth()+1,         yyyy = today.getfullyear();      if(dd<10) {         dd='0'+dd     }      if(mm<10) {         mm='0'+mm     }      return yyyy +'-' + mm + '-' + dd;  } 

and trying create timestamp object moment-timezone

starttime = moment.tz(new date(gettodaysdate() + ' ' + '11:00'), 'us/eastern'); 

but framework correctly takes date , transforms us/eastern time zone.

so when print starttime.format();

i

2016-08-01t07:00:00-04:00 

and like

2016-08-01t11:00:00-04:00 

so there way using moment-timezone package set date , time , treat them given timezone?

you don't need of date object manipulation. in theory, should able do:

var zone = 'us/eastern' var time = '11:00'  var result = moment.tz(time, 'hh:mm', zone).format(); 

however, there's a known bug uses utc current date instead of time zone's current date. until it's fixed, have instead:

var zone = 'us/eastern' var time = '11:00'  var s = moment.tz(zone).format('yyyy-mm-dd') + ' ' + time; var m = moment.tz(s, zone); var result = m.format(); 

(this assumes input time value in hh:mm format)


Comments