Brantino
Brantino

Reputation: 554

Web API Date Only UTC conversion

I am working on a project that has an ASP.NET Web API service accepting a DateTime value from a Angular client using the AngularStrap date picker. We have a field that represents the date on which something happened, and for this field the time component is not important. The client is sending the Date picker value as UTC with a time component relative to the user's time zone, but when storing the date we are storing it as UTC without time since time is not relevant, like 2014-05-08 00:00:00.000Z.

When the client retrieves the date, the server sends it back as UTC again with a time value of all zeros. However, the client is converting this from UTC back to the local time zone causing the date that is displayed to the user to get shifted one day into the future.

How can I get around the UTC conversion when I really don't care around the time? Should I return a pre-converted date value from the server relative to the user's time zone? Is there someway to ignore the time component all together?

Upvotes: 3

Views: 1736

Answers (1)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241475

Answering in general, since it's difficult to tell anything specific from what you described so far.

  • A date without a time is not a good candidate for the JavaScript Date object, or for the .Net DateTime object.

  • Since neither languages natively offer a true date-only data type, you might consider just keeping it as a string in ISO-8601 format ("YYYY-MM-DD")

  • If you need to work with date-only values in .Net, consider using the LocalDate type from Noda Time.

  • Whenever you use a date+time component to represent a date-only value by setting the time to all zeros, you are fixing that time to midnight.

    • For local time zones, this is problematic because in some parts of the world, midnight does not exist on the day of the spring-forward daylight saving time transition. (Example: Brazil)

    • UTC isn't a great choice either, because midnight UTC falls on the prior day in most time zones in the Americas.

  • Quite often, a date without a time refers to either the entire day, or to the end of the day. Using zeros would place it at the start of the day. Consider that 2014-01-01 to 2014-01-02 is two days, or 48 hours. But 2014-01-01T00:00:00 to 2014-01-02T00:00:00 spans only 24 hours. This is the cause of many bugs.

If you'd care to edit your question to provide specific code demonstrating the problem, I may be able to offer more specific advice.

Upvotes: 1

Related Questions