Net DateTime.ToString大全

1.DateTime.ToString(string format)输出不同格式的日期

      DateTime.ToString()函数有四个重载。一般用得多的就是不带参数的那个了。殊不知,DateTime.ToString(string format)功能更强大,能输出不同格式的日期。以下把一些情况罗列出来,供大家参考。有些在MSDN上有的就没有列出来了。

1.         y代表年份,注意是小写的y,大写的Y并不代表年份。

2.         M表示月份。

3.         d表示日期,注意D并不代表什么。

4.         hH表示小时,h用的是12小时制,H用的是24小时制。

5.         m表示分钟。

6.         s表示秒。注意S并不代表什么。

格式

输出

示例

y

7

string yy = DateTime.Now.ToString("y-MM")

yy="7-05"

yy

07

string yy = DateTime.Now.ToString("yy-MM")

yy="07-05"

yyy或更多的y

1984

string yy = DateTime.Now.ToString("yyyy");

yy="2007"

M

5.

string mon = DateTime.Parse("1984-05-09")ToString("yyyy-M")

mon = "1984-5"

MM

05.

string mon = DateTime.Parse("1984-05-09")ToString("M")

mon = "05"

MMM

如果是中文版的操作系统,则会输出:五月.

如果是英文操作系统,则输入月份前三个字母的简写:May

string mon = DateTime.Parse("2006-07-01").ToString("MMM")

英文版操作系统:Jul

中文版操作系统:七月

MMMM或更多的M

如果是中文版的操作系统,则会输出:五月.

如果是英文操作系统,则输入月份的全写

string mon = DateTime.Parse("2006-07-01").ToString("MMM")

英文版操作系统:July

中文版操作系统:七月

日期或星期

d

9

string dd= DateTime.Parse("1984-05-09")ToString("d")

dd= "9"

 

dd

09

string dd= DateTime.Parse("1984-05-09")ToString("dd")

dd= "09"

ddd

如果是中文版的操作系统,则会输出星期,如星期三。.

如果是英文操作系统,则输出星期的简写:如

Wed

string dd = DateTime.Parse("2006-07-01").ToString("ddd")

英文版操作系统:Wed

中文版操作系统:星期三

dddd或更多的d

如果是中文版的操作系统,则会输出星期,如星期三。.

如果是英文操作系统,则输出星期:如

Wednesday

string dd = DateTime.Parse("2006-07-01").ToString("dddd")

英文版操作系统:Wednesday

中文版操作系统:星期三

小时

h

小时范围:1-12

string hh = DateTime.Now.ToString(“h”);

hh = 8

hh或更多的h

小时范围:1-12

string hh = DateTime.Now.ToString(“hh”);

hh = 08

H

小时范围:0-23

string hh = DateTime.Now.ToString(“yyyy-H”);

hh = 2006-8

HH或更多的H

小时范围:0-23

string hh = DateTime.Now.ToString(“yyyy-HH”);

hh = 2006-08

string hh = DateTime.Pare(“2006-7-4 18:00:00”).ToString(“yyyy-HH”);

hh = 2006-18

分钟

m

6

string mm =  DateTime.Now.ToString("yyyy-MM-dd-m");

mm = “2006-07-01-6”;

mm或更多的m

06

string mm =  DateTime.Now.ToString("yyyy-MM-dd-mm");

mm = “2006-07-01-06”;

s

6

string mm =  DateTime.Now.ToString("yyyy-MM-dd-s");

mm = “2006-07-01-6”;

ss或更多的s

06

string mm =  DateTime.Now.ToString("yyyy-MM-dd-ss");

mm = “2006-07-01-06”;

 

 

 

2. tbody,你注意了吗?

请看下列代码:看完此段代码,先别急着往下看。你知道结果为什么是这样吗?

<html>
<head>parentElement Test</head>
<body>
<form name= "a">       
    <table name= "b ">
    <tr name= "c"   >       
       <td name="d">       
        <select name= "e" οnchange="javascript:alert(this.parentElement.parentElement.parentElement.name) ">
        <option> 1 </option>
        <option> 2 </option>
        <option> 3 </option>
        <option> 4 </option>
        </select>
       </td>    
       <td>   </td>
      </tr>       
      <tr>   </tr>
    </table>       
    </form>  
  </body>
</html>
问题:上述代码中,并没有得出我们预想的b,而是undefined。如果把onchange事件中的代码换 成:javascript:alert(this.parentElement.parentElement.parentElement.parentElement.name) ,此时才得出我们预想的b

请看:
<html>
<head>parentElement Test</head>
<body>
<form name= "a">       
    <table name= "b ">
   
