2

I have an Angular front end that sends back JSON to an MVC back end. All dates are converted in JS, using moment() to ISO format before being sent i.e. StartDate: 2015-02-17T15:06:12.180Z

For some reason MVC does not treat these as being in UTC and will convert them to local time. One thing I do use though is MVC's ability to automatically parse JSON into POCOs. My method signature is as follows:

[HttpPost]
public ActionResult AddSchedule(Schedule schedule)

How can I make MVC treat dates properly?

2
  • As in the dates you pass back with JSON are already utc, and when parsed into C# they are converted to local server time? Commented Feb 17, 2015 at 15:21
  • I have a similar issue although the dates are shown to be UTC. The issue is when they get saved to the database its the UTC time that's stored. For DOB pickers this is a pain because in summer time a day can be taken off. Commented Jun 14, 2019 at 15:35

1 Answer 1

2

That isn't the behaviour I would expect. Usually when I pass JSON containing dates to c#, they are not changed to local times. Is there code in your Schedule parameter-less constructor that could be affecting the dates?

If not you might have to use the C# ToUniversalTime() function on your dates after they are passed into your controller.

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

1 Comment

I have no constructors affecting it. I am now using the ToUniversalTime function in order to convert them back.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.