0

I want to get current date UTC midnight:

var d = new Date();
d.setUTCHours(0,0,0,0);
console.log(d.toISOString())

This returns me:

2016-04-21T00:00:00.000Z

Is this UTC time? I think it is not and it should be:

2016-04-20T22:00:00.000Z
1110
  • 7,829
  • 55
  • 176
  • 334
  • 2
    00:00 is midnight, 22:00 is 10 pm. Why are you expecting the latter? – RickyTomatoes Apr 21 '16 at 20:56
  • Take a look at http://stackoverflow.com/questions/9756120/how-do-i-get-a-utc-timestamp-in-javascript Will give you more insight in this area. – William Troup Apr 21 '16 at 20:56
  • Date handling in JS is basically evil in my opinion. Once you understand what is going on I recommend you checkout moment.js – aray12 Apr 21 '16 at 21:03
  • @aray12—javascript (ECMAScript) Dates are very simple, they're just a time value. However, many have trouble understanding how time and dates work in general. A library can't help that. E.g. the OP wants "*…current date UTC midnight*", which I think of as the end of the day, but the OP seems to want the start of the day. – RobG Apr 21 '16 at 21:13
  • @RobG I agree he/she should first understand native JS dates, which is why I recommended he not move on to moment.js without figuring out what is going on. That said JS date and specifically timezone handling can be painful. – aray12 Apr 21 '16 at 21:23

1 Answers1

0

If you want to have midnight of your time zone in UTC time then the code is as following:

var d = new Date();
d.setHours(0,0,0,0);
console.log(d.toISOString());
Stefan Berger
  • 423
  • 6
  • 11