-1

I wanna change the input date with time and time zone to this format yyyy-MM-dd+timezone

Example: I have this input

Sat Oct 05 2019 00:00:00 GMT+0200 (Ora legale dell’Europa centrale)

I want an output like this

2019-10-05+2:00

Is there a simple way to convert this?

R. Richards
  • 24,603
  • 10
  • 64
  • 64
travis_911
  • 301
  • 7
  • 19
  • Have you used [moment.js](https://momentjs.com/) ? This is a great tool to convert dates and time – Ricardo Gonzalez Oct 02 '19 at 13:50
  • @MykWillis I closed the question when the Angular tag was present. I thought it was related to Angular. I'm re-opening it ! –  Oct 02 '19 at 14:02
  • Well I can't re-open it, i can just vote to re-open it ... Either you guys vote too, or simply create a new question (And **DO NOT** use the Angular tag) –  Oct 02 '19 at 14:03
  • Someone's got a trigger-happy finger! ;-) I had an answer in the making and I voted to reopen. – Constantin Groß Oct 02 '19 at 14:04
  • 1
    @ConstantinGroß you don't say ! I'm the gate-keeper of the Angular board and trust me, there's a lot to do ... But hey, maybe that answered him ! –  Oct 02 '19 at 14:05
  • As with most things date and time related, the "simple" way is almost certainly going to end up being (1) not so simple, and (2) wrong in some situations of time and place. Use moment.js: > var moment = require('moment'); > moment.parseZone(`Sat Oct 05 2019 00:00:00 GMT+0200`).format("YYYY-MM-DDZ") '2019-10-05+02:00' – Myk Willis Oct 02 '19 at 14:07
  • `https://github.com/akshaybande81/date-conversion-javascript/wiki` use this link – Akshay Bande Oct 02 '19 at 14:09

1 Answers1

0

You can do this with simple Date functions.

public transform(date: Date): string {
    return `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate() >= 10 ? date.getDate() : '0'+date.getDate()}${date.getTimezoneOffset()}`;
}
Norbert Bartko
  • 2,468
  • 1
  • 17
  • 36