admin管理员组

文章数量:1026373

Im trying to get the EST date using moment.js but it always gives me the local datetime.

var curr_date    = moment.tz(new Date(), "America/New_York");

when I execute this mand in console, I don't get the NY time.

Im trying to get the EST date using moment.js but it always gives me the local datetime.

var curr_date    = moment.tz(new Date(), "America/New_York");

when I execute this mand in console, I don't get the NY time.

Share Improve this question asked Apr 28, 2016 at 10:49 user1050619user1050619 21k89 gold badges251 silver badges430 bronze badges 6
  • It stores a moment instance in curr_date. For me, when I execute this in console, it returns the object correctly. What do you get if you enter curr_date.format("HH:mm") afterwards? – user3297291 Commented Apr 28, 2016 at 10:59
  • I'm in Central Time and it gives the central time instead of EST time – user1050619 Commented Apr 28, 2016 at 11:00
  • The alert statement gives me the EST time but when I print in console it gives me the actual time(CST). – user1050619 Commented Apr 28, 2016 at 11:07
  • this worked...var curr_date = moment.tz(new Date(), "America/New_York");...curr_date.format() – user1050619 Commented Apr 28, 2016 at 11:19
  • 1 the _d object that you are seeing in the console does not reflect the actual value of the date contained in moment. Ignore any properties starting with _. Just pay attention to the result of .format() – Maggie Pint Commented Apr 28, 2016 at 18:46
 |  Show 1 more ment

1 Answer 1

Reset to default 3

I found this post really helpful. Basically, stop using the Date object explicitly.

const moment = require('moment-timezone')
const timeInEST = moment().tz('America/New_York').format('MM/DD/YYYY HH:mm')

Im trying to get the EST date using moment.js but it always gives me the local datetime.

var curr_date    = moment.tz(new Date(), "America/New_York");

when I execute this mand in console, I don't get the NY time.

Im trying to get the EST date using moment.js but it always gives me the local datetime.

var curr_date    = moment.tz(new Date(), "America/New_York");

when I execute this mand in console, I don't get the NY time.

Share Improve this question asked Apr 28, 2016 at 10:49 user1050619user1050619 21k89 gold badges251 silver badges430 bronze badges 6
  • It stores a moment instance in curr_date. For me, when I execute this in console, it returns the object correctly. What do you get if you enter curr_date.format("HH:mm") afterwards? – user3297291 Commented Apr 28, 2016 at 10:59
  • I'm in Central Time and it gives the central time instead of EST time – user1050619 Commented Apr 28, 2016 at 11:00
  • The alert statement gives me the EST time but when I print in console it gives me the actual time(CST). – user1050619 Commented Apr 28, 2016 at 11:07
  • this worked...var curr_date = moment.tz(new Date(), "America/New_York");...curr_date.format() – user1050619 Commented Apr 28, 2016 at 11:19
  • 1 the _d object that you are seeing in the console does not reflect the actual value of the date contained in moment. Ignore any properties starting with _. Just pay attention to the result of .format() – Maggie Pint Commented Apr 28, 2016 at 18:46
 |  Show 1 more ment

1 Answer 1

Reset to default 3

I found this post really helpful. Basically, stop using the Date object explicitly.

const moment = require('moment-timezone')
const timeInEST = moment().tz('America/New_York').format('MM/DD/YYYY HH:mm')

本文标签: javascriptGetting EST date in momentjsStack Overflow