<!--这里的tbody,不论你写上或是不写,默认都是存在的-->
    <tbody name="tbody">    
    <tr name= "c"   >       
       <td name="d">       
        <select name= "e" οnchange= "javascript:alert(this.parentElement.parentElement.parentElement.name) ">
        <option> 1 </option>
        <option> 2 </option>
        <option> 3 </option>
        <option> 4 </option>
        </select>
       </td>    
       <td>   </td>
      </tr>       
      <tr>   </tr>
     </tbody>
    </table>       
    </form>  
  </body>
</html>
还是第一段的代码,只是在<table>标签后加了<tbody>,再运行,弹出提示tbody。正是我们预想的。

3.DateTime.ToString() Patterns

All the patterns:

0

MM/dd/yyyy

08/22/2006

1

dddd, dd MMMM yyyy

Tuesday, 22 August 2006

2

dddd, dd MMMM yyyy

HH:mm Tuesday, 22 August 2006 06:30

3

dddd, dd MMMM yyyy

hh:mm tt Tuesday, 22 August 2006 06:30 AM

4

dddd, dd MMMM yyyy

H:mm Tuesday, 22 August 2006 6:30

5

dddd, dd MMMM yyyy

h:mm tt Tuesday, 22 August 2006 6:30 AM

6

dddd, dd MMMM yyyy HH:mm:ss

Tuesday, 22 August 2006 06:30:07

7

MM/dd/yyyy HH:mm

08/22/2006 06:30

8

MM/dd/yyyy hh:mm tt

08/22/2006 06:30 AM

9

MM/dd/yyyy H:mm

08/22/2006 6:30

10

MM/dd/yyyy h:mm tt

08/22/2006 6:30 AM

10

MM/dd/yyyy h:mm tt

08/22/2006 6:30 AM

10

MM/dd/yyyy h:mm tt

08/22/2006 6:30 AM

11

MM/dd/yyyy HH:mm:ss

08/22/2006 06:30:07

12

MMMM dd

August 22

13

MMMM dd

August 22

14

yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK

2006-08-22T06:30:07.7199222-04:00

15

yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK

2006-08-22T06:30:07.7199222-04:00

16

ddd, dd MMM yyyy HH':'mm':'ss 'GMT'

Tue, 22 Aug 2006 06:30:07 GMT

17

ddd, dd MMM yyyy HH':'mm':'ss 'GMT'

Tue, 22 Aug 2006 06:30:07 GMT

18

yyyy'-'MM'-'dd'T'HH':'mm':'ss

2006-08-22T06:30:07

19

HH:mm

06:30

20

hh:mm tt

06:30 AM

21

H:mm

6:30

22

h:mm tt

6:30 AM

23

HH:mm:ss

06:30:07

24

yyyy'-'MM'-'dd HH':'mm':'ss'Z'

2006-08-22 06:30:07Z

25

dddd, dd MMMM yyyy HH:mm:ss

Tuesday, 22 August 2006 06:30:07

26

yyyy MMMM

2006 August

27

yyyy MMMM

2006 August

The patterns for DateTime.ToString ( 'd' ) :

0

MM/dd/yyyy

08/22/2006

The patterns for DateTime.ToString ( 'D' ) :

0

dddd, dd MMMM yyyy

Tuesday, 22 August 2006

The patterns for DateTime.ToString ( 'f' ) :

0

dddd, dd MMMM yyyy HH:mm

Tuesday, 22 August 2006 06:30

1

dddd, dd MMMM yyyy hh:mm

tt Tuesday, 22 August 2006 06:30 AM

2

dddd, dd MMMM yyyy H:mm

Tuesday, 22 August 2006 6:30

3

dddd, dd MMMM yyyy h:mm

tt Tuesday, 22 August 2006 6:30 AM

The patterns for DateTime.ToString ( 'F' ) :

0

dddd, dd MMMM yyyy HH:mm:ss

Tuesday, 22 August 2006 06:30:07

The patterns for DateTime.ToString ( 'g' ) :

0

MM/dd/yyyy HH:mm

08/22/2006 06:30

1

MM/dd/yyyy hh:mm

tt 08/22/2006 06:30 AM

2

MM/dd/yyyy H:mm

08/22/2006 6:30

3

MM/dd/yyyy h:mm tt

08/22/2006 6:30 AM

The patterns for DateTime.ToString ( 'G' ) :

0

MM/dd/yyyy HH:mm:ss

08/22/2006 06:30:07

The patterns for DateTime.ToString ( 'm' ) :

0

MMMM dd

August 22

The patterns for DateTime.ToString ( 'r' ) :

0

ddd, dd MMM yyyy HH':'mm':'ss 'GMT'

Tue, 22 Aug 2006 06:30:07 GMT

The patterns for DateTime.ToString ( 's' ) :

0

yyyy'-'MM'-'dd'T'HH':'mm':'ss

