Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.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# - Fatal编程技术网

如何在C#中将字符串转换为特定格式的日期时间?

如何在C#中将字符串转换为特定格式的日期时间?,c#,C#,我正在尝试将“1/7/2014 1:37 PM”(这是一个字符串)转换为“01/07/2014 13:37”(日期时间格式)。最好的方法是什么 这就是我得到的 string dateString = "1/27/2014 1:37 PM"; string format = "MM/dd/yyyy HH:mm"; DateTime dt; bool temp = DateTime.TryParseExact(dateString,

我正在尝试将“1/7/2014 1:37 PM”(这是一个字符串)转换为“01/07/2014 13:37”(日期时间格式)。最好的方法是什么

这就是我得到的

        string dateString = "1/27/2014 1:37 PM"; 
        string format = "MM/dd/yyyy HH:mm";
        DateTime dt;
        bool temp = DateTime.TryParseExact(dateString, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dt);
        Console.WriteLine("bool = {0}, dt ={1}", temp, dt);
我的输出是

        bool = False, dt =1/1/0001 12:00:00 AM
非常感谢

编辑:

这不仅仅是针对我指定的字符串。再加上几个例子——LHS应该被精确地解析为RHS

    1/7/2014 1:37 PM -> 01/07/2014 13:37

    11/27/2014 1:40 AM -> 11/27/2014 01:40

    1/12/2014 2:05 PM -> 01/12/2014 14:05
基本上,输入字符串没有前导零,时间为12小时格式,输出应在需要时具有前导零,并应以24小时格式显示时间

我试过给予

    string format = "MM/dd/yyyy HH:mm tt";
但这也会产生同样的错误输出

    bool = False, dt =1/1/0001 12:00:00 AM

你的格式不对。您错过了上午/下午时间:

string format = "M/dd/yyyy h:mm tt";
注意
tt
部分,它是正确解析日期所必需的。此外,我建议您添加错误检查:

string dateString = "1/27/2014 1:37 PM"; 
string format = "M/dd/yyyy h:mm tt";
DateTime dt;
bool temp = DateTime.TryParseExact(dateString, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dt);
if (temp)
{
    Console.WriteLine("bool = {0}, dt = {1:M/dd/yyyy h:mm}", temp, dt);
}
else
{
    Console.WriteLine("Unable to parse date: {0}", dateString);
}

你的格式不对。您错过了上午/下午时间:

string format = "M/dd/yyyy h:mm tt";
注意
tt
部分,它是正确解析日期所必需的。此外,我建议您添加错误检查:

string dateString = "1/27/2014 1:37 PM"; 
string format = "M/dd/yyyy h:mm tt";
DateTime dt;
bool temp = DateTime.TryParseExact(dateString, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dt);
if (temp)
{
    Console.WriteLine("bool = {0}, dt = {1:M/dd/yyyy h:mm}", temp, dt);
}
else
{
    Console.WriteLine("Unable to parse date: {0}", dateString);
}

将日期和时间的指定字符串表示形式转换为其 使用特定于区域性的指定格式的等效日期时间 格式信息和样式字符串表示形式的格式 必须与指定的格式完全匹配。

就你而言,情况并非如此

  • 格式用于
    01
    12
    ,使用
    1
    12
    的格式
  • 格式适用于
    00
    23
    ,请改用
    1
    12
    格式
此外,您还缺少用于
AM/PM
指示器的格式

string str  = "1/27/2014 1:37 PM";
string format = "M/dd/yyyy h:mm tt";
DateTime dt;
bool result = DateTime.TryParseExact(str,
                                     format,
                                     CultureInfo.InvariantCulture,
                                     DateTimeStyles.None, out dt);
Console.WriteLine(result);
输出将是

True
这里有一个。

来自

将日期和时间的指定字符串表示形式转换为其 使用特定于区域性的指定格式的等效日期时间 格式信息和样式字符串表示形式的格式 必须与指定的格式完全匹配。

就你而言,情况并非如此

  • 格式用于
    01
    12
    ,使用
    1
    12
    的格式
  • 格式适用于
    00
    23
    ,请改用
    1
    12
    格式
此外,您还缺少用于
AM/PM
指示器的格式

string str  = "1/27/2014 1:37 PM";
string format = "M/dd/yyyy h:mm tt";
DateTime dt;
bool result = DateTime.TryParseExact(str,
                                     format,
                                     CultureInfo.InvariantCulture,
                                     DateTimeStyles.None, out dt);
Console.WriteLine(result);
输出将是

True
这里是一个。

