我想在 SQL Server 数据库中以这种格式记录 payment_date 。
Update.直觉在这一点上是正确的。在这里找到了解决方案:http://www.codeillustrator.com/2010/03/converting-paypal- paymentdate-to-net.html,正在验证……当然,如果 Paypal 搬出西海岸,我就会遇到麻烦。有没有更好的方法来解析这个?也许与时区有关?
public static DateTime ConvertPayPalDateTime(string payPalDateTime)
{
// accept a few different date formats because of PST/PDT timezone and slight month difference in sandbox vs. prod.
string[] dateFormats = { "HH:mm:ss MMM dd, yyyy PST", "HH:mm:ss MMM. dd, yyyy PST", "HH:mm:ss MMM dd, yyyy PDT", "HH:mm:ss MMM. dd, yyyy PDT" };
DateTime outputDateTime;
DateTime.TryParseExact(payPalDateTime, dateFormats, new CultureInfo("en-US"), DateTimeStyles.None, out outputDateTime);
// convert to local timezone
outputDateTime = outputDateTime.AddHours(3);
return outputDateTime;
}
等一下,上面的代码对我来说是完全错误的。我在西海岸!理想情况下,应该更新它以将日期发送到正确的 UTC DateTime 并处理任何时区。此外,上面的代码无法正确处理 PDT(如果转换为 UTC)。
Update2.显然,至少在以前的版本中,沙箱会返回“二月”。而现场返回的是“二月”。哈哈。谁来救救我吧!
Update3.链接到正则表达式版本http://www.ifinity.com.au/Blog/EntryId/77/Converting-PayPal-Dates-to-Net-DateTime-using-Regex,但调试可能是一个问题。正则表达式似乎不是执行此操作的正确方法。一定会有更好的办法。
/// <summary>
/// Converts a PayPal datestring into a valid .net datetime value
/// </summary>
/// <param name="dateValue">a string containing a PayPal date</param>
/// <param name="localUtcOffset">the number of hours from UTC/GMT the local
/// time is (ie, the timezone where the computer is)</param>
/// <returns>Valid DateTime value if successful, DateTime.MinDate if not</returns>
private static DateTime ConvertFromPayPalDate(string rawPayPalDate, int localUtcOffset)
{
/* regex pattern splits paypal date into
* time : hh:mm:ss
* date : Mmm dd yyyy
* timezone : PST/PDT
*/
const string payPalDateRegex = @"(?<time>\d{1,2}:\d{2}:\d{2})\s(?<date>(?<
Mmm>[A-Za-z\.]{3,5})\s(?<dd>\d{1,2}),?\s(?<yyyy>\d{4}))\s(?<tz>[A-Z]{0,3})";
//!important : above line broken over two lines for formatting - rejoin in code editor
//example 05:49:56 Oct. 18, 2009 PDT
// 20:48:22 Dec 25, 2009 PST
Match dateMatch = Regex.Match(rawPayPalDate, payPalDateRegex, RegexOptions.IgnoreCase);
DateTime time, date = DateTime.MinValue;
//check to see if the regex pattern matched the supplied string
if (dateMatch.Success)
{
//extract the relevant parts of the date from regex match groups
string rawDate = dateMatch.Groups["date"].Value;
string rawTime = dateMatch.Groups["time"].Value;
string tz = dateMatch.Groups["tz"].Value;
//create date and time values
if (DateTime.TryParse(rawTime, out time) && DateTime.TryParse(rawDate, out date))
{
//add the time to the date value to get the datetime value
date = date.Add(new TimeSpan(time.Hour, time.Minute, time.Second));
//adjust for the pdt timezone. Pass 0 to localUtcOffset to get UTC/GMT
int offset = localUtcOffset + 7; //pdt = utc-7, pst = utc-8
if (tz == "PDT")//pacific daylight time
date = date.AddHours(offset);
else //pacific standard time
date = date.AddHours(offset + 1);
}
}
return date;
}