How do I convert Paypal's HH:MM:SS DD Mmm(.) YYYY PST/PDT to a C# UTC DateTime?

后端 未结 4 439
南旧
南旧 2021-01-12 01:28

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

4条回答
  •  [愿得一人]
    2021-01-12 02:01

    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)

提交回复
热议问题