0

When I try to convert a user inputted date such as 05/13/1978 I want to send the date in ISO format to the server as that is what the back end accepts.

new Date('05/13/1978);

Outputs:

Sat May 13 1978 00:00:00 GMT+0100 (British Summer Time)

Which is expected but to convert to ISO date for the POST to the server I need to call .toISOString() which outputs:

1978-05-12T23:00:00.000Z

Why does the user input revert to the 12th May 1978 at 23:00?

If that is stored in the database when a user GET a response with that value will it output 05/13/1978 or 05/12/1978 in the browser?

RyanP13
  • 7,413
  • 27
  • 96
  • 166
  • 1
    `05/13/1978` is a non-standard date format the handling will be implementation dependant and might not work as expected. You should either use the explicit constructor passing in the different components of the date as numbers or use a standard compliant string which is a variant of ISO. Check [the `Date` constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date#Syntax) and [parsing syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse#Date_Time_String_Format) – VLAZ Oct 14 '20 at 10:17
  • 1
    Also, watch out for timezone changes. That's exactly what you see in your case - the string is assumed to be in local timezone but `toISOString` returns it in UTC which will adjust the date according to the timezone offset. If you're ahead, then the clock goes *back*, and thus you go from `00:00+01:00` to `23:00Z` on the previous day. – VLAZ Oct 14 '20 at 10:19
  • 1
    The clue is actually in your outputs section. The time includes 1 hour for BST. Without that, the actually GMT/UTC/Z time is 11pm the day before. – ATD Oct 14 '20 at 10:20

0 Answers0