I have this Java code that allows the input of a string of that particular format and converts it to Unix time (a long type).

string input = "2014-03-08T01:00:00-08:00";

Calendar cal = Calendar.getInstance();
SimpleDateFormat dateParser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");

(long) dateParser.parse(input.getTime())/1000

I was wondering is there an equivalent to this in C# that follows the same parameters?

有帮助吗?

解决方案

Try this code. See if it helps.

using System;
using System.Globalization;

namespace ConsoleApplication 
{
    class Program
    {
        static void Main(string[] args)
        {
            string dateString, format;
            DateTime result;
            CultureInfo provider = CultureInfo.InvariantCulture;

            dateString = "2010-12-25T05:05:05.888";
            format = "yyyy-MM-dd'T'HH:mm:ss.fff";
            try
            {
                result = DateTime.ParseExact(dateString, format, provider);
                Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());
                Console.WriteLine(result.Millisecond);
            }
            catch (FormatException fe)
            {
                Console.WriteLine(fe.StackTrace);
                Console.WriteLine("{0} is not in the correct format", dateString);
            }
        }
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top