如何在C#中将字符串转换为特定格式的日期时间?
我正在尝试将“1/7/2014 1:37 PM”(这是一个字符串)转换为“01/07/2014 13:37”(日期时间格式)。最好的方法是什么 这就是我得到的如何在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,
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
,因为时间是12小时,而不是24小时h
- 一个
而不是两个,因为你没有一个小时的前导零h
- 一个
而不是两个,因为您在该月没有前导零M
匹配AM或PMtt
- 小写
而不是h
,因为时间是12小时,而不是24小时h
- 一个
而不是两个,因为你没有一个小时的前导零h
- 一个
而不是两个,因为您在该月没有前导零M
匹配AM或PMtt
Datetime dtDate = DateTime.PareExact(dt,"DateFormat of dt",cultureInfo.InavriantCulture);
你可以用
Datetime dtDate = DateTime.PareExact(dt,"DateFormat of dt",cultureInfo.InavriantCulture);
要改变的四件事:
h
表示数小时1
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.
)
请参阅:要更改的四件事:
h
表示数小时1
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
的显示方式完全取决于您编写的将其转换为字符串的代码(或者