Convert UTC time to a given time zone should not depend on the browser's time zone


This kind of question is asked before but I have little tricky situation. I have a datetime that's in UTC. I want to convert it specific timezone whatever we pass. Currently i am using moment-timezone library. What happens with it is, it considers the provided time in browser's timezone and then try to convert it in provided timezone. I want to make provided time to be considered in UTC.

js code :

var datetime = '2015/04/10 15:35:00';
datetime = moment(datetime).format('YYYY-MM-DD HH:mm:ss');
datetime = moment.utc(datetime);
return moment(datetime).tz(siteTimeZone).format('YYYY-MM-DD HH:mm:ss');

i have also tried by directly applying utc like:

var datetime = '2015/04/10 15:35:00';
datetime = moment.utc(datetime).format('YYYY-MM-DD HH:mm:ss');
return moment(datetime).tz(siteTimeZone).format('YYYY-MM-DD HH:mm:ss');

but none of them work as expected. May i am not clear about how moment-timezone is working.

Please help.


Just parse as utc before converting to the time zone.

var datetime = '2015/04/10 15:35:00';

return moment.utc(datetime,'YYYY/MM/DD HH:mm:ss')
             .format('YYYY-MM-DD HH:mm:ss');

Note also that I provided a format string while parsing, which matched the format of the value being passed in. This is a good practice, and will prevent moment from generating a warning on the console.