将12小时格式的字符串转换为24小时格式 [英] Convert 12 hour format of string to 24 hour format

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

问题描述

我有两个代表12小时时间的字符串,

表示上午10点和下午5点。

为了比较它们我需要将字符串的格式转换为24小时时钟格式。

经过比较,再次将时间转换回12小时时钟格式。

c#

解决方案

< blockquote>

 DateTime d = DateTime.Now; 
// 12小时格式
string aaa = d.ToString( hh:mm);
// 24小时格式
string aa = d.ToString( H:mm);



快乐编码!

:)


使用此字符串将24小时格式转换为12小时格式:< br $>


string 12hrFormat = DateTime.Today.ToString(dd-MM-yyyy)++ DateTime.Today.ToString(hh:mm tt);


类似

  string  [] s = {< span class =code-string>  10AM  5PM}; 
DateTime [] h = {DateTime.Parse(s [ 0 ]),DateTime.Parse(s [ 1 ])};

if (h [ 0 ] > h [ 1 ])
Console.WriteLine( {0}晚于{1},s [ 0 ],s [ 1 ]);
else
Console.WriteLine( {0}早于{1},s [ 0 ],s [ 1 < /跨度>]);




I have two strings representing 12 hour clock time,
say 10AM and 5PM.
In order to compare them i need to convert the format of the string to 24 hour clock format.
After comparison, again convert the timings back to 12 hour clock format.
c#

解决方案

DateTime d = DateTime.Now;
            // 12 hour format
            string aaa = d.ToString("hh:mm");
            // 24 hour format
             string aa =d.ToString("H:mm");


Happy Coding!
:)


use this string to convert 24 hour format to 12 hour format:

string 12hrFormat = DateTime.Today.ToString("dd-MM-yyyy") + " " + DateTime.Today.ToString("hh:mm tt");


Something like

string [] s ={"10AM", "5PM"};
DateTime [] h = {DateTime.Parse(s[0]), DateTime.Parse(s[1])};

if (h[0] > h[1])
  Console.WriteLine("{0} is later than {1}", s[0], s[1]);
else
  Console.WriteLine("{0} is earlier than {1}", s[0], s[1]);


?


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

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