Home > Software engineering >  C# Convert a string "yyyy-MM-dd hh:mm:ss 'UTC'" to date time
C# Convert a string "yyyy-MM-dd hh:mm:ss 'UTC'" to date time

Time:06-21

i have a string, as an example: "2022-04-17 14:46:31 UTC"

And i want to convert it to date time but i have got the following error: {"The string '2022-04-17 14:46:31 UTC' was not recognized as a valid DateTime. There is an unknown word starting at index '20'."}

Notice that i have tried:

  1. var date = DateTime.Parse("2022-04-17 14:46:31 UTC")

2.DateTime dt; DateTime.TryParseExact("2022-04-17 14:46:31 UTC", "yyyy-MM-dd hh:mm:ss 'UTC'", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt)

CodePudding user response:

There are two things to be done:

  1. as mentioned in the comments: use HH for the 24 hour format
  2. adjust the result to an actual UTC time
DateTime.TryParseExact("2022-04-17 14:46:31 UTC", "yyyy-MM-dd HH:mm:ss 'UTC'", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt);
DateTime utc = new DateTime(dt.Ticks,  DateTimeKind.Utc);

CodePudding user response:

According to this answer you need to convert time zone name to time shift.

So to do it first we need to get table of time zone name to time zone shift. I copy it from here and convert to dictionary.

And the full algorithm is:

  1. Find time zone name
  2. Find it shift in dictionary
  3. Replace time zone name with shift
  4. Parse date string

var dateString = "2022-04-17 14:46:31 UTC";
var lastWhiteSpace = dateString.LastIndexOf(' ');
var timeZoneName = dateString.Substring(lastWhiteSpace 1);
var correctDate = dateString.Replace(timeZoneName, TimeZoneToShift[timeZoneName]);
var date = DateTime.ParseExact(correctDate, "yyyy-MM-dd HH:mm:ss zzz", null);

public static Dictionary<string, string> TimeZoneToShift = new Dictionary<string, string>()
{
    {"ACDT", "-10:30"},
    {"ACST", "-09:30"},
    {"ADT", " 03:00"},
    {"AEDT", "-11:00"},
    {"AEST", "-10:00"},
    {"AHDT", " 09:00"},
    {"AHST", " 10:00"},
    {"AST", " 04:00"},
    {"AT", " 02:00"},
    {"AWDT", "-09:00"},
    {"AWST", "-08:00"},
    {"BAT", "-03:00"},
    {"BDST", "-02:00"},
    {"BET", " 11:00"},
    {"BST", " 03:00"},
    {"BT", "-03:00"},
    {"BZT2", " 03:00"},
    {"CADT", "-10:30"},
    {"CAST", "-09:30"},
    {"CAT", " 10:00"},
    {"CCT", "-08:00"},
    {"CDT", " 05:00"},
    {"CED", "-02:00"},
    {"CET", "-01:00"},
    {"CST", " 06:00"},
    {"EAST", "-10:00"},
    {"EDT", " 04:00"},
    {"EED", "-03:00"},
    {"EET", "-02:00"},
    {"EEST", "-03:00"},
    {"EST", " 05:00"},
    {"FST", "-02:00"},
    {"FWT", "-01:00"},
    {"GMT", " 00:00"},
    {"GST", "-10:00"},
    {"HDT", " 09:00"},
    {"HST", " 10:00"},
    {"IDLE", "-12:00"},
    {"IDLW", " 12:00"},
    {"IST", "-05:30"},
    {"IT", "-03:30"},
    {"JST", "-09:00"},
    {"JT", "-07:00"},
    {"MDT", " 06:00"},
    {"MED", "-02:00"},
    {"MET", "-01:00"},
    {"MEST", "-02:00"},
    {"MEWT", "-01:00"},
    {"MST", " 07:00"},
    {"MT", "-08:00"},
    {"NDT", " 02:30"},
    {"NFT", " 03:30"},
    {"NT", " 11:00"},
    {"NST", "-06:30"},
    {"NZ", "-11:00"},
    {"NZST", "-12:00"},
    {"NZDT", "-13:00"},
    {"NZT", "-12:00"},
    {"PDT", " 07:00"},
    {"PST", " 08:00"},
    {"ROK", "-09:00"},
    {"SAD", "-10:00"},
    {"SAST", "-09:00"},
    {"SAT", "-09:00"},
    {"SDT", "-10:00"},
    {"SST", "-02:00"},
    {"SWT", "-01:00"},
    {"USZ3", "-04:00"},
    {"USZ4", "-05:00"},
    {"USZ5", "-06:00"},
    {"USZ6", "-07:00"},
    {"UT", " 00:00"},
    {"UTC", " 00:00"},
    {"UZ10", "-11:00"},
    {"WAT", " 01:00"},
    {"WET", " 00:00"},
    {"WST", "-08:00"},
    {"YDT", " 08:00"},
    {"YST", " 09:00"},
    {"ZP4", "-04:00"},
    {"ZP5", "-05:00"},
    {"ZP6", "-06:00"},
};
  • Related