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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With