使用c#将字符串转换为时间跨度?
我需要将字符串转换为timespan,为了实现这一点,我使用了以下转换:使用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,我有什么办法可以做到这一点吗 使用以下代码
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
而不是使用该方法使用simpleParse
方法。但是为了按时间进行转换,请使用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哦事实上是的,它工作得很好,我的坏我没有仔细调试,谢谢。