简体   繁体   中英

Converting dates in C#

I have an ASP.NET MVC app that is not converting dates values properly. I reside in one time zone. My users resides in another. At this time, assume I have the following string:

var date = "7/1/2014 4:00:00 AM +00:00";

I am converting this string to a DateTime using the following:

DateTime temp;
if (DateTime.TryParse(date, out temp))
{
    temp = temp.ToShortDateString();
    WriteToLog(temp);
}

When temp is written to the log file, I see it being written as 6/30/2014 . What would possibly cause this? I'm expecting 7/1/2014 . It works on my machine. However, it is not working on my users machine.

The answer is timezones. You're parsing a specific point in time (4:00 AM, GMT). This is the same point in time as say 10:00 PM CST the day before.

If you keep it in UTC:

var s = temp.ToUniversalTime().ToShortDateString();

You'll get the requested output.

string date = "7/1/2014 4:00:00 AM +00:00";
DateTime temp;
if (DateTime.TryParse(date, CultureInfo.InstalledUICulture,
                            DateTimeStyles.AdjustToUniversal, out temp))
{
    string result = temp.ToShortDateString());
}

By default the code will use server's local time, here is how I use DateTime in an online Website:

  1. Convert it to UTC:

     var mydate = DateTime.UtcNow; // Or var mydate2 = (new DateTime(2015, 6, 3)).ToUniversalTime(); 

    This will Make Your app Time Zone Independent.

  2. Convert it to Desired time zone:

    Using TimeZoneInfo Class to Convert local time to another alternative Time Zone

     var mydate = DateTime.UtcNow; TimeZoneInfo iranTimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Iran Standard Time"); var tehranDateTime = TimeZoneInfo.ConvertTime(mydate, iranTimeZoneInfo); 

This code will always return Tehran local time on any computer with correct Date-Time & Time Zone Configuration.

here is a list of valid TimeZone id's that you can use:

- Dateline Standard Time
- UTC-11
- Hawaiian Standard Time
- Alaskan Standard Time
- Pacific Standard Time (Mexico)
- Pacific Standard Time
- US Mountain Standard Time
- Mountain Standard Time (Mexico)
- Mountain Standard Time
- Central America Standard Time
- Central Standard Time
- Central Standard Time (Mexico)
- Canada Central Standard Time
- SA Pacific Standard Time
- Eastern Standard Time
- US Eastern Standard Time
- Venezuela Standard Time
- Paraguay Standard Time
- Atlantic Standard Time
- Central Brazilian Standard Time
- SA Western Standard Time
- Pacific SA Standard Time
- Newfoundland Standard Time
- E. South America Standard Time
- Argentina Standard Time
- SA Eastern Standard Time
- Greenland Standard Time
- Montevideo Standard Time
- Bahia Standard Time
- UTC-02
- Mid-Atlantic Standard Time
- Azores Standard Time
- Cape Verde Standard Time
- Morocco Standard Time
- UTC
- GMT Standard Time
- Greenwich Standard Time
- W. Europe Standard Time
- Central Europe Standard Time
- Romance Standard Time
- Central European Standard Time
- W. Central Africa Standard Time
- Namibia Standard Time
- Jordan Standard Time
- GTB Standard Time
- Middle East Standard Time
- Egypt Standard Time
- Syria Standard Time
- E. Europe Standard Time
- South Africa Standard Time
- FLE Standard Time
- Turkey Standard Time
- Israel Standard Time
- Kaliningrad Standard Time
- Libya Standard Time
- Arabic Standard Time
- Arab Standard Time
- Belarus Standard Time
- Russian Standard Time
- E. Africa Standard Time
- Iran Standard Time
- Arabian Standard Time
- Azerbaijan Standard Time
- Russia Time Zone 3
- Mauritius Standard Time
- Georgian Standard Time
- Caucasus Standard Time
- Afghanistan Standard Time
- West Asia Standard Time
- Ekaterinburg Standard Time
- Pakistan Standard Time
- India Standard Time
- Sri Lanka Standard Time
- Nepal Standard Time
- Central Asia Standard Time
- Bangladesh Standard Time
- N. Central Asia Standard Time
- Myanmar Standard Time
- SE Asia Standard Time
- North Asia Standard Time
- China Standard Time
- North Asia East Standard Time
- Singapore Standard Time
- W. Australia Standard Time
- Taipei Standard Time
- Ulaanbaatar Standard Time
- Tokyo Standard Time
- Korea Standard Time
- Yakutsk Standard Time
- Cen. Australia Standard Time
- AUS Central Standard Time
- E. Australia Standard Time
- AUS Eastern Standard Time
- West Pacific Standard Time
- Tasmania Standard Time
- Magadan Standard Time
- Vladivostok Standard Time
- Russia Time Zone 10
- Central Pacific Standard Time
- Russia Time Zone 11
- New Zealand Standard Time
- UTC+12
- Fiji Standard Time
- Kamchatka Standard Time
- Tonga Standard Time
- Samoa Standard Time
- Line Islands Standard Time

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