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

使用c#将字符串转换为时间跨度?

使用c#将字符串转换为时间跨度?,c#,.net,timespan,C#,.net,Timespan,我需要将字符串转换为timespan,为了实现这一点,我使用了以下转换: DateTime time = DateTime.ParseExact("12:00 AM", "h:mm tt", CultureInfo.InvariantCulture); TimeSpan timeSpan = time .TimeOfDay; 除了上午12:00,其他时间都很好,上午/下午都是12:00,下午12:00也可以,但我需要12:00上午作为00:00:00,我有什么办法可以做到这一点吗 使用以下代码

我需要将字符串转换为timespan,为了实现这一点,我使用了以下转换:

DateTime time = DateTime.ParseExact("12:00 AM", "h:mm tt", CultureInfo.InvariantCulture);
TimeSpan timeSpan = time .TimeOfDay;
除了上午12:00,其他时间都很好,上午/下午都是12:00,下午12:00也可以,但我需要12:00上午作为00:00:00,我有什么办法可以做到这一点吗

使用以下代码:

DateTime time = DateTime.ParseExact("00:00", "hh:mm", CultureInfo.InvariantCulture);
有关更多选项,请选中此项,使用以下代码:

DateTime time = DateTime.ParseExact("00:00", "hh:mm", CultureInfo.InvariantCulture);
有关更多选项,请选中此项,使用以下代码:

DateTime time = DateTime.ParseExact("00:00", "hh:mm", CultureInfo.InvariantCulture);
有关更多选项,请选中此项,使用以下代码:

DateTime time = DateTime.ParseExact("00:00", "hh:mm", CultureInfo.InvariantCulture);

有关更多选项,请选中此

您不需要使用
ParseExact
而不是使用该方法使用simple
Parse
方法。但是为了按时间进行转换,请使用
HH:mm
格式的
ToString
方法

DateTime timeAM = DateTime.Parse("12:00 AM", CultureInfo.InvariantCulture);
DateTime timePM = DateTime.Parse("12:00 PM", CultureInfo.InvariantCulture);
Console.WriteLine(timeAM.ToString("HH:mm"));
Console.WriteLine(timePM.ToString("HH:mm"));
返回类似以下内容的结果:

//00:00
//12:00

您不需要使用
ParseExact
而不是使用简单的
Parse
方法。但是为了按时间进行转换,请使用
HH:mm
格式的
ToString
方法

DateTime timeAM = DateTime.Parse("12:00 AM", CultureInfo.InvariantCulture);
DateTime timePM = DateTime.Parse("12:00 PM", CultureInfo.InvariantCulture);
Console.WriteLine(timeAM.ToString("HH:mm"));
Console.WriteLine(timePM.ToString("HH:mm"));
返回类似以下内容的结果:

//00:00
//12:00

您不需要使用
ParseExact
而不是使用简单的
Parse
方法。但是为了按时间进行转换,请使用
HH:mm
格式的
ToString
方法

DateTime timeAM = DateTime.Parse("12:00 AM", CultureInfo.InvariantCulture);
DateTime timePM = DateTime.Parse("12:00 PM", CultureInfo.InvariantCulture);
Console.WriteLine(timeAM.ToString("HH:mm"));
Console.WriteLine(timePM.ToString("HH:mm"));
返回类似以下内容的结果:

//00:00
//12:00

您不需要使用
ParseExact
而不是使用简单的
Parse
方法。但是为了按时间进行转换,请使用
HH:mm
格式的
ToString
方法

DateTime timeAM = DateTime.Parse("12:00 AM", CultureInfo.InvariantCulture);
DateTime timePM = DateTime.Parse("12:00 PM", CultureInfo.InvariantCulture);
Console.WriteLine(timeAM.ToString("HH:mm"));
Console.WriteLine(timePM.ToString("HH:mm"));
返回类似以下内容的结果:

//00:00
//12:00

您的代码为我提供
00:00:00
。12:00am=00:00:00,12:00pm=12:00:00作为well@SLaks哦,事实上是的,它工作得很好,我的错,我没有仔细调试,谢谢。你的代码为我提供了
00:00:00
。12:00 AM=00:00:00,12:00 PM=12:00:00,我也是well@SLaks哦,实际上是的,它工作得很好,我的错,我没有仔细调试,谢谢。你的代码为我提供了
00:00:00
。12:00 AM=00:00:00,12:00 PM=12:00:00我也是well@SLaks哦,事实上是的,它工作得很好,我的错,我没有仔细调试,谢谢。你的代码为我提供了
00:00:00
。12:00 AM=00:00:00,12:00 PM=12:00:00,我也是well@SLaks哦事实上是的,它工作得很好,我的坏我没有仔细调试,谢谢。