简体   繁体   English

将字符串值解析为DateTime对象?

[英]Parse a string value to DateTime object?

I have a text box that accepts DataTime string in format: MM/dd/yyyy hh:mm:ss. 我有一个文本框,它接受格式为MM / dd / yyyy hh:mm:ss的DataTime字符串。 For example : 例如 :

Let's say I have selected a datetime : "12/26/2013 17:37:03" 假设我选择了一个日期时间: "12/26/2013 17:37:03"

I am trying to get the Datetime object using: 我正在尝试使用以下方法获取Datetime对象:

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 } 但是每次它将字符串解析为:{ 1/1/0001 12:00:00 AM }

Please anyone correct me here. 请任何人在这里纠正我。

hh is for 01 to 12 . hh0112

Use HH instead which is for 00 to 23 . 使用HH代替0023

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 . 这里有demonstration

But even if you parsing, why still you get a DateTime value? 但是,即使您进行了解析,为什么仍要获取DateTime值?

From DateTime.TryParseExact method DateTime.TryParseExact方法

If date, time, and time zone elements are present in s, they must also appear in the order specified by format. 如果日期,时间和时区元素以s出现,则它们也必须以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). 如果format定义了没有时间元素的日期并且解析操作成功, 则所得的DateTime值的时间为午夜(00:00:00)。

NOTE : Please read ken2k 's comments: 1 and 2 . 注意 :请阅读ken2k的注释: 12 When you define your date with a default value like DateTime date ; 当你定义date与像一个默认值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( 1/1/0001 12:00:00 AM

  • DateTime date makes date as 1/1/0001 12:00:00 AM DateTime date将日期设为1/1/0001 12:00:00 AM
  • If your parsing fails, date will be also 1/1/0001 12:00:00 AM 如果解析失败, date也将是1/1/0001 12:00:00 AM

That's why, I change it in my code to DateTime dt = DateTime.Now; 这就是为什么我在代码中将其更改为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 如果成功,则您的date将为12/26/2013 17:37:03 ,如果12/26/2013 17:37:03 ,则您的日期将为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. 您的时间为24小时格式。 Therefore, you need to use HH(used for 24 hour format) instead of hh (used for 12 hour format). 因此,您需要使用HH(用于24小时格式)而不是hh(用于12小时格式)。

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} 但是每次它将字符串解析为:{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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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