0

I don't know why, when you enter a date, it give me back the same date but with one day less.

service.ts

  protected convertDateFromClient(project: IProject): IProject {
    const copy: IProject = Object.assign({}, project, {
      entryDate: project.entryDate != null ? moment(project.entryDate, 'DD/MM/YYYY').format('YYYY-MM-DD') : null
    });
    return copy;
  }

Project.java

...
@Column(name = "entry_date")
    private LocalDate entryDate;

public LocalDate getEntryDate() {
        return entryDate;
    }

    public Project entryDate(LocalDate entryDate) {
        this.entryDate = entryDate;
        return this;
    }

    public void setEntryDate(LocalDate entryDate) {
        this.entryDate = entryDate;
    }

The date entered: 12/2/2020 and return: 11/2/2020

Update: If I put the date 12/02/2020 (dd/MM/yyyy) by keyboard in the datePicker´s input, i recived from POST this 01/12/2020 (MM/dd/yyyy)

Any suggestions???

edoman
  • 11
  • 1
  • 4

2 Answers2

0

You might be having a problem with time zones, remember that in JavaScript Date class represent a timestamp, is the number of miliseconds since January 1, 1970 00:00 UTC.

Mozilla Javascript Doc - Date

Take a look at this post for an interesting discussion, (Is the Javascript date object always one day off?)[Is the Javascript date object always one day off?

I have been using one of this methods to solve this issue,

export function dateFromModel(d: Date): Date {
  const doo = new Date(d);
  return new Date(doo.getTime() + Math.abs(doo.getTimezoneOffset() * 60000));
}

UPDATE: I think you now have the problem I mention, plus you now are switching month and days when using moment. Just print the date before using moment and after, to check this. You can also use toISOString() to check what are you going to receive in the backend, this JavaScript function complies to ISO 8601 just like Java LocalDate.

cabesuon
  • 4,860
  • 2
  • 15
  • 24
0

I agree that this problem is related to time zones. And code below works for me

calculateWithoutTimeOffset(date: any): Date {
  var userTimezoneOffset = date.getTimezoneOffset() * 60000;
  return new Date(date.getTime() - userTimezoneOffset);
}