0

I'm reading data from a API server:

opTMP:
     { DATAI: "2019-10-27T00:00:00", …}
     { DATAI: "2019-10-31T00:00:00", …}

then I create a new date:

 const opTMP1 = this.opTMP.map(x => Object.assign({}, x));
  for (const op of opTMP1){ 
   let d = new Date(op.DATAI);
   console.log(d);
...
}

but in console I got different results,one is GMT+0300 and one GMT+0200 :

d: Sun Oct 27 2019 00:00:00 GMT+0300 (Eastern European Summer Time)
d: Thu Oct 31 2019 00:00:00 GMT+0200 (Eastern European Standard Time)

because of that I got problems when comparing it,I want to get only day month and year,no time info needed,how can I reset both to the same time or to 0:00:00?

mrapi
  • 5,214
  • 6
  • 27
  • 48
  • 1
    [This post](https://stackoverflow.com/a/50130338/378779) describes how to convert a JS date to YYYY-MM-DD format (i.e. without the time). You can then compare two such dates, e.g. ``if ( ymd1 < ymd2 ) ...``. – kmoser Oct 22 '19 at 05:51
  • 1
    It's daylight savings between these two dates, so the two would be an hour apart. In your case, they are shown (*correctly*) as being in two different time zones - the Summer timezone and the standard one. 1 hour apart – VLAZ Oct 22 '19 at 05:53
  • 1
    Are you sure it don't change from Summer time to standard time between those days? Many places have the change the last Sunday in October – rypskar Oct 22 '19 at 05:56
  • @rypskar One date is in "Eastern European **Summer** Time" the other in "Eastern European **Standard** Time", so - yes, it's daylight savings. – VLAZ Oct 22 '19 at 05:58
  • @kmoser the compraing is one problem,I need as date and to have the same GMT on both cases – mrapi Oct 22 '19 at 06:00
  • @mrapi what timezone are those dates in? If it's local (presumably Eastern European), then there really is no issue. If it's not local, then write them with the timezone they are in or in UTC, so they always have the same base. – VLAZ Oct 22 '19 at 06:01
  • both are coming from a DATE field,there is no timezone info there – mrapi Oct 22 '19 at 06:03
  • So, from which timezone are you *into* the date field? Is it supposed to be local or UTC? – VLAZ Oct 22 '19 at 06:05
  • Work with UTC, closing as dup. – kemicofa ghost Oct 22 '19 at 06:17
  • Possible duplicate of [How do you convert a JavaScript date to UTC?](https://stackoverflow.com/questions/948532/how-do-you-convert-a-javascript-date-to-utc) – kemicofa ghost Oct 22 '19 at 06:17
  • @mrapi Why do you want to convert it to a ``Date`` object to begin with? If you just want to compare which is earlier than the other, compare them as strings. – kmoser Oct 22 '19 at 06:17
  • 1
    So what is the exact problem? 1. `I got problems when comparing it`, 2. `I want to get only day month and year` and 3. `how can I reset both to the same time or to 0:00:00`. – Itang Sanjana Oct 22 '19 at 06:19
  • my mistake.I found the real problem:comparing the days between Thu Oct 31 2019 00:00:00 GMT+0200 (Eastern European Standard Time) and Sun Oct 27 2019 00:00:00 GMT+0300 (Eastern European Summer Time) gives wrong 5 not 4.solved using this https://stackoverflow.com/a/38356944/3492996.thanks to all of yous – mrapi Oct 22 '19 at 07:25

1 Answers1

1

Converting date to epoch time is good way to comparing the dates.

let d = new Date(op.DATAI).getTime();
Pratik Gandhi
  • 1,219
  • 1
  • 16
  • 31