转换AM / PM时间为24小时格式? [英] Convert AM/PM time to 24 hours format?

查看:502
本文介绍了转换AM / PM时间为24小时格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要12小时的时间格式(AM / PM)转换为24小时格式的时间,例如下午1时至13:00使用C#。我该如何转换呢?

I need to convert 12 hours format time (am/pm) to 24 hours format time, e.g. 01:00 PM to 13:00 using C#. How can I convert it?

推荐答案

如果您需要将字符串转换为DateTime,你可以尝试

If you need to convert a string to a DateTime you could try

DateTime dt = DateTime.Parse("01:00 PM"); // No error checking

或(错误检查)

DateTime dt;
bool res = DateTime.TryParse("01:00 PM", out dt);



变量 DT 包含您的日期时间,所以你可以写

Variable dt contains your datetime, so you can write it

dt.ToString("HH:mm");



最后一个为你的作品有,所以如果你仍然有日期时间,你可以写每一个日期时间VAR它以这种方式。

Last one works for every DateTime var you have, so if you still have a DateTime, you can write it out in this way.

这篇关于转换AM / PM时间为24小时格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