Rake36
Rake36

Reputation: 1022

.NET Save DateTime and completely ignore timezone

Maybe the answer is so obvious, I'm not seeing it, but I have a question that I'll risk asking anyway.

I want to allow users of a .NET Web application to enter a date/time, store it in an Oracle database, and no matter which timezone they are in, it always displays as the raw, original version as typed in by the user. So if one user in California enters 2PM and another in Maryland enters 2PM, they would both show as 2PM to a user in Japan. Two types of clients are possible, a web user and a windows client user (connected via web service).

Think of it like I want to completely break all timezone smarts that most applications worry about.

I don't want to save as a string though. I want a datetime that I can add/remove hours and minutes from.

Edit:

This was basically the exact problem I was having.

Upvotes: 9

Views: 29410

Answers (5)

TreeAndLeaf
TreeAndLeaf

Reputation: 1273

If you also need to remove the time component, this works for me:

DateTime WithoutTimeZone(DateTime dateTime)
{
    var dateOnly = DateOnly.FromDateTime(dateTime);
    return new DateTime(dateOnly, TimeOnly.MinValue, DateTimeKind.Utc);
}

Upvotes: 1

adospace
adospace

Reputation: 2019

If you really want discard time zone:

    public static DateTime WithoutTimeZone(this DateTime dateTime)
    {
        if (dateTime.Kind != DateTimeKind.Unspecified)
            return new DateTime(dateTime.Ticks, DateTimeKind.Unspecified);

        return dateTime;
    }

Upvotes: 2

Jason Hitchings
Jason Hitchings

Reputation: 677

I had a time sensitive fix and the SpecifyKind did not allow me to make proper TimeSpan comparisons. In my situation, the quickest solution was to simply remove the Hours, Minutes, and Seconds from the time, as the DateTime I was comparing 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);

Upvotes: 0

TheCodeKing
TheCodeKing

Reputation: 19220

You should always store DateTime in UTC format (universal). When you display it you can choose which ever timezone you wish, in your case this can be fixed for all users, rather than 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 things like day-light savings which can trip you up if start saving localized dates.

Upvotes: 12

ChrisF
ChrisF

Reputation: 137148

If you just store the DateTime as entered/picked by the user then it will be stored as just that. There will be time zone informations stored in it, but you are in control of what you do with it.

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

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

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

Upvotes: 1

Related Questions