您的位置:首页 >看世界 >

datetime格式怎么写(datetime格式)

导读 您好,今天小编胡舒来为大家解答以上的问题。datetime格式怎么写,datetime格式相信很多小伙伴还不知道,现在让我们一起来看看吧!1、1 默

您好,今天小编胡舒来为大家解答以上的问题。datetime格式怎么写,datetime格式相信很多小伙伴还不知道,现在让我们一起来看看吧!

1、1. 默认情况下,DateTime.Now.ToString()的输出与Control Panel中Date/Time的设置格式相关。

2、For example, 当Regional Options中Time设置:Time format: h:mm:ss ttAM symbol: 上午PM symbol:下午 Console.WriteLine(DateTime.Now.ToString());输出结果:12/6/2004 2:37:37 下午 DateTime.Parse("12/6/2004 2:37:37 下午")OK // 将日期和时间的指定 String 表示形式转换成其等效的 SqlDateTimeSqlDateTime.Parse("12/6/2004 2:37:37 下午")Exception:String was not recognized as a valid DateTime. SqlDateTime.Parse("12/6/2004 2:37:37 PM")OK 2. 通过DateTime.ToString(string format)方法,使用指定格式format将此实例的值转换成其等效的字符串表示。

3、DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss")输出结果:12/06/2004 14:56:37此时,DateTime的输出格式由format参数控制,与Regional Options中的Date/Time的设置无关。

4、不过,如果项目中有很多地方需要进行DateTime日期时间格式控制,这样写起来就比较麻烦,虽然可以通过常数const进行控制。

5、 3. 为当前线程的区域性创建 DateTimeFormatInfo。

6、// Sets the CurrentCulture property to U.S. English.System.Threading.Thread.CurrentThread.CurrentCulture = newCultureInfo("en-US", false); Console.WriteLine(DateTime.Now.ToString());输出结果:12/6/2004 2:37:37 PM 若要为特定区域性创建 DateTimeFormatInfo,请为该区域性创建 CultureInfo 并检索 CultureInfo.DateTimeFormat 属性。

7、// Creates and initializes a DateTimeFormatInfo associated with the en-US culture.DateTimeFormatInfo myDTFI = new CultureInfo( "en-US", false).DateTimeFormat; DateTimeFormatInfo 的实例可以针对特定区域性或固定区域性创建,但不能针对非特定区域性创建。

8、非特定区域性不提供显示正确日期格式所需的足够信息。

本文就为大家分享到这里,希望小伙伴们会喜欢。

免责声明:本文由用户上传,如有侵权请联系删除!