简体   繁体   中英

c# handling cross country DateTime timezone

I am developing one application for dietitian. In this application dietitian can set diet for there customers anywhere in the world. Right now I am facing one issue about MealTime set by dietitian. Let's consider following example.

If dietitian is in India and set Breakfast meal time to 28/04/2021 09:00:00 AM (IST). I store this value to sql database in GMT format( 28/04/2021 03:30:00 AM ). The customer is in Dubai then he is seeing Breakfast time to 28/04/2021 07:30:00 AM instead of 09:00:00 AM .

what is the best way to handle this scenario?

If you instead use DateTimeOffset to both store the time in the database and in your business domain logic, then the timezone information is available whenever you need it without having to join on a master reference to identify the correct timezone.

In most application designs that involve queries and comparisons across multiple timezones DateTimeOffset can significantly simplify querying and reduce lookup logic to resolve the correct timezone. The major caveat is that your UI layer should pass through the original client data timezone or your data ingestion should sanitise or convert it when the data is written.

In both SQL and C# it is trivial to convert the values to different timezones when you need to, and these values will natively sort and filter correctly without having to cast them to specific zones.

Since SQL Server 2008 DateTimeOffset has been the guidance on for how to manage application data that spans across multiple timezones, in 2021 it should not be your first option any more.

As I understand your question, you can save date time directly.

Eg. If you save 28/04/2021 09:00:00 AM in database, your customers can see the same time no matter where he/she is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM