如何处理datetime的时间格式
答案:3 悬赏:50
解决时间 2021-02-11 10:08
- 提问者网友:离殇似水流年飞逝
- 2021-02-11 04:06
如何处理datetime的时间格式
最佳答案
- 二级知识专家网友:一池湖水
- 2021-02-11 04:36
在.Net Framework 1.1平台下,可以这样处理日期时间格式。
1. 默认情况下,DateTime.Now.ToString()的输出与Control Panel中Date/Time的设置格式相关。
For example, 当Regional Options中Time设置:
Time format: h:mm:ss tt
AM symbol: 上午
PM symbol:下午
Console.WriteLine(DateTime.Now.ToString());
输出结果:12/6/2004 2:37:37 下午
DateTime.Parse("12/6/2004 2:37:37 下午")
OK
// 将日期和时间的指定 String 表示形式转换成其等效的 SqlDateTime
SqlDateTime.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将此实例的值转换成其等效的字符串表示。
DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss")
输出结果:12/06/2004 14:56:37
此时,DateTime的输出格式由format参数控制,与Regional Options中的Date/Time的设置无关。不过,如果项目中有很多地方需要进行DateTime日期时间格式控制,这样写起来就比较麻烦,虽然可以通过常数const进行控制。
3. 为当前线程的区域性创建 DateTimeFormatInfo。
// Sets the CurrentCulture property to U.S. English.
System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US", false);
Console.WriteLine(DateTime.Now.ToString());
输出结果:12/6/2004 2:37:37 PM
若要为特定区域性创建 DateTimeFormatInfo,请为该区域性创建 CultureInfo 并检索 CultureInfo.DateTimeFormat 属性。
// Creates and initializes a DateTimeFormatInfo associated with the en-US culture.
DateTimeFormatInfo myDTFI = new CultureInfo( "en-US", false).DateTimeFormat;
DateTimeFormatInfo 的实例可以针对特定区域性或固定区域性创建,但不能针对非特定区域性创建。非特定区域性不提供显示正确日期格式所需的足够信息。如果试图使用非特定区域性创建 DateTimeFormatInfo 的实例,将发生异常
1. 默认情况下,DateTime.Now.ToString()的输出与Control Panel中Date/Time的设置格式相关。
For example, 当Regional Options中Time设置:
Time format: h:mm:ss tt
AM symbol: 上午
PM symbol:下午
Console.WriteLine(DateTime.Now.ToString());
输出结果:12/6/2004 2:37:37 下午
DateTime.Parse("12/6/2004 2:37:37 下午")
OK
// 将日期和时间的指定 String 表示形式转换成其等效的 SqlDateTime
SqlDateTime.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将此实例的值转换成其等效的字符串表示。
DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss")
输出结果:12/06/2004 14:56:37
此时,DateTime的输出格式由format参数控制,与Regional Options中的Date/Time的设置无关。不过,如果项目中有很多地方需要进行DateTime日期时间格式控制,这样写起来就比较麻烦,虽然可以通过常数const进行控制。
3. 为当前线程的区域性创建 DateTimeFormatInfo。
// Sets the CurrentCulture property to U.S. English.
System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US", false);
Console.WriteLine(DateTime.Now.ToString());
输出结果:12/6/2004 2:37:37 PM
若要为特定区域性创建 DateTimeFormatInfo,请为该区域性创建 CultureInfo 并检索 CultureInfo.DateTimeFormat 属性。
// Creates and initializes a DateTimeFormatInfo associated with the en-US culture.
DateTimeFormatInfo myDTFI = new CultureInfo( "en-US", false).DateTimeFormat;
DateTimeFormatInfo 的实例可以针对特定区域性或固定区域性创建,但不能针对非特定区域性创建。非特定区域性不提供显示正确日期格式所需的足够信息。如果试图使用非特定区域性创建 DateTimeFormatInfo 的实例,将发生异常
全部回答
- 1楼网友:冷态度
- 2021-02-11 06:38
在.Net Framework 1.1平台下,从个人体验谈谈如何处理日期时间格式。
1. 默认情况下,DateTime.Now.ToString()的输出与Control Panel中Date/Time的设置格式相关。
For example, 当Regional Options中Time设置:
Time format: h:mm:ss tt
AM symbol: 上午
PM symbol:下午
Console.WriteLine(DateTime.Now.ToString());
输出结果:12/6/2004 2:37:37 下午
DateTime.Parse("12/6/2004 2:37:37 下午")
OK
// 将日期和时间的指定 String 表示形式转换成其等效的 SqlDateTime
SqlDateTime.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将此实例的值转换成其等效的字符串表示。
DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss")
输出结果:12/06/2004 14:56:37
此时,DateTime的输出格式由format参数控制,与Regional Options中的Date/Time的设置无关。不过,如果项目中有很多地方需要进行DateTime日期时间格式控制,这样写起来就比较麻烦,虽然可以通过常数const进行控制。
3. 为当前线程的区域性创建 DateTimeFormatInfo。
// Sets the CurrentCulture property to U.S. English.
System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US", false);
Console.WriteLine(DateTime.Now.ToString());
输出结果:12/6/2004 2:37:37 PM
若要为特定区域性创建 DateTimeFormatInfo,请为该区域性创建 CultureInfo 并检索 CultureInfo.DateTimeFormat 属性。
// Creates and initializes a DateTimeFormatInfo associated with the en-US culture.
DateTimeFormatInfo myDTFI = new CultureInfo( "en-US", false).DateTimeFormat;
DateTimeFormatInfo 的实例可以针对特定区域性或固定区域性创建,但不能针对非特定区域性创建。非特定区域性不提供显示正确日期格式所需的足够信息。如果试图使用非特定区域性创建 DateTimeFormatInfo 的实例,将发生异常
- 2楼网友:承载所有颓废
- 2021-02-11 06:09
这个需要用convert转换一下啦
select sum(bo_uddatapickupreport_stsfcmain.finquantity)
from bo_uddatapickupreport_stsfcmain
group by bo_uddatapickupreport_stsfcmain.cinvcode,
convert(varchar(6),bo_uddatapickupreport_stsfcmain.ddate,112)
其实你可以这样看看结果就知道了
select convert(varchar(6),getdate(),112)
varchar(6)这个地方限制长度就是取前六位了
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