1

I should transform my JS date in this format

2017-05-22T12:31:41.129+0100

which I'm not sure if it's a standard or not, but basically my new Date(); retrive this output:

Wed Aug 02 2017 20:11:36 GMT+0200 (W. Europe Daylight Time)

Is there any function to retrieve this standard (?) or should I concatenate all the results?

LucaP
  • 638
  • 1
  • 12
  • 34
  • Your question isn't exactly clear, you want a function that transforms the result of `new Date()` to the `2017-05-22T12:31:41.129+0100` format? – AGE Aug 02 '17 at 18:18
  • @AGE Yes, that is my goal, I guess I have to implement a function that concatenate all the results of .getFullYear(); etc? – LucaP Aug 02 '17 at 18:20
  • I think that is quite similar to an SQL standard, but there is also a T in the middle and +GMT at the end – LucaP Aug 02 '17 at 18:22
  • That is a non standard format. Closest built in is `Date#toISOString()`. which produces: `"2017-05-22T11:31:41.129Z"` – charlietfl Aug 02 '17 at 18:27
  • @charlietfl what that Z stands for? Should I split for the Z and concat the GMT? – LucaP Aug 02 '17 at 18:30
  • `Z` is for Zulu time (Zero offset) .... GMT timezone – charlietfl Aug 02 '17 at 18:30
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString – charlietfl Aug 02 '17 at 18:33
  • 3
    The question doesn't demonstrate any effort on OP's part. –  Aug 02 '17 at 18:34
  • 1
    @GoingSolo `Z` indicates that the date/time [is in UTC](https://en.wikipedia.org/wiki/ISO_8601#UTC), so if you remove it and add the GMT part, the result will be incorrect (because the date and time won't be adjusted accordingly). Example: in my machine, `new Date().toISOString()` produces `2017-08-02T18:38:52.157Z`, but the local time (in my timezone) is `2017-08-02T15:38:52.157-03:00`, while in Italy it'll be `2017-08-02T20:38:52.157+02:00` –  Aug 02 '17 at 18:40

1 Answers1

-2

If you have a date with new Date(); and you want to recieve this long string just do:

let x = new Date();
console.log(x.toString());

To convert the first string into a date object try the funtion Date.parse(dateString).

If you want to get exactly this first string format, you have to concat all parts of it manually, yes.

Caesar2011
  • 100
  • 5