0

I am working on project uses angular4(frontend) and java(backend). I get the date in below format from java backend server into angular server.

2018-05-23T18:30:00.000+0000

I need to convert it into javascript/angular Date object. I have tried below code

Date d = new Date(java_date);

but this gives Invalid Date error.

Any idea how to deal with above date format.

Prasad Parab
  • 437
  • 1
  • 7
  • 26
  • That string is definitely a valid date, so the variable `java_date` must have some other string (or something else) in it. – Pointy Sep 29 '18 at 12:49
  • 1
    Make sure you passing the date as `"2018-05-23T18:30:00.000+0000"` ,i.e., as string – Yashwardhan Pauranik Sep 29 '18 at 12:51
  • 1
    Also it should be `let d = new Date(str)` not `Date d`. – Pointy Sep 29 '18 at 12:59
  • That Java backend should be changed to return the result of `Instant.now().toString()` to get a standard `Z` on the end rather than `+0000`. – Basil Bourque Sep 29 '18 at 15:51
  • Related: [What are valid Date Time Strings in JavaScript?](https://stackoverflow.com/questions/51715259/what-are-valid-date-time-strings-in-javascript/) – str Sep 29 '18 at 18:44

2 Answers2

2

The string "2018-05-23T18:30:00.000+0000" is not consistent with the format in ECMA-262, it's missing colon in the timezone offset between the hours and minutes, so implementations may treat it as invalid (e.g. Safari).

You have a number of options:

  1. Replace the timezone offset with "Z" and use the built–in parser: new Date('2018-05-23T18:30:00.000Z')
  2. Insert a colon in the offset and use the built–in parser: new Date('2018-05-23T18:30:00.000+00:00')
  3. Write your own parser for this particular format (maybe 4 lines of code)
  4. Use a library (there are many good ones and they can help with formatting too)

I'd recommend either 3 or 4 as the built–in parser is notoriously fickle, but any of the above will likely do.

RobG
  • 142,382
  • 31
  • 172
  • 209
0

The Date as you wrote it - given by its ISO string, you should parse it using JS/Angular

var a = Date.parse("2018-05-23T18:30:00.000+0000");

Here's a relevant link to MDN :

MDN Parse

MDN ISO

Daniel
  • 257
  • 1
  • 7
  • I face issue due to last +0000 if I removed it then everything works perfect, but I don't want to parse it using string manipulation. – Prasad Parab Sep 29 '18 at 13:07
  • Date constructor (when passed a string) calls Date.parse. You answer does not solve anything. – Salman A Sep 29 '18 at 13:07
  • 1
    @PrasadParab What browser are you using? Seems to work fine in Chrome – user184994 Sep 29 '18 at 13:08
  • @user184994 correct it works in Chrome but I have tried it on Safari12.0 there its not working – Prasad Parab Sep 29 '18 at 13:12
  • In that case, sounds like you have no choice but to manipulate the string value (maybe something like `java_date.substring(0, 23)`) – user184994 Sep 29 '18 at 13:15
  • The string is not consistent with ECMA-262, do not recommend parsing it with the built–in parser as parsing is implementation dependent. – RobG Sep 29 '18 at 13:36
  • @user184994—you should manually parse the string. Either modify the string to be consistent with ECMA-262 (e.g. replace "+0000" with "Z" or "+00:00") or parse it with a custom function or library. – RobG Sep 29 '18 at 13:38