Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PayPal datetime (payment_date) parsing issue

Tags:

c#

.net

PayPal sends payment datetime like:

09%3A37%3A22+Nov+18%2C+2012+PST

I try to convert it by this code but getting an exception.

Any clue about how to parse it?

Thank you!

DateTime paymentDate;
DateTime.TryParse(WebUtility.HtmlDecode(r["payment_date"]), out paymentDate);
n.PaymentDate = paymentDate; // payment_date=09%3A37%3A22+Nov+18%2C+2012+PST
like image 374
Friend Avatar asked Nov 18 '12 23:11

Friend


2 Answers

There are various datetime formats in PayPal's documentation. I use this method to parse it, and convert it to UTC:

/// <summary>
/// Tries to parse PayPal formatted date and time and converts it to an UTC.
/// </summary>
public static bool TryParsePaypalDatetimeToUtc(this string paypalDatetime, out DateTime retValue)
{
    DateTime paymentDate;

    // PayPal formats from docs
    string[] formats = new string[] { "HH:mm:ss dd MMM yyyy PDT", "HH:mm:ss dd MMM yyyy PST", 
                                      "HH:mm:ss dd MMM, yyyy PST", "HH:mm:ss dd MMM, yyyy PDT", 
                                      "HH:mm:ss MMM dd, yyyy PST", "HH:mm:ss MMM dd, yyyy PDT" };
    if (false == DateTime.TryParseExact(paypalDatetime, formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out paymentDate))
    {
        retValue = DateTime.MinValue;
        return false;
    }

    retValue = TimeZoneInfo.ConvertTimeToUtc(paymentDate, TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time"));

    return true;
}
like image 134
DHlavaty Avatar answered Sep 18 '22 06:09

DHlavaty


This should do the trick:

DateTime paymentDate;
DateTime.TryParseExact(HttpUtility.UrlDecode(r["payment_date"]),
    "HH:mm:ss MMM dd, yyyy PST", CultureInfo.InvariantCulture,
    DateTimeStyles.None, out paymentDate);
like image 23
nick_w Avatar answered Sep 18 '22 06:09

nick_w