0

My website receives some data from PayPal. PayPal provides a string to represent the date time

10:52:32 Jan 17, 2019 PST

As it's a string, I understand I can manually break this up by splitting by white space, comma and by colon. I can then create the DateTime object based upon what has been split

Before I attempt this, my questions is, can this be done "automatically" by the Framework?

My effort suggests no

DateTime.Parse("10:52:32 Jan 17, 2019 PST");   //System.FormatException: 'The string was not recognized as a valid DateTime. There is an unknown word starting at index NN.'
MyDaftQuestions
  • 4,487
  • 17
  • 63
  • 120
  • Possible duplicate of [Parse DateTime with time zone of form PST/CEST/UTC/etc](https://stackoverflow.com/questions/241789/parse-datetime-with-time-zone-of-form-pst-cest-utc-etc) – haldo Jan 18 '19 at 13:45
  • Are you sure that PayPal does not return payment date in better format (ISO / yyyy-mm-dd...)? – i486 Jan 18 '19 at 14:49
  • @i486 https://developer.paypal.com/docs/classic/ipn/integration-guide/IPNandPDTVariables/. So no, I'm not sure as I'm currently testing their IPN simulator but I'd hope the simulator is in the same structure as the real IPN – MyDaftQuestions Jan 18 '19 at 16:46

2 Answers2

1

You can use the DateTime method ParseExact for this purpose:

var dateTimeString = "10:52:32 Jan 17, 2019 PST";
var dateTime = DateTime.ParseExact(dateTimeString.Substring(0, dateTimeString.LastIndexOf(" ")), "HH:mm:ss MMM dd, yyyy", CultureInfo.InvariantCulture);

Unfortunately, the framework has poor support for timezones. You will have to create a dictionary of timezones and map them to their respective time offset and manually add that to the resulting DateTime object.

Here's one way of doing it: https://stackoverflow.com/a/30303587/633098.

silkfire
  • 24,585
  • 15
  • 82
  • 105
  • Make the parse exact `DateTime.ParseExact(inputDate.Substring(0, inputDate.LastIndexOf(" ")), "HH:mm:ss MMM dd, yyyy", CultureInfo.InvariantCulture);` instead to handle timezone that are more or less than three characters. – ZarX Jan 18 '19 at 15:06
0

You could use DateTime.ParseExact to parse the string.

If you always know the timezone you can use the following

var paypalDateTime = DateTime.ParseExact("10:52:32 Jan 17, 2019 PST", "HH:mm:ss MMM dd, yyyy PST", CultureInfo.InvariantCulture);
Console.WriteLine(paypalDateTime.ToString());
// Output: 1/17/2019 10:52:32

If you want to

    var inputDates = new [] { 
        "10:52:32 Jan 17, 2019 PST",
        "10:52:32 Jan 17, 2019 GMT",
        "10:52:32 Jan 17, 2019 UT",
        "10:52:32 Jan 17, 2019 NZST"
    };

    foreach (var inputDate in inputDates) 
    {
        var timeZone = inputDate.Substring(inputDate.LastIndexOf(" ") + 1);  
        var paypalDateTime = DateTime.ParseExact(inputDate.Substring(0, inputDate.LastIndexOf(" ")), "HH:mm:ss MMM dd, yyyy", CultureInfo.InvariantCulture);
        // Here you can handle the timezone
        Console.WriteLine(paypalDateTime.ToString() + " in timezone " + timeZone);
    }

Outputs

1/17/2019 10:52:32 AM in timezone PST
1/17/2019 10:52:32 AM in timezone GMT
1/17/2019 10:52:32 AM in timezone UT
1/17/2019 10:52:32 AM in timezone NZST
ZarX
  • 1,096
  • 9
  • 17