如何转换此日期并保存到数据库

时间:2011-05-23 08:29:05

标签: c# asp.net datetime casting paypal

08:15:16 April 1, 2010 PDT我需要将它保存到MSSQL数据库中的DateTime,但我无法在C#中转换它。有人可以帮我施展吗?到底是什么'PDT'?

2 个答案:

答案 0 :(得分:2)

这篇文章与Parse DateTime with time zone of form PST/CEST/UTC/etc

有关

我不确定如何从alpha时区到数字,但在你这样做之后你可以

DateTime myDate = Convert.ToDateTime("Thu, 18 Mar 2004 14:52:56-07:00");

更新:此链接显然包含执行“PDT”到GMT偏移转换的代码。 http://bytes.com/topic/c-sharp/answers/214648-how-do-i-parse-date-w-time-zone另请查看TimeZoneInfo类http://msdn.microsoft.com/en-us/library/system.timezoneinfo.aspx

答案 1 :(得分:1)

这篇文章中的代码似乎运行正常:http://www.codeillustrator.com/2010/03/converting-paypal-paymentdate-to-net.html

using System;
using System.Globalization;

public static class PayPalTransaction
{
    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;
    }
}

(回答这个类似问题的交叉发布:How do I convert Paypal's HH:MM:SS DD Mmm(.) YYYY PST/PDT to a C# UTC DateTime?