Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何为带有可选时间部分的常规日期格式定义日期时间解析格式?_C#_.net_Datetime_Datetime Format - Fatal编程技术网

C# 如何为带有可选时间部分的常规日期格式定义日期时间解析格式?

C# 如何为带有可选时间部分的常规日期格式定义日期时间解析格式?,c#,.net,datetime,datetime-format,C#,.net,Datetime,Datetime Format,什么是正确的DateTime格式,可以用通用日期格式(“G”)和可选时间部分(“d”)从字符串解析日期 我可以有两种类型的日期: “2012年12月13日下午6:30:00” “3/29/2013” 如何以统一的方式解析它们? 现在我正在尝试使用“G”格式进行解析,如果没有使用“d”格式进行解析。只需使用DateTime.parse(),或者如果您想进行安全的解析尝试DateTime.TryParse() 或 您只需使用DateTime.ParseExact(),或者提供与DateTime.Pa

什么是正确的
DateTime
格式,可以用通用日期格式
(“G”)
和可选时间部分
(“d”)
从字符串解析日期

我可以有两种类型的日期:

  • “2012年12月13日下午6:30:00”
  • “3/29/2013”
  • 如何以统一的方式解析它们?
    现在我正在尝试使用
    “G”
    格式进行解析,如果没有使用
    “d”
    格式进行解析。

    只需使用
    DateTime.parse()
    ,或者如果您想进行安全的解析尝试
    DateTime.TryParse()

    您只需使用
    DateTime.ParseExact()
    ,或者提供与
    DateTime.Parse()
    接受的公认格式不同的格式,或者只允许使用一种特定格式。

    如果您支持
    MM/dd/yyyyy h:MM:ss tt
    (我假设您有
    h
    )和
    M/dd/yyyyyy
    (我假设您有
    M
    )作为标准日期和时间格式,使用可以解决所有问题

    string s = "";
    DateTime dt;
    if (DateTime.TryParse(s, out dt))
    {
        // Your string parsed successfully.
    }
    
    如果这些格式不是您的
    CurrentCulture
    的标准日期和时间格式,则使用可能是最佳选择,因为它将格式作为字符串数组的一部分。这意味着您可以提供多种格式,并且您的字符串将在第一次成功匹配时被解析

    string s = "";
    string[] formats = { "MM/dd/yyyy h:mm:ss tt", "M/dd/yyyy" };
    DateTime dt;
    if (DateTime.TryParseExact(s, formats, CultureInfo.CurrentCulture,
                               DateTimeStyles.None, out dt))
    {
        // Your string parsed with one of speficied format.
    }
    

    在分析具有的字符串时要小心。它具有用当前区域性或指定的区域性日期分隔符替换我的特殊含义。这意味着如果您的
    CurrentCulture
    不是
    /
    ,即使字符串和格式是相同的格式,您的解析操作也会失败。

    只需两次尝试解析-这是一个简单的方法任务。无法在一个解析请求中完成任务?@DreamTeamMobile:你所说的“一个解析请求”到底是什么意思?一个明显的简化是创建一个包含两个TryParse()调用的小方法。另一种可能是一组链接的“if”语句。您只需要一行。将字符串指定给变量并使用DateTime.Parse,我刚刚演示了两种格式都适用于DateTime.Parse。您的
    CurrentCulture
    是什么?
    string s = "";
    DateTime dt;
    if (DateTime.TryParse(s, out dt))
    {
        // Your string parsed successfully.
    }
    
    string s = "";
    string[] formats = { "MM/dd/yyyy h:mm:ss tt", "M/dd/yyyy" };
    DateTime dt;
    if (DateTime.TryParseExact(s, formats, CultureInfo.CurrentCulture,
                               DateTimeStyles.None, out dt))
    {
        // Your string parsed with one of speficied format.
    }