I would like to log a payment_date in this format in a SQL Server database.
Update. Instinct was right on this one. Found a solution here: http://w
Assuming you have already parsed the date with DateTime.ParseExact() (or one of the other similar methods) you can call DateTime.ToUniversalTime()
EDIT: perhaps TimeZoneInfo.ConvertTimeToUtc is more appropriate: The ToUniversalTime method converts a DateTime value from local time to UTC. To convert the time in a non-local time zone to UTC, use the TimeZoneInfo.ConvertTimeToUtc(DateTime, TimeZoneInfo) method. To convert a time whose offset from UTC is known, use the ToUniversalTime method.
This should work
public static DateTime ConvertPayPalDateTime(string payPalDateTime)
{
CultureInfo enUS = new CultureInfo("en-US");
// 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",
"HH:mm:ss dd MMM yyyy PST", "HH:mm:ss dd MMM. yyyy PST", "HH:mm:ss dd MMM yyyy PDT", "HH:mm:ss dd MMM. yyyy PDT"};
DateTime outputDateTime;
DateTime.TryParseExact(payPalDateTime, dateFormats, CultureInfo.InvariantCulture, DateTimeStyles.None, out outputDateTime);
// convert to local timezone
TimeZoneInfo hwZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
outputDateTime = TimeZoneInfo.ConvertTime(outputDateTime, hwZone, TimeZoneInfo.Local);
return outputDateTime;
}
I haven't done any C# since 2006, so this code probably doesn't compile. Test it before you fly!
public static DateTime ConvertPayPalDateTime(string payPalDateTime)
{
// Get the offset.
// If C# supports switching on strings, it's probably more sensible to do that.
int offset;
if (payPalDateTime.EndsWith(" PDT"))
{
offset = 7;
}
else if (payPalDateTime.EndsWith(" PST"))
{
offset = 8;
}
else
{
throw some exception;
}
// We've "parsed" the time zone, so remove it from the string.
payPalDatetime = payPalDateTime.Substring(0,payPalDateTime.Length-4);
// Same formats as above, but with PST/PDT removed.
string[] dateFormats = { "HH:mm:ss MMM dd, yyyy", "HH:mm:ss MMM. dd, yyyy" };
// Parse the date. Throw an exception if it fails.
DateTime ret = DateTime.ParseExact(payPalDateTime, dateFormats, new CultureInfo("en-US"), DateTimeStyles.None, out outputDateTime);
// Add the offset, and make it a universal time.
return ret.AddHours(offset).SpecifyKind(DateTimeKind.Universal);
}
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 to cast this date and save to database)