Converting UTC to Pacific time using Moment Timezone (javascript)
You can do this in one step:
moment.tz(1412144245453, 'America/Los_Angeles').format('MM/DD/YYYY h:mm a')
OUTPUT: "09/30/2014 11:17 pm"
Also, you had evaluated the times for this timestamp incorrectly. In UTC, this timestamp is October 1st, 2014 6:17:25 AM. The corresponding Pacific time is indeed September 30th, 2014, 11:17:25 PM.
You can check this using a site like epochconverter.com, or in moment.js like so:
moment.utc(1412144245453).format() // "2014-10-01T06:17:25+00:00"
user3101143
Updated on June 14, 2022Comments
-
user3101143 over 1 year
I am trying to set "formattedLocalTime" to the Pacific time and my 4 lines of code look as below.
Though the chrome debugger displays "locTime" as "Tue Sep 30 2014 16:17:25" which is the correct value I expect, the formattedLocalTime in the last line is "09/30/2014 11:17 pm" which is UTC time and not the timezone I have set (America/Los_Angeles) which should be "09/30/2014 4:17 pm" (4:17 instead of 11:17)
Would highly appreciate any suggestions.
var timestamp = 1412144245453; // Tue Sep 30 2014 23:17:25 var utc = moment.tz(timestamp, "Etc/UTC"); // Tue Sep 30 2014 23:17:25 (displayed in chrome debugger) var locTime = utc.clone().tz("America/Los_Angeles"); // Tue Sep 30 2014 16:17:25 (displayed in chrome debugger) var formattedLocalTime = moment(locTime).format("MM/DD/YYYY h:mm a")
-
user3101143 about 9 yearsThank you @MajoB. locTime.format("MM/DD/YYYY h:mm a") however is still returning "09/30/2014 11:17 pm"
-
user3101143 about 9 yearsThank you Matt ... you were right on the dot. The time was getting from the DB had not set the timezone and Oracle implicity was adding the local timezone (PDT). "You can check this using a site like epochconverter.com" did the trick.