0

My client and server are at different time-zones.I am trying to get datetime in javascript but moment function or method toISOString() gives me string output. Even when I recreate the date object it again goes to GMT time. So how can I get date object without timezone issue?

Some lines from my code are as below. All return string and my ngModel is Date type.

this.Element.nativeElement.value = this.Element.nativeElement.value.toISOString();
Hadi GhahremanNezhad
  • 2,377
  • 5
  • 29
  • 58

1 Answers1

0

Date objects in JavaScript contain both date, time and timezone. You have to convert it to something else, and you're using toISOString() for an ISO formatted valued.

If you only want a date value, then you can try using toLocaleDateString() which takes additional options depending upon what you want.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString

const str = new Date().toLocaleDateString('en-US')
console.log(str);
Reactgular
  • 52,335
  • 19
  • 158
  • 208
  • Your first statement is incorrect. `Date` objects in JavaScript only contain a UTC-based timestamp - which is the number of milliseconds since the Unix epoch. In other words, the value returned by `.valueOf()` or `.getTime()`. Everything else it does is computed to/from that. – Matt Johnson-Pint Oct 10 '19 at 18:08