获得时间领先0 [英] Get Time with leading 0

查看:101
本文介绍了获得时间领先0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



恐慌随着我休息了2天,无论如何我正在做

 TimeOnly = 字符串 .Format(  {0 }:{1},DateTime.Now.Hour,DateAndTime.Now.Minute.ToString)





这给出Windows报告的时间即14:25,但是当时间是09:56,就像现在它返回9:56我需要一种重新格式化时间的方法,所以它有一个领先的0所以9:56

变为09:56我在C#中做过类似的事情,用

DateTime.Now.ToString(hh)或DateTime.Now.ToString(HH)



 TimeOnly = 字符串 .Format(  {hh}:{mm},DateTime.Now.Hour,DateAndTime.Now.Minute.ToString)



给出:time is hh:mm如果没有声明DateTime.Now.Hour.ToString(hh),则相同。 br />
除了

 如果(DateAndTime。 Now.Hour<  10 然后 

不存在?

解决方案

这个怎么样?



 string timeOnly = DateTime.Now.ToString(hh:mm ); 




 TimeOnly = DateTime.Now.ToString(  hh:mm




Hi All,

Panic has ensued while I have had 2 days off, Anyway I am doing

TimeOnly = String.Format("{0}:{1}", DateTime.Now.Hour, DateAndTime.Now.Minute.ToString)



This gives the time reported by Windows i.e. 14:25 but when the time is say 09:56 like it is now it returns 9:56 I need a method of re formatting the time so it has a leading 0 so 9:56
becomes 09:56 I have done similar in the past in C# with
DateTime.Now.ToString("hh") or DateTime.Now.ToString("HH")

TimeOnly = String.Format("{hh}:{mm}", DateTime.Now.Hour, DateAndTime.Now.Minute.ToString)


gives: "time is hh:mm" the same if DateTime.Now.Hour.ToString(hh) is not declared.
There has to be a more elegant solution than

if (DateAndTime.Now.Hour < 10) Then 

doesn't there?

解决方案

What about this?

string timeOnly = DateTime.Now.ToString("hh:mm");


What's wrong with

TimeOnly = DateTime.Now.ToString("hh:mm")


?


这篇关于获得时间领先0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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