Date Formatting C#

前端 未结 7 1343
生来不讨喜
生来不讨喜 2021-01-27 08:32

I am having an issue converting this date format into another format. I was hoping that somebody on here would be able to help me out.

Here is my code:

s         


        
相关标签:
7条回答
  • 2021-01-27 09:27

    There's no simple, built-in way to convert from a Time Zone abbreviation to an offset or proper name. This is discussed in this topic here:

    Timezone Abbreviations

    You'll either need to create a table of the abbreviations you want to use, mapped to an offset or proper name, or you'll need to change the format of your incoming date/time string to use offsets.

    0 讨论(0)
  • 2021-01-27 09:27

    22 03 2013 00:00:00 GMT is not Monday it's Friday

    try

                string fromFormat = "ffffd, dd MM yyyy HH:mm:ss zzz";
                string toFormat = "yyyy-MM-dd";
    
                Console.WriteLine(DateTime.ParseExact("Fri, 22 03 2013 00:00:00 +00:00", fromFormat,
                                 CultureInfo.InvariantCulture).ToString(toFormat));
    
    0 讨论(0)
  • 2021-01-27 09:30

    As others pointed out, the input value you have is self-inconsistent. It refers to a March 22 2013 as a Monday when it is actually a Friday. So you should go back to your source data and figure out why that happens. I'm sure you have heard the saying, "Garbage In, Garbage Out".

    If you are sure you want to ignore the day of week, and you are certain that the time zone will always be GMT, then you can do this:

    var input = "Mon, 22 03 2013 00:00:00 GMT";
    var inner = input.Substring(5, 19);
    var format = "dd MM yyyy HH:mm:ss";
    var parsed = DateTime.ParseExact(inner, format, CultureInfo.InvariantCulture);
    var utcDateTime = DateTime.SpecifyKind(parsed, DateTimeKind.Utc);
    

    Note that I explicitly set the kind to UTC, because you are bringing in GMT values. GMT and UTC are identical for all practical purposes.

    If it's possible that other time zone values will be passed, then please provide a sampling of different possible inputs and perhaps we can find a way to accommodate that.

    As an aside - this string looks very similar to RFC822/RFC1123 formatted dates, except you are passing the month as a number instead of one of the three-letter abbreviations. Did you do this intentionally? If so, you have broken the spec for this format. If your intention was to remove potentially localizable strings from the data, then please use a format that is already designed for that, such as ISO8601/RFC3339.

    On Time Zones

    You said you want to convert to EST, You probably mean "US Eastern Time", which alternates between EST and EDT for daylight saving time. Despite this, the Windows Time Zone database uses the id of "Eastern Standard Time" to refer to both values - so try not to get confused on that point.

    Once you have the date as a DateTime of Utc kind, as I showed above, you can convert that to Eastern Time with the following:

    var zoneId = "Eastern Standard Time"; // don't get confused here! :)
    var eastern = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(utcDateTime, zoneId);
    

    Just be careful what you do with this value. If you are displaying it to an user, then you are ok. Just do this:

    var s = eastern.ToString("g"); // or whatever format you want.
    

    But if you do math with this, or store it as an recorded event time, you are potentially introducing errors into your results. This is due to daylight saving time transitions. One way to avoid this is to use a DateTimeOffset type instead:

    var utcDateTimeOffset = new DateTimeOffset(utcDateTime, TimeSpan.Zero);
    var eastern = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(utcDateTimeOffset, zoneId);
    

    Now when you work with these values, any ambiguities are captured by the offset.

    If this seems very confusing to you, don't worry - you're not alone. You might want to try using Noda Time instead. It will prevent you from shooting yourself in the foot.

    0 讨论(0)
  • 2021-01-27 09:30

    To see what's wrong, print a DateTime using your fromString:

    DateTime dt = new DateTime(2013, 3, 22);
    string s = dt.ToString(fromFormat);
    

    you'll see that the output is:

    Fri, 22 03 2013 00:00:00 -04:00
    

    so that is the format it would be expecting.

    You might be able to get some help on the abbreviations from this article.

    0 讨论(0)
  • 2021-01-27 09:30
    string fromFormat = "ffffd, dd MM yyyy HH:mm:ss zzz"; 
    string toFormat = "yyyy-MM-dd";
    
    DateTime newDate = DateTime.ParseExact("Mon, 22 03 2013 00:00:00 +00:00", fromFormat, null);
    
    Console.WriteLine(newDate.ToString(toFormat));
    
    0 讨论(0)
  • 2021-01-27 09:34
    string fromFormat = "ffffd, dd MM yyyy HH:mm:ss zzz"; 
    

    The error is your zzz, it is expecting the numerical representation of the timezone, not the abbreviation of the timezone.

    So a acceptable version would be

    DateTime newDate = DateTime.ParseExact("Mon, 22 03 2013 00:00:00 +0:00", fromFormat, null);
    

    but that would throw a different FormatExecption with the message "String was not recognized as a valid DateTime because the day of week was incorrect." If you make the Monday to Friday correction the parse works

    DateTime newDate = DateTime.ParseExact("Fri, 22 03 2013 00:00:00 +0:00", fromFormat, null);
    

    I don't think there is a format specifier that can take in the textual abbreviation version of the timezone.

    0 讨论(0)
提交回复
热议问题