0

The code:

console.log(start)

reads, Thu Mar 01 2018 00:00:00 GMT+0530 (India Standard Time)

I want it a new object start_NY which will read Thu Mar 01 2018 00:00:00 w.r.t. America/New_York timezone. Something like Thu Mar 01 2018 00:00:00 GMT-0500.

I used a script:

start_ny = new Date('Thu Mar 01 2018 00:00:00 GMT-0500');

But that reads, Thu Mar 01 2018 10:30:00 GMT+0530 (India Standard Time), which is actually converting the date into Indian Standard Time, instead of giving me a time from New_York timezone.

The format should be same as that of the existing one. How can I do that?

Saswat
  • 12,320
  • 16
  • 77
  • 156
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString – Fida Mar 11 '18 at 15:15
  • Possible duplicate of [How to initialize javascript date to a particular timezone](https://stackoverflow.com/questions/15141762/how-to-initialize-javascript-date-to-a-particular-timezone) – Feras Al Sous Mar 11 '18 at 15:16
  • check [moment library](http://momentjs.com/timezone/) you can define it as the following: `var newYork = moment.tz("2014-06-01 12:00", "America/New_York");` `newYork.format();` – Karim Mar 11 '18 at 15:19
  • @karim it returns 2018-03-01T00:00:00Z, which is totally different from the format I want. Check the question minutely. – Saswat Mar 11 '18 at 15:32
  • Don't use the built-in parser, [it's notoriously unreliable](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results). A library will help greatly. – RobG Mar 11 '18 at 22:46

1 Answers1

1

Try with this following code and MDN resources

new Date().toLocaleString('en-US', { timeZone: 'America/New_York' })

// Date Format

new Date().toDateString('en-US', { timeZone: 'America/New_York' });
Momin
  • 3,200
  • 3
  • 30
  • 48
  • This returns `3/11/2018, 11:33:58 AM` which is not exactly the format I want. – Saswat Mar 11 '18 at 15:34
  • @Saswat—if you want to format a date, then see [*Where can I find documentation on formatting a date in JavaScript?*](https://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript) – RobG Mar 11 '18 at 22:47