Click here to Skip to main content
15,881,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Mon, 18 Jun 2018 10:26:11 +0000 (UTC)


How to convert the above type of date string in ASP.NET

What I have tried:

i tried like this
DateTime.TryParse(msg.Headers.Date, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out dtout);


but not working. plz help
Posted
Updated 7-Jan-21 3:23am

Try DateTime.TryParseExact:
string inp = "Mon, 18 Jun 2018 10:26:11 +0000 (UTC)";
string format = "ddd, d MMM yyyy hh:mm:ss zzzz (UTC)";
DateTime dt;
if (!DateTime.TryParseExact(inp, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
    {
    Console.WriteLine("Nope!");
    }
Me? I'd probably strip off the "(abc)" part from the end instead of hardcoding the timezone name
 
Share this answer
 
Comments
[no name] 18-Jun-18 8:07am    
Nice and elegant solution. If the timezone is not (UTC) however, it won't parse. I made an alternative that just checks the +0000 part.
OriginalGriff 18-Jun-18 8:08am    
That's why I said I'd probably strip it off! :laugh:
[no name] 18-Jun-18 8:09am    
Aha, I misunderstood that the first time I read it. All good then :)
OriginalGriff has a nice solution, but if the timezone is not (UTC), it will not parse. I would instead remove that part before parsing:

C#
string inp = "Mon, 18 Jun 2018 10:26:11 +0000 (UTC)";
inp = inp.Substring(0, inp.IndexOf(" ("));
string format = "ddd, d MMM yyyy hh:mm:ss zzzz";
DateTime dt;
if (!DateTime.TryParseExact(inp, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dt)) {
  Console.WriteLine("Nope!");
}
 
Share this answer
 
This posting has date formatting solutions see if it works for you

https://www.c-sharpcorner.com/blogs/date-and-time-format-in-c-sharp-programming1
 
Share this answer
 
I did this by checking the `DateTimeKind`. On my function, 2 different types of date-times are coming. What I want is to convert UTC time to local time from the below function. Input parameter `date` is always coming as UTC.

Eg inputs: `2021-01-19 07:43:00 AM` and `01/07/2021 02:16:00 PM +00:00`

    public static DateTime GetDateTime(string date)
        {
            try
            {
                DateTime parsedDate = DateTime.Parse(date, GetCulture()); //invarient culture

                if (parsedDate.Kind == DateTimeKind.Unspecified)
                {
                    parsedDate = DateTime.SpecifyKind(parsedDate, DateTimeKind.Utc);
                }
                
                return parsedDate.ToLocalTime();
            }
            catch (Exception e)
            {
                throw;
            }
        }
 
Share this answer
 
Comments
CHill60 8-Jan-21 6:48am    
Why bother using try-catch if you are just going to throw every exception?

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