Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse datetime in multiple formats

Tags:

c#

datetime

I have created an API end-point. The caller may call the API with POST method passing the relevant parameters. In the parameters there is one parameter that is of datetime format.

The problem is that when calling this API the caller may passes datetime in 3 different formats:

  1. long int - e.g. 1374755180
  2. US format - e.g. "7/25/2013 6:37:31 PM" (as string)
  3. Timestamp format - e.g. "2013-07-25 14:26:00" (as string)

I have to parse the datetime value and convert it to a DateTime or string in Timestamp format.

I have tried using DateTime.TryParse(), DateTime.Parse(), Convert.ToDateTime() and Convert.ToDouble() but none of them are working in certainty for me.

The required output has to be in en-GB format.

Edit:

I had thought to have an if-else if-else block to use with TryParse 3 times with one else to say the string could not be parsed. Is this the best solution? Or are there solutions better than this?

Please help!

like image 440
Vivek Jain Avatar asked Jul 25 '13 13:07

Vivek Jain


People also ask

What formats does datetime parse?

Only the ISO 8601 format ( YYYY-MM-DDTHH:mm:ss.sssZ ) is explicitly specified to be supported.

What does datetime ParseExact do?

The DateTime. ParseExact(String, String, IFormatProvider) method parses the string representation of a date, which must be in the format defined by the format parameter.

How does Python handle different date formats?

Use datetime. strftime(format) to convert a datetime object into a string as per the corresponding format . The format codes are standard directives for mentioning in which format you want to represent datetime. For example, the %d-%m-%Y %H:%M:%S codes convert date to dd-mm-yyyy hh:mm:ss format.

What is datetime parsing?

The Parse method tries to convert the string representation of a date and time value to its DateTime equivalent. It tries to parse the input string completely without throwing a FormatException exception.


2 Answers

You should consider requiring a timezone. 1 doesn't need it, but #2 and #3 do.

public DateTime ParseRequestDate() {     // https://stackoverflow.com/questions/2883576/how-do-you-convert-epoch-time-in-c      CultureInfo enUS = new CultureInfo("en-US");      var dt = "1374755180";     //var dt = "7/25/2013 6:37:31 PM";     //var dt = "2013-07-25 14:26:00";      DateTime dateValue;     long dtLong;      // Scenario #1     if (long.TryParse(dt, out dtLong))         return dtLong.FromUnixTime();      // Scenario #2     if (DateTime.TryParseExact(dt, "MM/dd/yyyy hh:mm:ss tt", enUS, DateTimeStyles.None, out dateValue))         return dateValue;      // Scenario #3     if (DateTime.TryParseExact(dt, "yyyy-MM-dd hh:mm:ss", enUS, DateTimeStyles.None, out dateValue))         return dateValue;      throw new SomeException("Don't know how to parse..."); } 

EDIT As Matt Johnson points out, DateTime.TryParseExact accepts an array of format strings. 2 & 3 could be condensed.

public DateTime ParseRequestDate() {     // https://stackoverflow.com/questions/2883576/how-do-you-convert-epoch-time-in-c      CultureInfo enUS = new CultureInfo("en-US");      var dt = "1374755180";     //var dt = "7/25/2013 6:37:31 PM";     //var dt = "2013-07-25 14:26:00";      DateTime dateValue;     long dtLong;      // Scenario #1     if (long.TryParse(dt, out dtLong))         return dtLong.FromUnixTime();      // Scenario #2 & #3     var formatStrings = new string[] { "MM/dd/yyyy hh:mm:ss tt", "yyyy-MM-dd hh:mm:ss" };     if (DateTime.TryParseExact(dt, formatStrings, enUS, DateTimeStyles.None, out dateValue))         return dateValue;      throw new SomeException("Don't know how to parse..."); } 

The epoch conversion I borrowed from another question. (An extension method)

public static class MyExtensions {     public static DateTime FromUnixTime(this long unixTime)     {         var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);         return epoch.AddSeconds(unixTime);     } } 
like image 159
Randy James Avatar answered Sep 22 '22 04:09

Randy James


You are looking for the DateTime.ParseExact (MSDN Article)

Which you would use in a situation like this:

string[] formats= { "MM/dd/yyyy hh:mm:ss tt", "yyyy-MM-dd hh:mm:ss" } var dateTime = DateTime.ParseExact("07/25/2013 6:37:31 PM", formats, new CultureInfo("en-GB"), DateTimeStyles.None); 

This allows you to add as many DateTime formats to the array as you need and the method will do the conversion without the if...else statements.

If your integer is in seconds since Unix Epoch you add the number of seconds to the DateTime of the Epoch (01/01/1970) (.Net doesn't have an out of the box method for this, but the logic is seconds since 'Epoch'):

new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(seconds); 

From this question.

like image 34
Darbio Avatar answered Sep 20 '22 04:09

Darbio