如何在下午不在AM中获取日期时间 [英] How to get datetime in am not in pm

查看:65
本文介绍了如何在下午不在AM中获取日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

dDeleveryTimeAUS =新的DateTime(年,月,日,dHrs,dMins,dSec);

它显示日期格式为"2012年1月17日下午12:00:00"

但我需要与"AM"相同的dateformat.

dDeleveryTimeAUS = new DateTime(Year, Month, Day, dHrs, dMins,dSec);

It displaying date format as "17/1/2012 12:00:00 PM"

but I need same dateformat as "AM"

推荐答案

"t"自定义格式说明符
-------------------------------------------------- ------------------------------

"t"自定义格式说明符表示AM/PM标志符的第一个字符.从当前或特定区域性的DateTimeFormatInfo.AMDesignator或DateTimeFormatInfo.PMDesignator属性中检索适当的本地化指示符.从0:00:00(午夜)到11:59:59.999的所有时间都使用AM指示符.从12:00:00(中午)到23:59:59.99的所有时间都使用PM指示符.

如果在不使用其他自定义格式说明符的情况下使用"t"格式说明符,则将其解释为"t"标准日期和时间格式说明符.有关使用单一格式说明符的详细信息,请参阅本主题后面的使用单一自定义格式说明符".

以下示例在自定义格式字符串中包含"t"自定义格式说明符.


The "t" Custom Format Specifier
--------------------------------------------------------------------------------

The "t" custom format specifier represents the first character of the AM/PM designator. The appropriate localized designator is retrieved from the DateTimeFormatInfo.AMDesignator or DateTimeFormatInfo.PMDesignator property of the current or specific culture. The AM designator is used for all times from 0:00:00 (midnight) to 11:59:59.999. The PM designator is used for all times from 12:00:00 (noon) to 23:59:59.99.

If the "t" format specifier is used without other custom format specifiers, it is interpreted as the "t" standard date and time format specifier. For more information about using a single format specifier, see Using Single Custom Format Specifiers later in this topic.

The following example includes the "t" custom format specifier in a custom format string.


DateTime date1; 
date1 = new DateTime(2008, 1, 1, 18, 9, 1);
Console.WriteLine(date1.ToString("h:m:s.F t", 
                  CultureInfo.InvariantCulture));
// Displays 6:9:1 P
Console.WriteLine(date1.ToString("h:m:s.F t", 
                  CultureInfo.CreateSpecificCulture("el-GR")));
// Displays 6:9:1 µ                        
date1 = new DateTime(2008, 1, 1, 18, 9, 1, 500);
Console.WriteLine(date1.ToString("h:m:s.F t", 
                  CultureInfo.InvariantCulture));
// Displays 6:9:1.5 P
Console.WriteLine(date1.ToString("h:m:s.F t", 
                  CultureInfo.CreateSpecificCulture("el-GR")));
// Displays 6:9:1.5 µ




谢谢,
Mamun




Thanks,
Mamun


var dDeleveryTimeAUS = new DateTime(2012, 2, 1, 12, 0, 0).ToString(CultureInfo.GetCultureInfo("en-us")); 


将返回2/1/2012 12:00:00 PM


will return 2/1/2012 12:00:00 PM

var dDeleveryTimeAUS = new DateTime(2012, 2, 1, 0, 0, 0).ToString(CultureInfo.GetCultureInfo("en-us")); 


将返回2012年2月1日12:00:00 AM


will return 2/1/2012 12:00:00 AM


您可以使用

you can use

Console.WriteLine(DateTime.Now.ToString("MM/dd/yyyy hh:mm tt"));




or

DateTime date1 = new DateTime();
Console.WriteLine(date1.ToString("MM/dd/yyyy hh:mm tt"));


这篇关于如何在下午不在AM中获取日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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