首页 > 开发 > 综合 > 正文

日期和时间的转换

2024-07-21 02:16:37
字体:
来源:转载
供稿:网友
,欢迎访问网页设计爱好者web开发。the datetime data type also contains a format method and a tostring method that return a string-formatted representation of the original value. the format method can be used to format a value in one of fifteen common ways, while the tostring method can only be used to format a value in one way.

the format method takes the following form, where xxx is the name of the numeric base data-type:

virtual xxx format(string* format,iserviceobjectprovider* sp)
similar to the format methods found in other .net framework data types, the datetime format method takes a string format character and an iserviceobjectprovider object. the iserviceobjectprovider object specifies the culture. the method defaults to the current culture if null (in visual basic nothing) is passed.

the following table shows the valid format strings.


format character description default return format
d short date pattern mm/dd/yyyy
d long date pattern dddd, mmmm dd, yyyy
f full (long date + short time) dddd, mmmm dd, yyyy hh:mm
f full date time pattern (long date + long time) dddd, mmmm dd, yyyy hh:mm:ss
g general (short date + short time) mm/dd/yyyy hh:mm
g general (short date + long time) mm/dd/yyyy hh:mm:ss
m,m month day pattern mmmm dd
r,r rfc1123 pattern  ddd, dd mmm yyyy hh':'mm':'ss'gmt'
s sortable date time pattern: conforms to iso 8601 yyyy-mm-dd hh:mm:ss
t short time pattern hh:mm
t long time pattern hh:mm:ss
u similar to "s"but uses universal time instead of local time. yyyy-mm-dd hh:mm:ss
u universal sortable date time pattern dddd, mmmm dd, yyyy hh:mm:ss
y,y year month pattern mmmm, yyyy


given a datetime object, mydate, representing 12:01 am sunday, january 1, 2000, the following example illustrates some of the formatting options available:

[c#]
mydate.format( "d", null );
// returns the string "01/01/2000"

mydate.format( "d", null );
// returns the string "sunday, january 1, 2000"

mydate.format( "f", null );
// returns the string "sunday january 1, 2000 12:01 am"
the tostring method will quickly convert a datetime type value into a string. similar to other .net framework base types, it requires no arguments and is easy to use. unlike the format method, the tostring method is unaffected by the current culture and only takes one form. the following example converts a datetime value into a string value:

[c#]

datetime mydate = new datetime(2000, 01, 01)
mydate.tostring();
//returns "01/01/2000 00:00:00"


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表