0

I have datepicker and want to send a date to my rest service. While debug I see a date and it is valid.

object

But when I do .toJSON() to send it to service, Angular converts date to UTC and send it like this:

htttp request

How to send valid date?

Capitan Planet
  • 155
  • 3
  • 14
  • Maybe this helps: https://stackoverflow.com/questions/31096130/how-to-json-stringify-a-javascript-date-and-preserve-timezone – Marvin May 21 '18 at 10:26
  • Answered here: https://stackoverflow.com/questions/1486476/json-stringify-changes-time-of-date-because-of-utc – matramos May 21 '18 at 10:31
  • It is the same date (just represented in a different time zone), isn't it? – Henry May 21 '18 at 10:37
  • Henry. Yes - this is the date, but function converted it to UTC, so in my case date changed -1 day (-2 hours to be precise)... – Capitan Planet May 21 '18 at 10:44

2 Answers2

0

Done!

Transform on datepipe did the trick

let dp = new DatePipe('en-EN' );
dp.transform(this.MyDate, 'yyyy-MM-dd'))
Capitan Planet
  • 155
  • 3
  • 14
0

Before sending date to rest service use:

var variableToSendToRestService = yourDateVariable.toLocaleString();

This will convert 'yourDateVariable' (which is date object) object to string using your local settings.

For more please refer:

toLocaleString() reference for Date

Sagar
  • 1
  • 2