1

I am trying to read date from angular control and passing it my c# web api controller.

But somehow the javascript is converting it to UTC date, so when I receive the date in my api controller it is in UTC while I am passing the form values as it in post method

getAssetActivities(form: NgForm) {
    const formValue: AssetActivitySearchModel = form.value;
    console.log(formValue);
    console.log(formValue.fromDate.toISOString());
    console.log(formValue.toDate.toISOString());
    var response =  this._request.post<AssetActivitiesResponse>(`api/assets/${this.machineData.assetId}/getAssetActivities`,formValue)
    .subscribe((data: any) => {
       console.log(data);
       this.machineData.activities = data.activities;
    });
}

I checked that the date I received from the control is correct but when it send it is somehow getting converted to UTC.

I checked the ToISOString and found that ISOString also converts the date to UTC.

This is the log of actual dates and ISO date, the same ISO dates are receive in my C# code

enter image description here

4
  • 1
    Why send a utc date to the api is a problem? Commented Jan 25, 2021 at 8:57
  • ECMAScript Dates don't have a timezone, they're just an offset in ms from 1 Jan 1970. toISOString is specified as producing a UTC timestamp, so yes, it's UTC. Commented Jan 25, 2021 at 9:00
  • ISO timezone is the same as UTC. Commented Jan 25, 2021 at 9:05
  • ISO isn't a timezone, it's a standards body. ISO 8601 is an ISO standard that specifies time and date formats. UTC isn't a timezone either, it's a time standard that timezone offsets are measured from. Commented Jan 25, 2021 at 9:13

1 Answer 1

1

When you console.log the formValue.fromDate.toISOString() that does not actually change the formValue.fromDate. It just prints the date as an ISO string. You still post the UTC date when you post the form.

A way to do this would be to add the ISO strings in the form object before post. There is work to do so that typescript will not complain, but the idea is this:

formValue["fromISOdateString"] = formValue.fromDate.toISOString();
Sign up to request clarification or add additional context in comments.

Comments

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.