Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to know which datetime.TryParseExact function will support for format like '2021-11-21 11:34:45.234567+0000' in C#.Net?

I am getting above datetime format in my source file.

What I have tried:

I have tried "yyyy-MM-dd HH:mm:ss.ffffffzzz", But Its not working. Thanks.
Posted
Updated 22-Nov-21 1:21am
Comments
Richard MacCutchan 22-Nov-21 7:02am    
You forgot the + sign after ffffff, and the time zone field should be four characters: zzzz.
Member 10833473 22-Nov-21 7:14am    
Thanks, But Its not working too.
Maciej Los 22-Nov-21 7:20am    
Richard, [+] sign is redundant. Please, see my answer.
Richard MacCutchan 22-Nov-21 7:33am    
I lazily did not test it. Thus a comment rather than a solution.
Maciej Los 22-Nov-21 8:08am    
OK.

Take a look at example:
C#
string s = @"2021-11-21 11:34:45.234567+0000";
string format = "yyyy-MM-dd HH:mm:ss.ffffffzzz";
System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.InvariantCulture;
DateTime d;
if(DateTime.TryParseExact(s, format, ci, System.Globalization.DateTimeStyles.None, out d))
	Console.WriteLine(d.ToString());
else
	Console.WriteLine("Incorrect date format!");


More at: Custom date and time format strings | Microsoft Docs[^]
 
Share this answer
 
Comments
Member 10833473 23-Nov-21 6:11am    
This format is not working Los.
Maciej Los 23-Nov-21 6:22am    
I have tested it and it's working fine.
Member 10833473 23-Nov-21 7:29am    
Its adding half an hour more in the above time, So whatever time is in out Input, It gives 30 minutes plus. Thanks
Try this:
C#
string input = "2021-11-21 11:34:45.234567+0000";
DateTime dt;
if (DateTime.TryParseExact(input,
                           "yyyy-MM-dd hh:mm:ss.ffffffzzzz",
                           CultureInfo.InvariantCulture,
                           DateTimeStyles.None,
                           out dt))
    {
    Console.WriteLine(dt);
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900