简体   繁体   中英

PayPal datetime (payment_date) parsing issue

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

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;
}

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM