0

i am struggling with my code, new Date() convert my value to next day 1 hour

new Date('2013-03-27T23:59:59.999Z') // => Thu Mar 28 2013 00:59:59 GMT+0100 

As a Solution:

new Date('2013-03-27T23:59:59.999Z'.replace(/-/g, '\/').replace(/T.+/, '')) // => Wed Mar 27 2013 00:00:00 GMT+0100

Any suggestions how to get the correct date?

User1979
  • 817
  • 3
  • 13
  • 23
  • 1
    Don't you want to use date functions for that rather than regex: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date Such as setTime(getTime + 1day + 1hour in ms) – PeterS Nov 09 '22 at 15:23
  • 1
    Here is a link I think can help you: https://stackoverflow.com/questions/13762930/how-to-add-24-hours-to-datetime-object-of-javascript – PalesaDev Nov 09 '22 at 15:26
  • @PalesaDev I don't think that's what is being asked. It's not how to add time to a date, it's why does this date appear to jump 1 hour into the next day. – jarmod Nov 09 '22 at 15:28
  • You're passing a UTC date into the Date constructor. What locale/country are you based in? Are you always one hour ahead of UTC or do you have daylight savings like in the UK? – Robin Webb Nov 09 '22 at 15:31
  • 1
    OP is in GMT +1 timezone and this `2013-03-27T23:59:59.999Z'` is a UTC timestamp. Hence the date will be displayed as `Thu Mar 28 2013 00:59:59 GMT+0100` that's the expected behaviour – Konrad Nov 09 '22 at 15:32
  • 1
    @PalesaDev thanks that was very useful, instead of "+" i used "-" – User1979 Nov 09 '22 at 16:48

0 Answers0