-1

From the server I get '2019-01-19T19:11:00.000Z' I need to convert to local timezone, so that I end up with '2019-01-19T11:11:00'. My UTC offset is 8 hrs.

new Date('2019-01-19T19:11:00.000Z') produces Sat Jan 19 2019 11:11:00 GMT-0800 (Pacific Standard Time), how do I get it back to '2019-01-19T11:11:00'? Thanks

dt1000
  • 3,374
  • 6
  • 41
  • 62

2 Answers2

2

You want the date string in iso format, respecting the local time zone:

const tzoffset = (new Date()).getTimezoneOffset() * 60000;

const d = new Date('2019-01-19T19:11:00.000Z')

console.log(new Date(d - tzoffset).toISOString().split('.')[0])

console.log('2019-01-19T11:11:00')
ic3b3rg
  • 13,584
  • 4
  • 23
  • 49
  • `var d = new Date(); d.setMinutes(d.getMinutes() - d.getTimezoneOffset()); d.toISOString().slice(0,19);`. ;-) – RobG Dec 21 '18 at 04:52
0

var now = new Date();
console.log(now.toISOString().split('.')[0]);
Jorn
  • 26
  • 3
  • Not cool. This generates a UTC timestamp that masquerades as local, it doesn't do what the OP wants. – RobG Dec 21 '18 at 04:48