2006-08-22T06:30:07

The patterns for DateTime.ToString ( 'u' ) :

0

yyyy'-'MM'-'dd HH':'mm':'ss'Z'

2006-08-22 06:30:07Z

The patterns for DateTime.ToString ( 'U' ) :

0

dddd, dd MMMM yyyy HH:mm:ss

Tuesday, 22 August 2006 06:30:07

The patterns for DateTime.ToString ( 'y' ) :

0

yyyy MMMM 2006 August

Building a custom DateTime.ToString Patterns

The following details the meaning of each pattern character. Note the K and z character.

d

Represents the day of the month as a number from 1 through 31. A single-digit day is formatted without a leading zero

dd

Represents the day of the month as a number from 01 through 31. A single-digit day is formatted with a leading zero

ddd

Represents the abbreviated name of the day of the week (Mon, Tues, Wed etc)

dddd

Represents the full name of the day of the week (Monday, Tuesday etc)

h

12-hour clock hour (e.g. 7)

hh

12-hour clock, with a leading 0 (e.g. 07)

H

24-hour clock hour (e.g. 19)

HH

24-hour clock hour, with a leading 0 (e.g. 19)

m

Minutes

mm

Minutes with a leading zero

M

Month number

MM

Month number with leading zero

MMM

Abbreviated Month Name (e.g. Dec)

MMMM

Full month name (e.g. December)

s

Seconds

ss

Seconds with leading zero

t

Abbreviated AM / PM (e.g. A or P)

tt

AM / PM (e.g. AM or PM

y

Year, no leading zero (e.g. 2001 would be 1)

yy

Year, leadin zero (e.g. 2001 would be 01)

yyy

Year, (e.g. 2001 would be 2001)

yyyy

Year, (e.g. 2001 would be 2001)

K

Represents the time zone information of a date and time value (e.g. +05:00)

z

With DateTime values, represents the signed offset of the local operating system's time zone from Coordinated Universal Time (UTC), measured in hours. (e.g. +6)

zz

As z but with leadin zero (e.g. +06)

zzz

With DateTime values, represents the signed offset of the local operating system's time zone from UTC, measured in hours and minutes. (e.g. +06:00)

f

Represents the most significant digit of the seconds fraction; that is, it represents the tenths of a second in a date and time value.

ff

Represents the two most significant digits of the seconds fraction; that is, it represents the hundredths of a second in a date and time value.

fff

Represents the three most significant digits of the seconds fraction; that is, it represents the milliseconds in a date and time value.

ffff

Represents the four most significant digits of the seconds fraction; that is, it represents the ten thousandths of a second in a date and time value. While it is possible to display the ten thousandths of a second component of a time value, that value may not be meaningful. The precision of date and time values depends on the resolution of the system clock. On Windows NT 3.5 and later, and Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.

fffff

Represents the five most significant digits of the seconds fraction; that is, it represents the hundred thousandths of a second in a date and time value. While it is possible to display the hundred thousandths of a second component of a time value, that value may not be meaningful. The precision of date and time values depends on the resolution of the system clock. On Windows NT 3.5 and later, and Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.

ffffff

Represents the six most significant digits of the seconds fraction; that is, it represents the millionths of a second in a date and time value. While it is possible to display the millionths of a second component of a time value, that value may not be meaningful. The precision of date and time values depends on the resolution of the system clock. On Windows NT 3.5 and later, and Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.

fffffff

Represents the seven most significant digits of the seconds fraction; that is, it represents the ten millionths of a second in a date and time value. While it is possible to display the ten millionths of a second component of a time value, that value may not be meaningful. The precision of date and time values depends on the resolution of the system clock. On Windows NT 3.5 and later, and Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.

F

Represents the most significant digit of the seconds fraction; that is, it represents the tenths of a second in a date and time value. Nothing is displayed if the digit is zero.

:

Represents the time separator defined in the current DateTimeFormatInfo..::.TimeSeparator property. This separator is used to differentiate hours, minutes, and seconds.

/

Represents the date separator defined in the current DateTimeFormatInfo..::.DateSeparator property. This separator is used to differentiate years, months, and days.

"

Represents a quoted string (quotation mark). Displays the literal value of any string between two quotation marks ("). Your application should precede each quotation mark with an escape character (/).

'

Represents a quoted string (apostrophe). Displays the literal value of any string between two apostrophe (') characters.

%c

Represents the result associated with a c custom format specifier, when the custom date and time format string consists solely of that custom format specifier. That is, to use the d, f, F, h, m, s, t, y, z, H, or M custom format specifier by itself, the application should specify %d, %f, %F, %h, %m, %s, %t, %y, %z, %H, or %M. For more information about using a single format specifier, see Using Single Custom Format Specifiers.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值