In peacetime coding, often to convert the date into a variety of forms of output or hold, today specifically to do a test, found that datetime tostring () method incredibly have so many forms of expression, and everyone to share.
DateTime Time=datetime.now; 2010-5-28 11:22:02.4552691 Friday
Ime. to ("Y") "May 2010"
Time. to ("yy") "10"
Time. to ("yyy") "2010"
Time. to ("yyyy") "2010"
Time. to ("Y") "May 2010"
Time. To ("M") "May 28"
Time. to ("MM") "22"
Time. to ("MMM") "22"
Time. to ("MMMM") "22"
Time. to ("M") "May 28"
Time. to ("MM") "05"
Time. to ("MMM") "May"
Time. to ("MMMM") "May"
Time. to ("D") "2010-5-28"
Time. to ("DD") "28"
Time. to ("ddd") "Five"
Time. to ("dddd") "Friday"
Time. to ("D") "May 28, 2010"
Time. to ("HH") "11"
Time. to ("HHH") "11"
Time. to ("HHHH") "11"
Time. to ("HH") "11"
Time. to ("HHH") "11"
Time. to ("HHHH") "11"
Time. To ("S") "2010-05-28t11:22:02"
Time. to ("SS") "02"
Time. to ("SSS") "02"
Time. to ("SSSs") "02"
Time. to ("F") "May 28, 2010 11:22"
Time. to ("FF") "45"
Time. to ("FFF") "455"
Time. to ("FFFF") "4552"
Time. to ("T") "11:22"
Time. to ("TT") "AM"
Time. to ("TTT") "AM"
Time. to ("Tttt") "AM"
Time. to ("T") "11:22:02"
Time. to ("ZZ") "+08"
Time. to ("zzz") "+08:00"
Time. to ("zzzz") "+08:00"
Time. to ("R") "Fri, 11:22:02 GMT"
Time. to ("R") "Fri, 11:22:02 GMT"
Time. to ("U") "2010-05-28 11:22:02z"
Time. to ("U") "May 28, 2010 3:11:02"
Time. to ("O") "2010-05-28t11:22:02.4552691+08:00"
Time. to ("O") "2010-05-28t11:22:02.4552691+08:00"
Time. to ("U") "2010-05-28 11:22:02z"
Time. to ("U") "May 28, 2010 3:11:02"
Time. To ("G") "2010-5-28 11:22"
Time. to ("GG") "A.D."
Time. to ("GGG") "A.D."
Time. to ("Gggg") "A.D."
Time. to ("G") "2010-5-28 11:22:02"
The ability to take out a single part of a string can be used together, such as:
Time. ToString ("GG yyyy/mm/dd hh:mm:ss dddd") "A.D. 2010-05-28 11:56:26 Friday"
Because the gg,yyyy,mm,dd,hh,mm,ss,dddd here only takes out a single part.
Not like the following combinations:
Time. ToString ("Gg y") "A.D. 10"//results are not what we need "A.D. May 2010"
Because Y represents "May 2010", it is not a single function.
in ASP. NET C #/
We can get the current time by using the Datatime class. We can get different times by invoking various methods in the class: Date (2008-09-04), Time (12:12:12), date + time (2008-09-04 12:11:10), etc.
Get Date + Time
DateTime.Now.ToString (); 2008-9-4 20:02:10
DateTime.Now.ToLocalTime (). ToString (); 2008-9-4 20:12:12
Get Date
DateTime.Now.ToLongDateString (). ToString (); September 4, 2008
DateTime.Now.ToShortDateString (). ToString (); 2008-9-4
DateTime.Now.ToString ("Yyyy-mm-dd"); 2008-09-04
DateTime.Now.Date.ToString (); 2008-9-4 0:00:00
Get time
DateTime.Now.ToLongTimeString (). ToString (); 20:16:16
DateTime.Now.ToShortTimeString (). ToString (); 20:16
DateTime.Now.ToString ("Hh:mm:ss"); 08:05:57
DateTime.Now.TimeOfDay.ToString (); 20:33:50.7187500
Other
Datetime.tofiletime (). ToString (); 128650040212500000
DATETIME.NOW.TOFILETIMEUTC (). ToString (); 128650040772968750
DateTime.Now.ToOADate (). ToString (); 39695.8461709606
DateTime.Now.ToUniversalTime (). ToString (); 2008-9-4 12:19:14
DateTime.Now.Year.ToString (); Get year//2008
DateTime.Now.Month.ToString (); Get Month//9
DateTime.Now.DayOfWeek.ToString (); Get Week//Thursday
DateTime.Now.DayOfYear.ToString (); Get the day of the Week//248
DateTime.Now.Hour.ToString (); Get hours//20
DateTime.Now.Minute.ToString (); Get minutes//31
DateTime.Now.Second.ToString (); Gets the number of seconds//45
N is a number, can be a number of integers, can also matter a decimal
Dt. AddYears (n). ToString (); Time Plus n years
Dt. AddDays (n). ToString (); Plus n days
Dt. AddHours (n). ToString (); Add N Hours
Dt. AddMonths (n). ToString (); Add n months
Dt. AddSeconds (n). ToString (); Plus n seconds
Dt. AddMinutes (n). ToString (); Add n Points
SQL statements use time and date functions
GETDATE (): Gets the current time of the system
DATEADD (datepart,number,date): Calculates a new time value after a time based on a time, such as: DateAdd (Yy,30,getdate ())
DateDiff (datepart,startdate,enddate): Calculates the difference of two time, such as: DateDiff (Yy,getdate (), ' 2008-08-08 ')
Dataname (datepart,date): Gets the value of a different part of time, the return value is a string
DatePart (datepart,date): Similar to Datename, except that the return value is integer type
Day: Gets the number of days of the specified time
Month (date): Gets the month of the specified time
Year (date): Gets the years of the specified time
Select year (GETDATE ()): Current Years
The problem of converting the time format to SQL when reading
Time is a DateTime type now in some places I'm going to show only the year/month/day put the specific points behind the reality of this conversion how to write
<%# Eval ("Ntime")%> in this case how to convert
Tied in the GridView inside the <asp:boundfield datafield= "Ntime" headertext= "Time" readonly= "True"/> In this case how to convert
All two places I have to convert to show only the year/month/day how to write
<% #Convert. ToDateTime (Eval ("Ntime")). ToString ("Yyyy/mm/dd")%>--as long as the ntime can be converted into a time format.
<asp:boundfield datafield= "Ntime" headertext= "Time" readonly= "True"
Dataformatstring= "{0:yyyy\/mm\/dd}"/>--the type of the "ntime" column here must be a time type, otherwise it cannot be converted
Both of these, the converted format is: Year/month/day.
<asp:boundfield datafield= "Ntime" headertext= "Time" readonly= "True" dataformatstring= "{0:yyyy-mm-dd}"/>
<%# Eval ("Ntime")%>:
<%# Eval (Container.DataItem, "Ntime", "{0:d}")%>
ASP. Format and conversion of net date time, month and day