Date Format C # (rpm)

 
C # format date 

 date a transformation
 
in order to achieve different display times, we need time to convert the default format is: 2007-01-03 14:33:34, to be converted to other formats, use of the DateTime.ToString method (String, IFormatProvider), as follows:

the using the System;
the using the System.Globalization;
String the format = "D";
the DateTime DataTime = DATE, Now;
Response.Write (Date.toString (the format, DateTimeFormatInfo.InvariantInfo));

results output
Thursday, June 16, 2005

the parameters detailed usage format format:

format character associated attributes / Description
D ShortDatePattern
D LongDatePattern
F full date and time (long and short)
F. FullDateTimePattern (date and time length)
G conventional (short date and short)
G conventional (short date and time)
m, m MonthDayPattern
R & lt, R & lt RFC1123Pattern 
S use local time SortableDateTimePattern (based on ISO 8601)
T ShortTimePattern
T LongTimePattern
u UniversalSortableDateTimePattern universal time format for displaying 
U full day and time of the universal time (date and time length) 
Y, the Y YearMonthPattern 

following table may be combined to construct a pattern from a defined pattern. These patterns are case sensitive; e.g., identifying "MM", but does not recognize "mm". If the custom mode comprising blank characters or characters enclosed in single quotes, the output string will contain these characters page. Undefined undefined format mode or as a part of their character format character literal replication. 

Mode Description Format 
a day of the month d. Single-digit days without leading zeros. 
dd month a day. Single-digit days have a leading zero. 
ddd day of the week abbreviated name, defined in the AbbreviatedDayNames. 
dddd full name of the day of the week, as defined in DayNames in. 
M month number. Digit months without leading zeros. 
MM month number. Single-digit months have a leading zero. 
MMM The abbreviated name of the month, as defined in AbbreviatedMonthNames in. 
MMMM The full name of the month, as defined in MonthNames in. 
y Year without the century. If the year without the century is less than 10, the display does not have a leading zero year. 
yy Year without the century. If the year without the century is less than 10, a leading zero year is displayed. 
h h 12 hour clock. Single-digit hours without leading zeros.
yyyy era including the four-digit year.
gg period or era. To set the date format that does not have an associated period or era string is ignored in this mode. 
zz time zone offset ( "+" or "-" followed by the hour only). Single-digit hours have a leading zero. For example, Pacific Standard Time is "-08."
h hh 12 hour clock. Single-digit hours have a leading zero. 
H H 24-hour clock. Single-digit hours without leading zeros. 
HH 24-hour clock. Single-digit hours have a leading zero. 
m min. Single-digit minutes without leading zeros. 
mm minutes. Single-digit minutes have a leading zero. 
s seconds. Single-digit seconds without leading zeros. 
ss seconds. Single-digit seconds have a leading zero. 
decimal precision f s is a. The remaining digits are truncated. 
ff decimal precision for two seconds. The remaining digits are truncated. 
fff decimal precision for three seconds. The remaining digits are truncated. 
ffff decimal precision for four seconds. The remaining digits are truncated. 
fffff decimal precision for five seconds. The remaining digits are truncated. 
ffffff decimal precision of six seconds. The remaining digits are truncated. 
fffffff decimal precision for seven seconds. The remaining digits are truncated. 
t PMDesignator defined in AMDesignator or the AM / PM indicating a character to item (if present). 
tt PMDesignator defined in AMDesignator or the AM / PM indicator item (if present). 
z time zone offset ( "+" or "-" followed by the hour only). Single-digit hours without leading zeros. For example, Pacific Standard Time is "-8." 
zzz The full time zone offset ( "+" or "-" followed by hours and minutes). Single-digit hours and minutes with leading zeros. For example, Pacific Standard Time is "-08: 00." 
: Delimiter TimeSeparator defined default time.
/ DateSeparator default date defined delimiters. 
% C where c is the format pattern (if used alone). If the format pattern is combined with literal characters or other format patterns, you can omit the "%" character. 
\ C where c is any character. According to the original meaning of the character. To display the backslash character, use "\\." 

