How to parse custom string to DateTime C#? -


i need parse following date aug 20 11:38:43 2017 gmt i'm trying use datetime.tryparseexact can't find correct format.

my latest format 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 run code crldt ~ date = {1/1/01 12:00:00 am}

my question : format should use or alternative way can use parse string datetime

update

using suggestions : @sergey berezovskiy i've updated code :

    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 date doesn't fit format : next update: oct 7 06:16:18 2017 gmt issue ?

update 2

i've found issue , can't find clean solution. issue problematic date oct 7 ... , while format mmm dd ...

my workaround adding format mmm d hh:mm:ss yyyy , using if date = {1/1/01 12:00:00 am}

what other solution may use in scenario

first of all, don't forget string contains "gmt". should either remove string, or add 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 : because there : symbols in string. , array parts ["next update", " aug 20 11", "38", "43 2017 gmt"]. taking second item array gives " aug 20 11". instead, should take substring after first : occurance:

string s = nextupdate.substring(nextupdate.indexof(':') + 1).trim(); 

and parse string using format:

iformatprovider provider = cultureinfo.invariantculture; datetime date = datetime.parseexact(s, format, provider, datetimestyles.assumeuniversal); 

output depend on time zone. e.g. time zone gmt+3 be:

8/20/2017 14:38:43 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -