0

I am trying to convert string to DateTime. Code look as follows:

DateTime.Parse("20131101T210705.282Z").ToShortTimeString()

I am getting format exception.

I tried providing following format "yyyyMMddTssmmhh.fffz" but received same exception. Code looked as follows

DateTime dt;
if (DateTime.TryParseExact("20131101T210705.282Z",
                           "yyyyMMddTssmmhh.fffz",
                          new CultureInfo("en-US"),
                          DateTimeStyles.None,
                          out dt))
    return dt.ToShortTimeString();

In this case code doesn't parse the string.

3 Answers 3

1

Try this :

 DateTime dt;
    if (DateTime.TryParseExact("20131101T210705.282Z",
                               "yyyyMMddTssmmhh.fffZ",
                              new CultureInfo("en-US"),
                              DateTimeStyles.None,
                              out dt))
        return dt.ToShortDateString() + " " + dt.ToShortTimeString();
Sign up to request clarification or add additional context in comments.

3 Comments

Did you try this code? I can see our arguments for TryParseExact () are excatly same that means if condition is going to fail.
i feel that you are giving format in wrong for TryParseExact() as -> 20131101T210705.282Z != yyyyMMddTssmmhh.fffz
Yes i am struggling with format. I made that using the chart provided on MSDN.What is correct format?
0

This might be one way to parse.

   var timeStamp = "20131101T210705.282Z";
   var datetime = timeStamp.Split(new[] { 'T' ,'.'});
   DateTime dt1;


  if (DateTime.TryParseExact(datetime[0],
                     new string[] { "yyyyMMdd" },
                    new CultureInfo("en-US"),
                    DateTimeStyles.None,
                    out dt1))
  {
    Console.WriteLine(dt1.ToShortDateString());
  }

  DateTime dt2;


  if (DateTime.TryParseExact(datetime[1],
                     new string[] { "ssmmhh" },
                    new CultureInfo("en-US"),
                    DateTimeStyles.None,
                    out dt2))
  {
    Console.WriteLine(dt2.ToShortTimeString());
  }

  Console.WriteLine(dt1.ToShortDateString() + " " + dt2.ToShortTimeString());
  Console.ReadLine();

1 Comment

Problem with this code is that there is no way to recognize time is AM or PM.
0

The format was simply incorrect. The timestamp value given doesn't clearly indicate where the hours are since all values (hours, minutes, and seconds) are less than 24. The following code works correctly.

DateTime.TryParseExact(value,
                       "yyyyMMddTHHmmss.fffZ",
                       CultureInfo.InvariantCulture,
                       DateTimeStyles.None,
                       out dt)

Given this proprietary format, the hours are in 24 hour format and come first. A test from this morning yielded the following value: 20131106T162733.032Z. I am able to test this proprietary format because we work for the same company. :)

Comments

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.