显示时间的格式很多,现总结如下:
Format |
Description |
Example |
d |
Short date |
4/11/2008 |
D |
Long date |
Friday, April 11, 2008 |
t |
Short time |
4:46 PM |
T |
Long time |
4:48:27 PM |
f |
Long date & short time |
Friday, April 11, 2008 4:48 PM |
F |
Long date & long time |
Friday, April 11, 2008 4:50:02 PM |
g |
Short date & short time |
4/11/2008 4:53 PM |
G |
Short date & long time |
4/11/2008 4:53:49 PM |
M |
Month |
April 11 |
R |
RFC1123 |
Fri, 11 Apr 2008 16:55:07 GMT |
s |
Sortable |
2008-04-11T16:56:28 |
u |
Universal sortable(invariant) |
2008-04-11 16:57:03Z |
U |
Universal sortable |
Friday, April 11, 2008 8:58:28 AM |
Y |
year |
April, 2008 |
o |
Round trip(local) |
2008-04-11T17:00:47.5986364+08:00 |
dd | day in month | 14(the full date:2008-4-14) |
ddd | day in week, display in three characters | Mon(the full date:2008-4-14) |
dddd | day in week, display in full name. | Monday(the full date:2008-4-14) |
hh | hour, 0~12 | 06 |
HH | hour, 0~23 | 18 |
d MMMM yyyy | 16 April 2008 | |
convert to GMT Time
string date = "2009-02-25 16:13:00Z"; // Coordinated Universal Time string from DateTime.Now.ToUniversalTime().ToString("u");
DateTime localDateTime = DateTime.Parse(date); // Local .NET timeZone.
DateTime utcDateTime = localDateTime.ToUniversalTime();
// ID from "HKEY_LOCAL_MACHINE"Software"Microsoft"Windows NT"CurrentVersion"Time Zone"
// See http://msdn.microsoft.com/en-us/library/system.timezoneinfo.id.aspx
string nzTimeZoneKey = "New Zealand Standard Time";
TimeZoneInfo nzTimeZone = TimeZoneInfo.FindSystemTimeZoneById(nzTimeZoneKey);
DateTime nzDateTime = TimeZoneInfo.ConvertTimeFromUtc(utcDateTime, nzTimeZone);
2> DateTime 方法:
判断一个月有几天: DateTime.DayInMonth(int iyear, int imonth)
3>sqlserver date 处理
select CONVERT(varchar, getdate(), 120 )
2004-09-12 11:06:08
select replace(replace(replace(CONVERT(varchar, getdate(), 120 ),"'-"',"'"'),"' "',"'"'),"':"',"'"')
20040912110608
select CONVERT(varchar(12) , getdate(), 111 )
2004/09/12
select CONVERT(varchar(12) , getdate(), 112 )
20040912
select CONVERT(varchar(12) , getdate(), 102 )
2004.09.12
select CONVERT(varchar(12) , getdate(), 101 )
09/12/2004
select CONVERT(varchar(12) , getdate(), 103 )
12/09/2004
select CONVERT(varchar(12) , getdate(), 104 )
12.09.2004
select CONVERT(varchar(12) , getdate(), 105 )
12-09-2004
select CONVERT(varchar(12) , getdate(), 106 )
12 09 2004
select CONVERT(varchar(12) , getdate(), 107 )
09 12, 2004
select CONVERT(varchar(12) , getdate(), 108 )
11:06:08
select CONVERT(varchar(12) , getdate(), 109 )
09 12 2004 1
select CONVERT(varchar(12) , getdate(), 110 )
09-12-2004
select CONVERT(varchar(12) , getdate(), 113 )
12 09 2004 1
select CONVERT(varchar(12) , getdate(), 114 )
11:06:08.177