0

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?

1
  • cal appears to be unused. Commented Mar 7, 2014 at 16:56

1 Answer 1

2

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);
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Hi there peter. It appears that this only returns a 3-digit number when I use 2010-12-25T05:05:05.888 as an example. What property would return a format that's like this: puu.sh/7mn7D/4a850f37f6.png
Hey @peter -- I just noticed that the trailing digits of -07:00 in 2014-03-10T08:00:00-07:00 indicates the time zone. Any ideas what to do with it and include it in the conversion?
Not sure. You may want to look here and you use it if/as needed. msdn.microsoft.com/en-us/library/bb382770%28v=vs.110%29.aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.