one, C # date format
- DateTime dt = DateTime.Now;
- Dt. ToString ();//2005-11-5 13:21:25
- Dt. Tofiletime (). ToString ();//127756416859912816
- Dt. TOFILETIMEUTC (). ToString ();//127756704859912816
- Dt. ToLocalTime (). ToString ();//2005-11-5 21:21:25
- Dt. Tolongdatestring (). ToString ();//November 5, 2005
- Dt. Tolongtimestring (). ToString ();//13:21:25
- Dt. ToOADate (). ToString ();//38661.5565508218
- Dt. ToShortDateString (). ToString ();//2005-11-5
- Dt. Toshorttimestring (). ToString ();//13:21
- Dt. ToUniversalTime (). ToString ();//2005-11-5 5:21:25
- Dt. Year.tostring ();//2005
- Dt. Date.tostring ();//2005-11-5 0:00:00
- Dt. Dayofweek.tostring ();//saturday
- Dt. Dayofyear.tostring ();//309
- Dt. Hour.tostring ();//13
- Dt.Millisecond.ToString ();//441
- Dt. Minute.tostring ();//30
- Dt. Month.tostring ();//11
- DT. Second.tostring ();//28
- . Dt. Ticks.tostring ();//632667942284412864
- DT. Timeofday.tostring ();//13:30:28.4412864
- DT. ToString ();//2005-11-5 13:47:04
- Dt. AddYears (1). ToString ();//2006-11-5 13:47:04
- Dt. AddDays (1.1). ToString ();//2005-11-6 16:11:04
- Dt. AddHours (1.1). ToString ();//2005-11-5 14:53:04
- Dt. Addmilliseconds (1.1). ToString ();//2005-11-5 13:47:04
- Dt. AddMonths (1). ToString ();//2005-12-5 13:47:04
- Dt. AddSeconds (1.1). ToString ();//2005-11-5 13:47:05
- Dt. AddMinutes (1.1). ToString ();//2005-11-5 13:48:10
- Dt. Addticks (1000). ToString ();//2005-11-5 13:47:04
- Dt. ADD (?). ToString ();//question mark is a time period
- Dt. Equals ("2005-11-6 16:11:04"). ToString ();//false
- Dt. Equals (DT). ToString ();//true
- Dt. GetHashCode (). ToString ();//1474088234
- Dt. GetType (). ToString ();//system.datetime
- Dt. GetTypeCode (). ToString ();//datetime
- Dt. Getdatetimeformats (' s ') [0]. ToString ();//2005-11-05t14:06:25
- Dt. Getdatetimeformats (' t ') [0]. ToString ();//14:06
- Dt. Getdatetimeformats (' y ') [0]. ToString ();//November 2005
- Dt. Getdatetimeformats (' D ') [0]. ToString ();//November 5, 2005
- Dt. Getdatetimeformats (' D ') [1]. ToString ();//2005 11 05
- Dt. Getdatetimeformats (' D ') [2]. ToString ();//Saturday 2005 11 05
- Dt. Getdatetimeformats (' D ') [3]. ToString ();//Saturday November 5, 2005
- Dt. Getdatetimeformats (' M ') [0]. ToString ();//November 5
- Dt. Getdatetimeformats (' f ') [0]. ToString ();//November 5, 2005 14:06
- Dt. Getdatetimeformats (' g ') [0]. ToString ();//2005-11-5 14:06
- Dt. Getdatetimeformats (' R ') [0]. ToString ();//sat, 2005 14:06:25 GMT
- String. Format ("{0:d}", DT);//2005-11-5
- String. Format ("{0:d}", dt);//November 5, 2005
- String. Format ("{0:f}", dt);//November 5, 2005 14:23
- String. Format ("{0:f}", dt);//November 5, 2005 14:23:23
- String. Format ("{0:g}", DT);//2005-11-5 14:23
- String. Format ("{0:g}", DT);//2005-11-5 14:23:23
- String. Format ("{0:m}", dt);//November 5
- String. Format ("{0:r}", DT);//sat, 2005 14:23:23 GMT
- String. Format ("{0:s}", DT);//2005-11-05t14:23:23
- String. Format ("{0:t}", DT);//14:23
- String. Format ("{0:t}", DT);//14:23:23
- String. Format ("{0:u}", DT);//2005-11-05 14:23:23z
- String. Format ("{0:u}", dt);//November 5, 2005 6:23:23
- String. Format ("{0:y}", dt);//November 2005
- String. Format ("{0}", dt);//2005-11-5 14:23:23
- String. Format ("{0:yyyymmddhhmmssffff}", DT);
Second, date format pattern description
A day in the D month. One-digit date has no leading zeros.
DD a day of the month. A one-digit date has a leading zero.
The abbreviated name of the day of the DDD week, defined in Abbreviateddaynames.
dddd the full name of the day of the week, as defined in DayNames.
M-month number. One-digit month has no leading zeros.
MM month number. One-digit month has a leading zero.
Abbreviated name of the MMM month, defined in AbbreviatedMonthNames.
The full name of the MMMM month, as defined in MonthNames.
Y does not contain the year of the era. If the year that does not contain an era is less than 10, the year is displayed without leading zeros.
YY does not contain the year of the era. If the year that does not contain an era is less than 10, the year with leading zeros is displayed.
The YYYY includes the four-digit year of the era.
GG period or ERA. If the date to be formatted does not have an associated period or era string, the pattern is ignored.
H 12 Hour hour system. One-digit hours do not have leading zeros.
HH 12-hour hour. One-digit hours have leading zeros.
H 24 hour hour system. One-digit hours do not have leading zeros.
HH 24-hour hour. One-digit hours have leading zeros.
M minutes. A single-digit number of minutes does not have a leading zero.
MM minutes. A single-digit number of minutes has a leading zero.
s seconds. The number of seconds in a single digit does not have a leading zero.
SS seconds. The number of seconds of one digit has a leading zero.
The fractional precision of the F-second is one digit. The remaining digits are truncated.
The fractional precision of the FF seconds is two bits. The remaining digits are truncated.
The fractional precision of FFF seconds is three bits. The remaining digits are truncated.
The fractional precision of ffff seconds is four bits. The remaining digits are truncated.
The fractional precision of fffff seconds is five bits. The remaining digits are truncated.
The fractional precision of ffffff seconds is six bits. The remaining digits are truncated.
The fractional precision of fffffff seconds is seven bits. The remaining digits are truncated.
t the am/pm defined in AMDesignator or PMDesignator indicates the first character of the item, if one exists.
The AM/PM indicated by the TT in AMDesignator or PMDesignator (if present).
The Z Time zone offset ("+" or "-" followed by hours only). One-digit hours do not have leading zeros. For example, Pacific Standard Time is "-8".
ZZ Time zone offset ("+" or "-" followed by hours only). One-digit hours have leading zeros. For example, Pacific Standard Time is "-08".
The ZZZ Full time zone offset ("+" or "-" followed by hours and minutes). The number of hours and minutes of a single digit has a leading zero. For example, Pacific Standard Time is " -08:00".
: The default time delimiter defined in TimeSeparator.
/The default date delimiter defined in DateSeparator.
% c where C is the format pattern (if used alone). You can omit the "%" character if the format pattern is merged with literal characters or other format patterns.
\ c where c is any character. Displays the characters as they are originally defined. To display a backslash character, use "\ \".
Only the format patterns listed in the second table above can be used to create custom patterns, and the standard format characters listed in the first table cannot be used to create custom patterns.
The custom pattern is at least two characters long;
1. DateTime.ToString ("D"); Returns a DateTime value; "D" is a standard short date pattern.
2. DateTime.ToString ("%d"); Returns the day of the month; "%d" is a custom pattern.
3. DateTime.ToString ("D"); Returns the day of the month followed by a blank character, and "D" is a custom pattern.
Three, C # common date processing function
Calculates the difference in the number of days between 2 dates
- DateTime dt1 = Convert.datetime ("2007-8-1");
- DateTime DT2 = Convert.datetime ("2007-8-15");
- TimeSpan span = dt2. Subtract (DT1);
- int Daydiff = span. Days + 1;
Calculate the number of days of the month in a year
- int days = Datetime.daysinmonth (2007, 8);
- days = 31;
Add one day to date, one day less
- DateTime DT =datetime.now;
- Dt. AddDays (1); Add one day
- Dt. AddDays (-1);//Decrease One day
* Note: the following milliseconds use a maximum of 997, not 999 because SQL Server has a precision of 3 milliseconds
Days of the Month
int daysinmonth = Datetime.daysinmonth (DateTime.Now.Year, DateTime.Now.Month);
whether the number of days is a leap year
int daysinyear = Datetime.isleapyear (DateTime.Now.Year)? 366:365;
First day of the month
DateTime firstdayinmonth = new DateTime (DateTime.Now.Year, DateTime.Now.Month, 1);
The last day of this month, 1th plus one months next month 1th, and then cut a day is the last day of this month
DateTime lastdayinmonth = new DateTime (DateTime.Now.Year, DateTime.Now.Month, 1). AddMonths (1). AddDays (-1);
Midnight of the last day of this month
DateTime lastDayInMonth2 = new DateTime (DateTime.Now.Year, DateTime.Now.Month, 1). AddMonths (1). Addmilliseconds (-3);
First day of the year
DateTime firstdayinyear = new DateTime (DateTime.Now.Year, 1, 1);
Last day of the year
DateTime lastdayinyear = new DateTime (DateTime.Now.Year, 12, 31);
Midnight of the last day of the year
DateTime lastDayInYear2 = new DateTime (DateTime.Now.Year, 12, 31, 23, 59, 59, 997);
Get the day of the week in Sunday for 7
int dayOfWeek = Convert.ToInt32 (DateTime.Now.DayOfWeek) < 1? 7:convert.toint32 (DateTime.Now.DayOfWeek);
Monday
DateTime Monday = new DateTime (datetime.now.year,datetime.now.month,datetime.now.day). AddDays (1-dayofweek);
This Sunday
DateTime Sunday = Monday. AddDays (6);
Midnight of Sunday this week
DateTime Sunday2 = Monday. AddDays (7). Addmilliseconds (-3);
First day of the quarter
DateTime firsydayinquarter = new DateTime (DateTime.Now.Year, DateTime.Now.Month-(datetime.now.month-1)% 3, 1);
Last day of the quarter
DateTime Lastdayinquarter = firsydayinquarter.addmonths (3). AddDays (-1);
Midnight of the last day of the quarter
DateTime LastDayInQuarter2 = firsydayinquarter.addmonths (3). Addmilliseconds (-3);
string datetime = "10-jul-09";
String newdatetime = DateTime.Parse (DateTime, New System.Globalization.CultureInfo ("De-de")). ToString ("YyyyMMdd");
d. C # compare two time sizes
1. Comparison of time and size experiments
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. Calculates the function of two time difference value, returns the absolute value of the time difference:
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 () + "Day" +ts. Hours.tostring () + "Hour" +ts. Minutes.tostring () + "minutes" +ts. Seconds.tostring () + "seconds";
}
Catch
{
}
return DateDiff;
}
3. Realize the function of calculating datetime1-36 day =datetime2
TimeSpan ts=new TimeSpan (40,0,0,0);
DateTime dt2=datetime.now.subtract (TS);
Msg. Text=datetime.now.tostring () + "-" +ts. Days.tostring () + "days \ r \ n"; Msg. Text+=dt2. ToString ();
C # time-date operations