How to cast this date and save to database
08:15:16 April 1, 2010 PDT
I need to save this to DateTime in MSSQL database but I can't convert this in C#. Can s开发者_JAVA百科omebody help me to cast it? And what the hell is 'PDT
'?
This post is related to Parse DateTime with time zone of form PST/CEST/UTC/etc
I'm not sure how to get from alpha timezone to numeric, but after you do that you can
DateTime myDate = Convert.ToDateTime("Thu, 18 Mar 2004 14:52:56-07:00");
Update: This link apparently contains code to do the "PDT" to GMT offset conversion. http://bytes.com/topic/c-sharp/answers/214648-how-do-i-parse-date-w-time-zone Also check out the TimeZoneInfo class http://msdn.microsoft.com/en-us/library/system.timezoneinfo.aspx
The code in this post seems to work fine: 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;
}
}
(answer cross-posted for this similar question: How do I convert Paypal's HH:MM:SS DD Mmm(.) YYYY PST/PDT to a C# UTC DateTime?)
精彩评论