您应该使用

  • 小写
    h
    而不是
    h
    ,因为时间是12小时,而不是24小时
  • 一个
    h
    而不是两个,因为你没有一个小时的前导零
  • 一个
    M
    而不是两个,因为您在该月没有前导零
  • tt
    匹配AM或PM
您应该使用

  • 小写
    h
    而不是
    h
    ,因为时间是12小时,而不是24小时
  • 一个
    h
    而不是两个,因为你没有一个小时的前导零
  • 一个
    M
    而不是两个,因为您在该月没有前导零
  • tt
    匹配AM或PM
您可以使用

 Datetime dtDate = DateTime.PareExact(dt,"DateFormat of dt",cultureInfo.InavriantCulture);
你可以用

 Datetime dtDate = DateTime.PareExact(dt,"DateFormat of dt",cultureInfo.InavriantCulture);
要改变的四件事:

  • 如果使用am/pm指示符,则需要小写字母
    h
    表示数小时
  • 如果小时数可以是
    1
  • 您需要为am/pm指示器添加
    tt
  • 您需要一个月的
    M
    ,因为它是
    1

  • 见:

    更新:根据您的编辑:

    我还需要将其转换回特定格式的字符串。它应该有前导零,时间是12小时格式,输出应该有前导零(如果需要),并且应该以24小时格式显示时间

    然后您可以在
    DateTime.ToString
    中使用此格式字符串:
    MM/dd/yyyy HH:MM
    CultureInfo.InvariantCulture
    。请注意,我对24小时格式使用了大写字母
    HH

    string output = dt.ToString("MM/dd/yyyy HH:mm", CultureInfo.InvariantCulture);
    
    您需要
    InvariantCulture
    ,否则
    /
    将替换为当前区域性的实际日期分隔符(德国的f.e.

    请参阅:

    要更改的四件事:

  • 如果使用am/pm指示符,则需要小写字母
    h
    表示数小时
  • 如果小时数可以是
    1
  • 您需要为am/pm指示器添加
    tt
  • 您需要一个月的
    M
    ,因为它是
    1

  • 见:

    更新:根据您的编辑:

    我还需要将其转换回特定格式的字符串。它应该有前导零,时间是12小时格式,输出应该有前导零(如果需要),并且应该以24小时格式显示时间

    然后您可以在
    DateTime.ToString
    中使用此格式字符串:
    MM/dd/yyyy HH:MM
    CultureInfo.InvariantCulture
    。请注意,我对24小时格式使用了大写字母
    HH

    string output = dt.ToString("MM/dd/yyyy HH:mm", CultureInfo.InvariantCulture);
    
    您需要
    InvariantCulture
    ,否则
    /
    将替换为当前区域性的实际日期分隔符(德国的f.e.

    请参阅:

    试试这个

     static void Main(string[] args)
                {
                    string dateString = "1/27/2014 1:37 PM";
                    string format = "MM/dd/yyyy HH:mm";
    
    
                    DateTime dt2 = Convert.ToDateTime(dateString);
    
                    Console.WriteLine(dt2.ToString(format));
                    Console.ReadLine();
                }
    
    试试这个

     static void Main(string[] args)
                {
                    string dateString = "1/27/2014 1:37 PM";
                    string format = "MM/dd/yyyy HH:mm";
    
    
                    DateTime dt2 = Convert.ToDateTime(dateString);
    
                    Console.WriteLine(dt2.ToString(format));
                    Console.ReadLine();
                }
    
    这可能有助于:

    DateTime txtmyDate = DateTime.ParseExact(dateString "MM/dd/yyyy hh:mm",     
    CultureInfo.InvariantCulture);
    
    这可能有助于:

    DateTime txtmyDate = DateTime.ParseExact(dateString "MM/dd/yyyy hh:mm",     
    CultureInfo.InvariantCulture);
    

    也许你可以这样试试

    DateTime dt = new DateTime(2008, 3, 9, 16, 5, 7, 123);
    Console.WriteLine(String.Format("{0:MM/dd/yyyy}", dt));
    

    也许你可以这样试试

    DateTime dt = new DateTime(2008, 3, 9, 16, 5, 7, 123);
    Console.WriteLine(String.Format("{0:MM/dd/yyyy}", dt));
    

    一旦转换为
    日期时间
    ,它就没有格式。
    DateTime
    值是自01/01/0001以来发生的100纳秒间隔数的计数,并指示它认为是UTC、本地时间还是不知道。当您将其转换回字符串时,
    DateTime
    的显示方式完全取决于您编写的将其转换为字符串的代码(或者