What is the correct way in C# to parse this date format "Mon Oct 07 00:00:00 EDT 2013"?

leora

I am getting a json message from a server and I am trying to parse it into C# objects. I am using the RestSharp Deserializer. There is one field that is not properly converting into a datetime:

the string value of the field in this message is:

"createDateTime":"Mon Oct 07 00:00:00 EDT 2013"

on my object I have this as:

public DateTime? createDateTime { get; set; }

NOTE: that is a nullable DateTime because something the field is blank

but when I do this:

var deSerializer = new JsonDeserializer();
var response = client.Execute(request);
var responseObj = _deSerializer .Deserialize<Response>(response);
return responseObj;

I realized the root cause is that the DateTime.Parse is failing. I tried adding this which causes it to use

   DateTime.ParseExact()

  _deserializer.DateFormat = "ddd MMM dd HH:mm:ss zzz yyyy";

but I then get an error stating:

String was not recognized as a valid DateTime.

so this all comes down to how in C# to parse a date coming in as this format

   Mon Oct 07 00:00:00 EDT 2013
leora

since in my case I don't really care about time and the timezone is always in EDT, i wrote this to get around this issue in the short term.

    private DateTime? ParseMe(string s)
    {
        var split = s.Split(new[] {' '},StringSplitOptions.RemoveEmptyEntries);

        var year = int.Parse(split[split.Count()-1]);
        var day = int.Parse(split[2]);
        var month = split[1];
        int monthInDigit = DateTime.ParseExact(month, "MMM", CultureInfo.InvariantCulture).Month;
        return new DateTime(year, monthInDigit, day);
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

What is the correct date format (NSDateFormatter)?

What is the correct date format for writexl

What is the correct way to format this grep?

Correct format string to parse a date time in Haskell

What is the correct date format for this date-as-a-string?

What is the best way to parse a millisecond date time in C++11

What is the correct api document for date().parse()?

What is the correct way to parse variables using JavaParser?

What is the best way to correct parse log file?

Easy way to parse syslog date format

What is the best way to parse a date in MM/DD/YY format and adjust it to the current / previous century?

What's the correct date format for this case?

What is correct date format in material-ui?

What Java Date Format to correctly parse?

Correct Way of Email format Validation in Objective c

What is the best way to convert date format in DOM

What is this date format? 44303.02359 . new Date() can't parse it

Parse Particular Date Format C#

What would be the correct json format to import GeoPoints in Parse.com?

What is the correct way to parse extended hello messages in TLS protocol?

What is the correct way to parse my nested JSON data using Swift?

What is the correct way to parse the list items using Map?

is there a way to check if a column has correct format of date in postgresql?

There is a way to parse and compare weather.gov date format with Javascript?

What is the correct format for denoting that an input parameter is a date/time in Apiary?

What is the correct Date Format String for Month DD, YYYY to use in Powershell

What is the correct syntax for Date format in Oracle SQL Developer

What is the correct way to define this C macro?

What is the correct way to initialize a variable in C++

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive