I really bad in JS and I'm struggling with moment.js
My wish is: get a date from a div (UTC) and convert this date depending on the timezone of the user.
So if the UTC date is 30/03/2017 6:34:22 AM
Someone in New York sees 30/03/2017 2:34:22 AM
Someone in Perth sees 30/03/2017 2:34:22 PM
I did something similar in C# with TimeZoneInfo and IsDaylightSavingTime but I don't know how to do this in JS.
Below my code:
// <div id="utc-time">30/03/2017 6:34:22 AM</div>
var dt = document.getElementById("utc-time").innerHTML;
var m = moment(dt, "DD-MM-YYYY hh:mm:ss a");
var tz = moment.tz.guess();
var user = moment.tz(dt, "DD-MM-YYYY hh:mm:ss a", tz);
var mString = m.toString();
// Console.log
// -----------
dt = 30/03/2017 6:34:22 AM
m = 1490826862000
user = Thu Mar 30 2017 06:34:22 GMT+0800
mString = Wed Mar 29 2017 22:34:22 GMT+0000
It looks like the moment created is not UTC but keep the local timezone. A hint would be nice :)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…