9

PayPal 发送付款方式datetime如下:

09%3A37%3A22+11 月+18%2C+2012+太平洋标准时间

我尝试通过此代码对其进行转换,但出现异常。

关于如何解析它的任何线索?

谢谢!

DateTime paymentDate;
DateTime.TryParse(WebUtility.HtmlDecode(r["payment_date"]), out paymentDate);
n.PaymentDate = paymentDate; // payment_date=09%3A37%3A22+Nov+18%2C+2012+PST
4

2 回答 2

14

PayPal 的文档中有多种日期时间格式。我使用此方法对其进行解析,并将其转换为 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;
}
于 2013-09-25T12:16:58.457 回答
11

这应该可以解决问题:

DateTime paymentDate;
DateTime.TryParseExact(HttpUtility.UrlDecode(r["payment_date"]),
    "HH:mm:ss MMM dd, yyyy PST", CultureInfo.InvariantCulture,
    DateTimeStyles.None, out paymentDate);
于 2012-11-18T23:12:35.037 回答