-3

I have a string and I want to convert it from string to datetime. When I'm trying its giving exception. Please suggest me how to do.

string stime = "2014-02-02T24:00:00";
DateTime dtStartDateTime = Convert.ToDateTime(stime);
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
user3264676
  • 253
  • 5
  • 8
  • 20
  • `DateTime d = DateTime.Parse("your string goes here")l` – abatishchev Feb 27 '14 at 06:33
  • getting same exception. – user3264676 Feb 27 '14 at 06:36
  • @user3264676 Why did you accept the wrong answer? Take a look at my answer also http://stackoverflow.com/a/22061641/447156 – Soner Gönül Feb 27 '14 at 07:19
  • hello @SonerGönül let him decide! I agree noticing the '24' but what ever he posted its just a sample date now please dont say that todays date is also wrong..! Looking towards his score he is newbee...right? And he has already mentioned that it "throws exception" hence to prevent exception the solution i have given.. – SHEKHAR SHETE Feb 27 '14 at 08:11
  • @SHEKHARSHETE Of course his decide but Stack Overflow is not a place people can get help one by one. **Stack Overflow is for Internet**. In the future, people can find this question after a search. And when they found, they should see the right answer. OP should not be select the wrong answer at all. His string includes `T` character for example, but your answer does not. That's makes huge difference between your answer and question. – Soner Gönül Feb 27 '14 at 11:51
  • edited my answer @SonerGönül as per your requirement and tested too..and it works! :) – SHEKHAR SHETE Feb 27 '14 at 12:04
  • @SHEKHARSHETE Your answer still doesn't answer the _how to parse_ part.. – Soner Gönül Feb 27 '14 at 12:25

2 Answers2

1

This is useful—it does the same thing as DateTime.Parse, BUT

DOES NOT THROW ANY EXCEPTIONS

It returns true if the parse succeeded, and false otherwise.

protected  void Foo()
    {
        // Use DateTime.TryParse when input is valid.
        string input = "2014-02-02T04:00:00";//"2014-02-02";
        DateTime dateTime;
        if (DateTime.TryParse(input, out dateTime))
        {
            lblresult.Text = dateTime.ToString();
        }
        else {
            lblresult.Text = "invalid";
        }
}

See Here

SHEKHAR SHETE
  • 5,964
  • 15
  • 85
  • 143
-1

All answers are wrong, unfortunately. I'm really shocked no one even tested their examples.

.NET doesn't support 24 as an hour. It is not possible to parse your string with DateTime.ParseExact method. But NodaTime does that. It can parse the value but value parsed as next day's midnight. That's probably because there is no standart 24:00 as an hour.

From Wikipedia;

In the 24-hour time notation, the day begins at midnight, 00:00, and the last minute of the day begins at 23:59. Where convenient, the notation 24:00 may also be used to refer to midnight at the end of a given date – that is, 24:00 of one day is the same time as 00:00 of the following day.

I can't install NodaTime right now I installed NodaTime and but this example probably work works..

using System;
using NodaTime;
using NodaTime.Text;
using System.Xml;

class Test
{
    static void Main()
    {
        string s = "2014-02-02T24:00:00";
        var pattern = LocalDateTimePattern.CreateWithInvariantCulture
             ("yyyy-MM-dd'T'HH:mm:ss");
        var dt = pattern.Parse(s).Value;
        Console.WriteLine(pattern.Format(dt)); // 2014-02-03T00:00:00
    }
}
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364