0

I have trouble with converting my date format into a good one that can be sent to my database. At default I have the current day set in my dateTimePicker at 08:00 am. When I want to send it it's format is like this "2020-12-01T07:00:00.812Z".What I try to achieve is "2020-12-01 08-00" so not only the format is bad but it's an hour before the chosen time. I tried converting with moment.js but nothing happens. Here's the code I tried:

// form where I can pick the date //
ngOnInit() {
    this.data= this.formBuilder.group({
      id: [],
      dateFrom: [this.setTime(), Validators.required],
      dateTo: [this.getNextWeek(), Validators.required],
      status: [-1],
    });
  }

// function where I try to convert the date to a useable form //

dataModify(object) {
    let formattedObject = {
      id: "",
      dateFrom: object.dateFrom,
      dateTo: object.dateTo,
      status: object.status,
    };

    moment.utc(object.dateFrom).format('YYYY-MM-DD HH:mm');
    moment.utc(object.dateTo).format('YYYY-MM-DD HH:mm');

return formattedObject;
}
Kerk
  • 283
  • 1
  • 4
  • 24
  • Does this answer your question? [How do I format a date as ISO 8601 in moment.js?](https://stackoverflow.com/questions/25725019/how-do-i-format-a-date-as-iso-8601-in-moment-js) – Liam Dec 01 '20 at 11:25
  • Worth noting that `2020-12-01T07:00:00.812Z` is a UTC date so the timezone will be shifted depending on what timezone your browser is in, so a CET will be one hour ahead of UTC – Liam Dec 01 '20 at 11:27

2 Answers2

0

Generally speaking you want to save your times in the database as UTC, then convert where required (typically the client).

I'm guessing from your question that you're in GMT+1. You could do the conversion manually, or add moment-timezone:

https://www.npmjs.com/package/moment-timezone

Then you convert like so:

// Change 'Europe/Madrid' to whatever timezone  you need
moment(object.dateFrom).tz('Europe/Madrid').format('YYYY-MM-DD HH:mm'))

The nice thing is that if ever you end up having to serve multiple timezones, you can store all the times in UTC then convert where necessary

Michael Beeson
  • 2,840
  • 2
  • 17
  • 25
  • `2020-12-01T07:00:00.812Z` is UTC. Thats what the `Z` means – Liam Dec 01 '20 at 11:32
  • its like its doing nothing at all, same format – Kerk Dec 01 '20 at 11:49
  • I know it's UTC. I think Kerk's whole issue is converting it to local time in a format he likes. As for why it's not working kerk, are you importing 'moment-timezone' and not just 'moment'? Otherwise the timezone elements won't work – Michael Beeson Dec 01 '20 at 13:07
  • yes everything is imported the timezone is good now but the format is still the same and I don't know why – Kerk Dec 01 '20 at 13:15
0

Sorry but there's no need of momentjs to format date. You can use this.

formatDate = (date) => {
    const _date = new Date(date);
    const day = _date.getDate();
    const month = _date.getMonth() + 1;
    const year = _date.getFullYear();
    return `${year}-${month}-${day}`;
}

formatTime = (date) => {
    const _date = new Date(date);
    const hours = _date.getHours()
    const minutes = _date.getMinutes();
    const seconds = _date.getSeconds();
    return `${hours}:${minutes}:${seconds}`;
}

toDateTimestamp = (date) => {
    const dateStamp = this.formatDate(date);
    const timeStamp = this.formatTime(date);
    return `${dateStamp} ${timeStamp}`
}
Srinath Kamath
  • 542
  • 1
  • 6
  • 17