kekeh / angular-mydatepicker

Angular datepicker and date range picker :date:
https://kekeh.github.io/angular-mydatepicker/
MIT License
1 stars 11 forks source link

jsDateToMyDate method receive string type but expect JS Date type #30

Closed pin8marian closed 4 years ago

pin8marian commented 4 years ago

Hi

I have a small issue regarding the writeValue method. I have the following scenario: 1. I get the value angular-mydatepicker field and save in database. I save exactly what is in the field into JSON format. This value, for instance: {....., "dateAdded":"Custom","customDateRange":{"isRange":true,"singleDate":null,"dateRange":{"beginDate":{"year":2020,"month":2,"day":3},"beginJsDate":"2020-02-02T22:00:00.000Z","beginEpoc":1580680800,"endDate":{"year":2020,"month":2,"day":29},"endJsDate":"2020-02-28T22:00:00.000Z","endEpoc":1582927200,"formatted":"03-Feb-2020-29-Feb-2020"}},.....}

those "....." represents other fields that can be ignored 2 The user have a drop down with all the custom dateRange saved in DB and he can pick any value and fill the existing angular-mydatepicker field from UI with the date.

Now, here is where is issue: the value cannot be written into the field because (from what I saw), the writeValue method takes the beginDate and endDate using jsDateToMyDate method, by sending the beginJsDate/endDate as string but inside jsDateToMyDate is called getFullYear() of the parameter, which is string. I guess it must be date to use getFullYear() I hope I made myself understood. Sorry if I repeat the same word multiple time into one the phrase,

Thank you M

kekeh commented 4 years ago

It happens because you pass string to the beginJsDate and the endJsDate properties. It should be javascript date object.

It was my mistake. I made minor fix to the writeValue() method. You don't need to change format of your DB string just install new version 0.4.2 of the component from npm.

kzimny commented 2 years ago

It still doesn't work in version 0.11.5. I get the date from database as JSON in format "2026-03-01T00:00:00". I need to create new date:

if (g.MyNextDate) {
  const dateModel: IMyDateModel = {
      isRange: false,
      singleDate: {
          jsDate: new Date(g.MyNextDate)
      },
      dateRange: undefined
  };
  this.formGroup.controls[this.fields.naechsterTermin].setValue(dateModel);
}

And jsDate doesn't accept null, but only Date | undefined. image

kekeh commented 2 years ago

Hi @kzimny Try to set the model to null in case there is not initial date. And remove dateRange: undefined from the code.

if (g.MyNextDate) {
  const dateModel: IMyDateModel = {
      isRange: false,
      singleDate: {
          jsDate: new Date(g.MyNextDate)
      }
  };
  this.formGroup.controls[this.fields.naechsterTermin].setValue(dateModel);
}
else {
  // no initial date
  this.formGroup.controls[this.fields.naechsterTermin].setValue(null);
}
kzimny commented 2 years ago

Yes, this is the workaround. I suggested to change the source code of angular-mydatepicker to handle/accept null value.