.NET Save DateTime and completely ignore timezone

Maybe the answer is so obvious, I donโ€™t see it, but I have a question that I will put at risk in any case.

I want the users of the .NET web application to enter the date / time, save it in the Oracle database and no matter what time zone they are in, it is always displayed as the original, original version typed by the User. Therefore, if one user in California enters 2PM and the other in Maryland enters 2PM, they will both show as 2PM to a user in Japan. Two types of clients are possible: a web user and a Windows client user (connected through a web service).

Think of it as if I want to completely break all the time zones that most applications worry about.

I do not want to save as a string. I want a datetime so that I can add / remove hours and minutes.

Edit:

This was basically the exact problem I ran into.

+4
source share
3 answers

You should always store DateTime in UTC (universal) format. When you display it, you can choose which timezone you want, in your case it can be fixed for all users, not based on location.

 // when recording date time DateTime utcDateTime = DateTime.UtcNow; // parse DateTime back out from string DateTime utcDateTime = DateTime.SpecifyKind(DateTime.Parse(dateStr), DateTimeKind.Utc); // localized DateTime DateTime localDate = utcDateTime.ToLocalTime(); // fixed DateTime based on timezone string timeZoneKey = "New Zealand Standard Time"; TimeZoneInfo timeZone = TimeZoneInfo.FindSystemTimeZoneById(timeZoneKey); DateTime nzlocalDate = TimeZoneInfo.ConvertTimeFromUtc(utcDateTime, timeZone); 

This takes into account such things as saving daylight, which can overturn you if you start saving localized dates.

+9
source

If you just save the DateTime as entered / selected by the user, then it will be saved that way. It will store information about time zones, but you control what to do with it.

For example, when you want to display it on a screen / file, etc., you need to format it to a line. If you use this ToString overload with CultureInfo.InvariantCulture , then this should ignore the current culture and display the date as it is:

 DateTime date = DateTime.Now; string output = date.ToString("d", CultureInfo.InvariantCulture); 

Other operations will require different processing, but you will need to indicate what happens in each case.

0
source

I had a time-dependent fix, and SpecifyKind did not allow me to make the correct TimeSpan comparisons. In my situation, the quickest solution was to simply remove the hours, minutes and seconds from that time, since the DateTime that I compared with was at midnight (00:00:00),

 DateTime eventDateTime = (DateTime)row["event_date"]; eventDateTime = eventDateTime.AddHours(-1 * eventDateTime.Hour).AddMinutes(-1 * eventDateTime.Minute).AddSeconds(-1 * eventDateTime.Second); 
0
source

All Articles