1

I need to parse to following Date Aug 20 11:38:43 2017 GMT I'm trying to use DateTime.TryParseExact but can't find the correct format.

my latest format is MMM dd hh:mm:ss yyyy

my code :

string nextUpdate; //Next Update: Aug 20 11:38:43 2017 GMT
string dateTimeFormat =          "MMM dd hh:mm:ss yyyy";
DateTime crldt;
DateTime.TryParseExact(nextUpdate.Split(':')[1].Trim(), dateTimeFormat, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out crldt);

when I run the code I get crldt ~ Date = {1/1/01 12:00:00 AM}

my question : what format should I use or what alternative way can I use to parse this string to DateTime

UPDATE

using suggestions from : @Sergey Berezovskiy I've updated the code to :

    string nextUpdate; //  Next Update: Oct  7 06:16:18 2017 GMT
    string dateTimeFormat = @"MMM dd HH:mm:ss yyyy \G\M\T";
    Regex r =new Regex(".*Next Update:.*");
    nextUpdate = r.Match(Crltext).Value;
    DateTime crldt;             


  DateTime.TryParseExact(nextUpdate.Substring(nextUpdate.IndexOf(':')+1).Trim(),
  dateTimeFormat, System.Globalization.CultureInfo.InvariantCulture, 
  System.Globalization.DateTimeStyles.AssumeUniversal, out crldt);
  int intDTComp = DateTime.Compare(crldt, DT_now);

I've found a date that doesn't fit this format : Next Update: Oct 7 06:16:18 2017 GMT what is the issue now ?

UPDATE 2

I've found the issue , but can't find a clean solution. The issue is that the problematic date is Oct 7 ... , while the format is MMM dd ...

my workaround is adding another format MMM d hh:mm:ss yyyy and using it if date = {1/1/01 12:00:00 AM}

what other solution may I use in this scenario

LordTitiKaka
  • 2,087
  • 2
  • 31
  • 51
  • Possible duplicate: https://stackoverflow.com/questions/26105457/string-to-utc-time-conversion – Isma Aug 20 '17 at 10:58
  • 1
    Why are you using `Split()`? Isn't that just passing `"38"` to be parsed? `"38"` isn't a date. Or is it passing `"Aug 20 11"`? Also not a date. – David Aug 20 '17 at 10:58

4 Answers4

2

First of all, don't forget that your string contains "GMT". You should either remove it from the string, or add to format pattern:

string nextUpdate = "Next Update: Aug 20 11:38:43 2017 GMT";
string format = @"MMM dd hh:mm:ss yyyy \G\M\T";

Next - don't split input string by : because there is another : symbols in the string. And you will get array with parts ["Next Update", " Aug 20 11", "38", "43 2017 GMT"]. Taking the second item from array gives you " Aug 20 11". Instead, you should just take substring after first : occurance:

string s = nextUpdate.Substring(nextUpdate.IndexOf(':') + 1).Trim();

And finally parse that string using your format:

IFormatProvider provider = CultureInfo.InvariantCulture;
DateTime date = DateTime.ParseExact(s, format, provider, DateTimeStyles.AssumeUniversal);

Output will depend on your time zone. E.g. for my time zone GMT+3 it will be:

8/20/2017 14:38:43
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
1

If this is your string:

"Next Update: Aug 20 11:38:43 2017 GMT"

Then when you do this:

nextUpdate.Split(':')[1].Trim()

You get this:

"Aug 20 11"

Which doesn't match your date format. I suspect you don't just want the second split value, but also all remaining split values. You can re-join them. Something like this:

string.Join(":", nextUpdate.Split(':').Skip(1)).Trim()

This would split them by the ":" character, skip the first one but keep all remaining ones, and re-join the remaining ones into another string with the ":" character again.

Note: You may also need to account for that time zone value. There are some helpful ideas on this question for how to do that.

David
  • 208,112
  • 36
  • 198
  • 279
1

If that's the input, I propose that you take the format exactly as the input

string nextUpdate = "Next Update: Aug 20 11:38:43 2017 GMT";
string dateTimeFormat = @"\N\e\x\t\ \U\p\d\a\t\e\:\ MMM dd hh:mm:ss yyyy\ \G\M\T";
DateTime crldt;
crldt = DateTime.ParseExact(nextUpdate, dateTimeFormat , System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None);

This way, when reading the code it's very clear what the expected format is, and you don't have code with Split() or other items that need explanation.

Also note that if you leave the colons (:) like that, they may be replaced by .NET to match the hour separator. Use \: if you always require a literal colon.

As I wrote in the comments before, note that you're ignoring the time zone.

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
1

Use following code

CultureInfo provider = CultureInfo.InvariantCulture;
DateTime givenDate = DateTime.ParseExact("Aug 20 14:38:43 2017 GMT", "MMM dd HH:mm:ss yyyy GMT", provider); // here HH is for 24 hour format . use hh for 12 hour format
string expectedDate = givenDate.ToString("dd/MM/yy hh:mm:ss tt");  // tt is for AM or PM (no need to use tt if you use hour as HH I mean 24 hour format)

Your Output will be 20/08/17 11:38:43 AM

Sumon Tito
  • 105
  • 10