Only above the second format mode listed in the table can be used to create custom patterns; the standard format characters listed in the first table can not be used to create custom patterns. Custom mode at least two characters in length; e.g., 

DateTime.ToString ( "D") Returns the DateTime value; "d" is the standard short date format. 
DateTime.ToString ( "% d") returns the day of the month; "% d" is a custom pattern. 
DateTime.ToString ( "d") returns the day of the month followed by a blank character; "d" is a custom pattern. 

More convenient that the above parameters can be freely combined, and can not go wrong, try more, and will definitely find the time format you want 
, such as to get time in June 2005, this format 
can be written like this: 
Date.toString ( "yyyy in MM month ", DateTimeFormatInfo.InvariantInfo) the date of conversion of two 
DateTime dt = DateTime.Now; 
Label1.Text = dt.ToString (); // 2005-11-5 13:21:25


 
Label2.Text = dt.ToFileTime () ToString ();. // 127756416859912816 
. Label3.Text = dt.ToFileTimeUtc () ToString ();
Label4.Text = dt.ToLocalTime().ToString();//2005-11-5 21:21:25
Label5.Text = dt.ToLongDateString().ToString();//2005年11月5日
Label6.Text = dt.ToLongTimeString().ToString();//13:21:25
Label7.Text = dt.ToOADate().ToString();//38661.5565508218
Label8.Text = dt.ToShortDateString().ToString();//2005-11-5
Label9.Text = dt.ToShortTimeString().ToString();//13:21
Label10.Text = dt.ToUniversalTime().ToString();//2005-11-5 5:21:25

Label1.Text = dt.Year.ToString();//2005
Label2.Text = dt.Date.ToString();//2005-11-5 0:00:00
Label3.Text = dt.DayOfWeek.ToString();//Saturday
Label4.Text = dt.DayOfYear.ToString();//309
Label5.Text = dt.Hour.ToString();//13
Label6.Text = dt.Millisecond.ToString();//441
Label7.Text = dt.Minute.ToString();//30
Label8.Text = dt.Month.ToString();//11
Label9.Text = dt.Second.ToString();//28
Label10.Text = dt.Ticks.ToString();//632667942284412864
Label11.Text = dt.TimeOfDay.ToString();//13:30:28.4412864

Label1.Text = dt.ToString();//2005-11-5 13:47:04
Label2.Text = dt.AddYears(1).ToString();//2006-11-5 13:47:04
Label3.Text = dt.AddDays(1.1).ToString();//2005-11-6 16:11:04
Label4.Text = dt.AddHours(1.1).ToString();//2005-11-5 14:53:04
Label5.Text = dt.AddMilliseconds(1.1).ToString();//2005-11-5 13:47:04
Label6.Text = dt.AddMonths(1).ToString();//2005-12-5 13:47:04
Label7.Text = dt.AddSeconds(1.1).ToString();//2005-11-5 13:47:05
Label8.Text = dt.AddMinutes(1.1).ToString();//2005-11-5 13:48:10
Label9.Text = dt.AddTicks(1000).ToString();//2005-11-5 13:47:04
Label10.Text = dt.CompareTo(dt).ToString();//0
Label11.Text = dt.Add(?).ToString();//问号为一个时间段

Label1.Text = dt.Equals("2005-11-6 16:11:04").ToString();//False
Label2.Text = dt.Equals(dt).ToString();//True
Label3.Text = dt.GetHashCode().ToString();//1474088234
Label4.Text = dt.GetType().ToString();//System.DateTime
Label5.Text = dt.GetTypeCode().ToString();//DateTime

