2

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
  • 137
  • 1
  • 3
  • 10

3 Answers3

6

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"
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • Thank 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. – user3101143 Oct 01 '14 at 19:36
0

try to use:

var formattedLocalTime = locTime.format("MM/DD/YYYY h:mm a")

if you write moment(locTime) then your datetime will be converted back to local time

Marian Ban
  • 8,158
  • 1
  • 32
  • 45
0

Use: moment-timezone - TypeError: moment().tz is not a function

const moment = require('moment-timezone');
const time = moment.tz(1412144245453, 'America/Los_Angeles').format('MM/DD/YYYY h:mm a');
console.log("time : ", time);

Output: time : 09/30/2014 11:17 pm

Sahil Thummar
  • 1,926
  • 16
  • 16