1

I am using PrimeNg calendar module for picking up a date. I have the following code :

 <p-calendar formControlName="valid_till" [dateFormat]="'mm/dd/yy'"></p-calendar>

Now when I select a date for eg: 31st July 2020, in the component, if I log the value I am getting the following :

Fri Jul 31 2020 00:00:00 GMT+0530 (India Standard Time)

But when I pass this value to the server as follows :

this.myService
    .save(this.myForm.value, this.editId);

It is passing the date as valid_till: "2020-07-30T18:30:00.000Z" and this value is saved in the database as 2020-07-30 23:59:59. But I have to save it as 2020-07-31 23:59:59. why it is happening like this and how can I save the exact date that I am selected ?

I have tried to add the offset as seen in another post like

newDate =  new Date(date_valid_till.getTime() +date_valid_till.getTimezoneOffset() * 60000);

But this has again given me Thu Jul 30 2020 18:29:59 GMT+0530 (India Standard Time), still not the date I would like to have. I only need Thu Jul 31 which I have selected in the date picker.

PS : I am using MySQL database and the column is of type DateTime.

4
  • here is the issue of timezone. Commented Jul 29, 2020 at 4:35
  • How this can be fixed ? I have tried to add the offset like newDate = new Date(date_valid_till.getTime() + date_valid_till.getTimezoneOffset() * 60000); but it has given me Thu Jul 30 2020 18:29:59 GMT+0530 (India Standard Time) and it is still one day off Commented Jul 29, 2020 at 4:37
  • use the moment to set the timezone Commented Jul 29, 2020 at 4:39
  • But as the form field is datepicker, if i set using moment, it is showing some error when I use pathvalue. It should be a date object Commented Jul 29, 2020 at 4:40

1 Answer 1

1

Use dataType as string.

<p-calendar formControlName="valid_till" dataType="string"></p-calendar>

I have created the stackblitz, its working

https://stackblitz.com/edit/angular-z5ifcn

Sign up to request clarification or add additional context in comments.

2 Comments

When I use it , I am getting core.js:6228 ERROR Unexpected literal at position 2
Yes it is. You are a lifesaver. Actually I was using moment and all even after changing the dataType. thanks a lot !!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.