0

I'm saving a datetime like this in Backbone.js:

    this.save('GameEndTime', new Date().toJSON()).done(function() {
        self.score.fetch();
    });

new Date().toJSON() is UTC, but when it hits my controller, it's somehow been converted to my local timezone.

I can see it right away when stepping through in Visual Studio.

Here is my controller, and I when I inspect the 'dto' parameter it's already in my local timezone.

    [HttpPut("{id}")]
    public virtual IActionResult Put(TId id, [FromBody] TDto dto)

    { ... }

So I'm unsure where/how it's going from UTC to local.

For example, if I write out the time in console.log in my browser and get this:

GameEndTime: "2018-10-18T19:36:46.982Z"

When it hit's my controller, it's this:

GameEndTime = {10/18/2018 3:36:46 PM}

This is when I set a breakpoint on the entrance to the api controller. So I can't figure out where it's being converted.

Has anyone ever had an issue like this?

Thanks!

SkyeBoniwell
  • 6,345
  • 12
  • 81
  • 185
  • How are you viewing the data when it hits your controller? Have you set a watch on it and viewed the actual UTC time property? – maccettura Oct 18 '18 at 20:15
  • I set a breakpoint in my controller in Visual Studio – SkyeBoniwell Oct 18 '18 at 20:16
  • 1
    The "Z" at the end of the time string specifies UTC, so when it is being converted back into a datetime in the controller, it takes that into account. If you use `GameEndTime.ToUniversalTime()` you should get the right value. – Ron Beyer Oct 18 '18 at 20:18
  • Do you have a `DateTime` or `DateTimeOffset` property somewhere? What is `TDto`? – gunr2171 Oct 18 '18 at 20:18
  • A `DateTime` is a measurement of ticks from an epoch. Your local timezone (eastern I imagine?) would display those ticks as the value you see. `DateTime` has a `.ToUniversalTime()` method. Check that to see if it matches, I bet it will – maccettura Oct 18 '18 at 20:18
  • @maccettura I'm trying to save the UTC format in my database. So can I just convert it using .ToUniversalTime()? Thanks – SkyeBoniwell Oct 18 '18 at 20:22
  • Possible duplicate of [How to make ASP.Net MVC model binder treat incoming date as UTC?](https://stackoverflow.com/questions/10293440/how-to-make-asp-net-mvc-model-binder-treat-incoming-date-as-utc) – mjwills Oct 18 '18 at 20:27

0 Answers0