简体   繁体   中英

Parse a string value to DateTime object?

I have a text box that accepts DataTime string in format: MM/dd/yyyy hh:mm:ss. For example :

Let's say I have selected a datetime : "12/26/2013 17:37:03"

I am trying to get the Datetime object using:

DateTime.TryParseExact(strDate, "MM/dd/yyyy hh:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None,out date);

But every time it parses the string to:{ 1/1/0001 12:00:00 AM }

Please anyone correct me here.

hh is for 01 to 12 .

Use HH instead which is for 00 to 23 .

For example;

string s = "12/26/2013 17:37:03";
DateTime dt = DateTime.Now;
bool success = DateTime.TryParseExact(s, "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt);
Console.WriteLine("Is Parsing Successful? {0}", success);
Console.WriteLine(dt);

Output will be;

Is Parsing Successful? True
12/26/2013 5:37:03 PM

Here a demonstration .

But even if you parsing, why still you get a DateTime value?

From DateTime.TryParseExact method

If date, time, and time zone elements are present in s, they must also appear in the order specified by format. If format defines a date with no time element and the parse operation succeeds, the resulting DateTime value has a time of midnight (00:00:00).

NOTE : Please read ken2k 's comments: 1 and 2 . When you define your date with a default value like DateTime date ; You can't know your conversion is successful or not because since these both generate the default value of DateTime ( 1/1/0001 12:00:00 AM )

  • DateTime date makes date as 1/1/0001 12:00:00 AM
  • If your parsing fails, date will be also 1/1/0001 12:00:00 AM

That's why, I change it in my code to DateTime dt = DateTime.Now; which you can check whether your converssion is successful or not. If it is successful, your date will be 12/26/2013 17:37:03 , if it is not, your date will be 1/1/0001 12:00:00 AM

Apart from the hour issue that other people have already noted. Why use invariant culture? The datetime in your example is US.

Your time is in 24 hour format. Therefore, you need to use HH(used for 24 hour format) instead of hh (used for 12 hour format).

DateTime.TryParseExact(strDate, "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None,out date);

But every time it parses the string to:{1/1/0001 12:00:00 AM}

It means that your parsing failed. You need to check the result of parsing before proceeding further. You can do it like below.

if(DateTime.TryParseExact(strDate, "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None,out date))
{
     //Parsing successful
}
else
{
     //Parsing failed
}

Here the Answer:

var TimeIn = Application.Current.Properties["date"].ToString();
DateTime InTime = DateTime.ParseExact(TimeIn, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM