p-calendar should accept ISO 8601 formatted date strings
Current behavior Currently p-calendar accepts formatted strings or JavaScript date object as input but not ISO 8601 formatted strings. See the example on stackblitz.
Expected behavior
p-calendar should accept ISO 8601 formatted date strings.
If the passed string can be parsed as a date can be checked as follow:
!isNan(Date.parse('2019-11-11T00:00:00Z'))
What is the motivation / use case for changing the behavior? Getting a date from the backend in a ISO 8601 formatted string such as '2019-11-11T00:00:00Z' is a common use case. However, before this value can be passed to the p-calendar, it must be parsed or a date object must be created from it.
I'm submitting a ... (check one with "x")
[ ] bug report => Search github for a similar issue or PR before submitting
[x] feature request => Please check if request is not on the roadmap already https://github.com/primefaces/primeng/wiki/Roadmap
[ ] support request => Please do not submit support request here, instead see http://forum.primefaces.org/viewforum.php?f=35
@riorudo, I believe my PR would handle this too
Scenario 1 When you pass the new Date object.

Scenario 2 When you pass the same date string (ISO/JSON string) from the control.

Whereas, if go to the control source code, there is a "WriteValue" function where it is trying to convert the string to the date object.
we tried various string formats but it is not working and this.value is becoming null for the control.
the current scenario is tested in the PrimeNG hosted Stackblitz environment.
https://stackblitz.com/edit/primeng-calendar-demo?file=src%2Fapp%2Fapp.component.ts
Version:

@cagataycivici you closed @Steven-Harris PR to add this. Is this still the plan to drop string support?
It's not super hard to convert dates I get from my API to Date(), but it would be really nice to pass a string in this format directly to calendar component since it's a pretty common date format.
Same here, it would be nice to bring ISO string date support ... An API will give us an ISO string not a JS Date object ... We have to add new Date() everywhere we use p-calendar ...
Hi,
So sorry for the delayed response! Improvements have been made to many components recently, both in terms of performance and enhancement. Therefore, this improvement may have been developed in another issue ticket without realizing it. You can check this in the documentation. If there is no improvement on this, can you reopen the issue so we can include it in our roadmap? Please don't forget to add your feedback as a comment after reopening the issue. These will be taken into account by us and will contribute to the development of this feature. Thanks a lot for your understanding!
Best Regards,
what about this?
@mertsincan Thanks for such great work regarding Prime Calendar, but the current implementation with Date objects only, force developers to have a list of the boilerplate code and checks added to the project where we need to convert every ISO string passed by the backend to a JS Date in order to get the calendar working. I could give you a couple of arguments why this feature is required:
- The amount of boilerplate code from the programmers' side would be significantly reduced.
- Say we have a very heavy invoice form that has around 10 date pickers or more. When we get the invoice information from the backend and as already mentioned, all the dates that come from BE are in ISO date format.
- We then want to conveniently patch the form in one line
this.formGroup.patchValue(this.invoiceData) - Because of the current behaviour, we need to programatically patch every control with the date instance value. And suddenly instead of one line of code, we have 1 + n, when n is the number of controls that need to be converted from ISO string to Date.
this.formGroup.patchValue(this.invoiceData);
this.formGroup.controls.startDate.patchValue(new Date(this.invoiceData.startDate));
this.formGroup.controls.endDate.patchValue(new Date(this.invoiceData.endDate));
this.formGroup.controls.birthDate.patchValue(new Date(this.invoiceData.birthDate));
...
- Every time when we execute patchValue, Angular forms calls updateValueAndValidity() method, so as a result we have more checks.
- In the context of Angular's 16 Typed forms, FormGroup models would reflect only the necessary type.
- Instead of declaring
new FormGroup({startDate: ''})and to infer the type as string, we need to explicitly specifynew FormGroup({startDate: new FormControl<Date | string>('') }), otherwise we won't be able to patch the valuethis.formGroup.controls.startDate.patchValue(new Date(this.invoiceData.startDate));
- And because we are converting iso string to Date, having optional controls with null values will need yet another check:
// suppose we have an optional nullable endDate:
new FormGroup({
startDate: new FormControl<Date | string>('', Validators.required),
endDate: new FormControl<Date | string | null>(null),
}
// and we again, patch the values:
this.formGroup.patchValue(this.invoiceData);
this.formGroup.controls.startDate.patchValue(new Date(this.invoiceData.startDate));
if (this.invoiceData.endDate) { // Check for the value not to be null
this.formGroup.controls.endDate.patchValue(new Date(this.invoiceData.endDate)); // new Date(null) will convert to Jan 01 1970
}
this.formGroup.controls.birthDate.patchValue(new Date(this.invoiceData.birthDate));
...
So as you can see there are a list of disadvantages when everything could be resolved by allowing a valid ISO string as control value.
It would be really beneficial to support this feature.