Label1.Text = dt.GetDateTimeFormats('s')[0].ToString();//2005-11-05T14:06:25
Label2.Text = dt.GetDateTimeFormats('t')[0].ToString();//14:06
Label3.Text = dt.GetDateTimeFormats('y')[0].ToString();//2005年11月
Label4.Text = dt.GetDateTimeFormats ( 'D') [0] .ToString (); // 5 November 2005 
Label4.Text = string.Format ( "{0: F.}", dt); // 2005 at 14:23 on November 5:23
Label5.Text = dt.GetDateTimeFormats('D')[1].ToString();//2005 11 05
Label6.Text = dt.GetDateTimeFormats('D')[2].ToString();//星期六 2005 11 05
Label7.Text = dt.GetDateTimeFormats ( 'D') [3] .ToString (); // Saturday, November 5, 2005 
Label8.Text = dt.GetDateTimeFormats ( 'M') [0] .ToString (); / / November 5 
Label9.Text = dt.GetDateTimeFormats ( 'f') [0] .ToString (); // 5 November 2005, 14:06 
Label10.Text = dt.GetDateTimeFormats ( 'G') [0 ] .ToString (); // 2005-11-5 14:06 
Label11.Text = dt.GetDateTimeFormats ( 'R & lt') [0] .ToString (); // Sat, 05-Nov 2005 14:06:25 GMT 

Label1 = string.Format .text ( "{0: D}", dt); // 2005-11-5 
Label2.Text = string.Format ( "{0: D}", dt); // November 2005 5 
Label3.Text = string.Format ( "{0: f}", dt); // 5 November 2005 14:23 
Label5.Text = string.Format ( "{0: G}", dt) ; // 2005-11-5 14:23 
Label6.Text = string.Format ( "{0: G}", dt); // 2005-11-5 14:23:23
Label7.Text = string.Format("{0:M}",dt);//11月5日
Label8.Text = string.Format("{0:R}",dt);//Sat, 05 Nov 2005 14:23:23 GMT
Label9.Text = string.Format("{0:s}",dt);//2005-11-05T14:23:23
Label10.Text   string.Format("{0:t}",dt);//14:23
Label11.Text = string.Format("{0:T}",dt);//14:23:23
Label12.Text = string.Format("{0:u}",dt);//2005-11-05 14:23:23Z
Label13.Text = string.Format("{0:U}",dt);//2005年11月5日 6:23:23
Label14.Text = string.Format("{0:Y}",dt);//2005年11月
Label15.Text = string.Format("{0}",dt);//2005-11-5 14:23:23
Label16.Text = string.Format("{0:yyyyMMddHHmmssffff}",dt);  
 
       

C # compare two times the size of the

1、比较时间大小的实验
    string st1="12:13";
    string st2="14:14";
    DateTime dt1=Convert.ToDateTime(st1);
    DateTime dt2=Convert.ToDateTime(st2);
    DateTime dt3=DateTime.Now;
    if(DateTime.Compare(dt1,dt2)>0)
     msg.Text=st1+">"+st2;
    else
     msg.Text=st1+"<"+st2;
    msg.Text+="\r\n"+dt1.ToString();
    if(DateTime.Compare(dt1,dt3)>0)
     msg.Text+="\r\n"+st1+">"+dt3.ToString();
    else
     msg.Text+="\r\n"+st1+"<"+dt3.ToString();
2、计算两个时间差值的函数,返回时间差的绝对值:
         private string DateDiff(DateTime DateTime1,DateTime DateTime2)
         {
             string dateDiff=null;
             try
             {
                 TimeSpan ts1=new   TimeSpan(DateTime1.Ticks);
                 TimeSpan ts2=new   TimeSpan(DateTime2.Ticks);
                 TimeSpan ts=ts1.Subtract(ts2).Duration();
                 dateDiff=ts.Days.ToString()+"天"
                         +ts.Hours.ToString()+"小时"
                         +ts.Minutes.ToString()+"分钟"
                         +ts.Seconds.ToString()+"秒";
             }
             catch
             {
             }
             return dateDiff;
         }
3、实现计算DateTime1-36天=DateTime2的功能
    TimeSpan ts=new TimeSpan(40,0,0,0);
    DateTime dt2=DateTime.Now.Subtract(ts);
    msg.Text=DateTime.Now.ToString()+"-"+ts.Days.ToString()+"天\r\n";
    msg.Text+=dt2.ToString();
 

转载于:https://www.cnblogs.com/softwareking/archive/2011/04/28/2032184.html

Guess you like

Origin blog.csdn.net/weixin_33763244/article/details/93766399