Jigar Joshi Jigar Joshi - 5 months ago 43
Javascript Question

moment.js format current datetime in multiple timezones

I need pacific time US & Canada in my javascript code. I am using below line of code for that

moment(new Date()).zone("-07:00").format('hh:mm A')

but if daylight is on then static -07:00 will give incorrect output

can you please assist how to get current server time in javascript using moment.js

Thanks in advance


You dont state what canadian time zone you want, but moment.js's timezone add on is likely what you need. With that you can do something like the below which has internal support for DST and not DST:

var pacific    = moment.tz("US/Pacific");
var canada = pacific.clone().tz("Canada/Eastern"); 

console.log(pacific.format('hh:mm A'));
console.log(canada.format('hh:mm A'));   
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.4.1/moment-timezone-with-data-2010-2020.min.js"></script>