C date time format summary

  • 2020-05-19 05:37:13
  • OfStack

Date conversion 1

In order to achieve different display effects, sometimes we need to convert the time, the default format is: 2007-01-03 14:33:34, to convert to other formats, we need to use the method DateTime.ToString (String, IFormatProvider) as follows:


using System;
using System.Globalization;
String format="D";
DateTime date=DataTime,Now;
Response.Write(date.ToString(format, DateTimeFormatInfo.InvariantInfo));

Results output

Thursday, June 16, 2005

Parameter format format detailed usage:

Format character association properties/descriptions
d ShortDatePattern
D LongDatePattern
f full date and time (long date and short time)
F FullDateTimePattern (long date and long time)
g general (short date and short time)
G general (short date and long time)
m, M MonthDayPattern
r, R RFC1123Pattern
s USES SortableDateTimePattern local time (based on ISO 8601)
t ShortTimePattern
T LongTimePattern
u UniversalSortableDateTimePattern is a format for displaying common times
Full date and time (long date and long time) for U to use the common time
y, Y YearMonthPattern

The following table lists patterns that can be combined to construct custom patterns. These patterns are case sensitive; For example, identify "MM" but not "mm". If the custom pattern contains white space characters or characters enclosed in single quotes, the output string page will also contain these characters. A character that is not defined as part 1 of a format pattern or as a format character is copied in its original meaning.

Format pattern specification
A day in d month. A 1-digit date has no leading zero.
A day in the month dd. A 1-digit date has a leading zero.
The abbreviation of a day of the week defined in AbbreviatedDayNames.
The full name of a day in the week of dddd, defined in DayNames.
M month figures. One-digit months have no leading zeros.
MM monthly figures. A 1-digit month has a leading zero.
The abbreviated name of the month MMM, as defined in AbbreviatedMonthNames.
The full name of the month MMMM, defined in MonthNames.
The y does not include the year of the era. If the year without epoch is less than 10, the year without leading zero is displayed.
yy does not include the year of the era. If the year without epoch is less than 10, the year with leading zero is displayed.
yyyy includes 4-digit years for era.
gg period or era. If you want to format a date that does not have an associated period or epoch string, you ignore the pattern.
h is a 12-hour system. The number of one-digit hours has no leading zero.
hh 12-hour hour system. The number of one-digit hours has a leading zero.
H 24 hour system. The number of one-digit hours has no leading zero.
HH 24-hour hours. The number of one-digit hours has a leading zero.
m minutes. There is no leading zero for the number of minutes of one digit.
mm minutes. The number of minutes in a 1-digit number has a leading zero.
s seconds. The number of seconds in a 1-digit number has no leading zero.
ss seconds. The number of seconds in a 1-digit number has a leading zero.
f seconds has a decimal precision of 1 bit. The remaining Numbers are truncated.
ff seconds has a decimal precision of two digits. The remaining Numbers are truncated.
fff seconds has a decimal precision of 3 digits. The remaining Numbers are truncated.
ffff seconds has a decimal precision of 4 digits. The remaining Numbers are truncated.
fffff seconds has a decimal precision of 5 digits. The remaining Numbers are truncated.
ffffff seconds has a decimal precision of 6 digits. The remaining Numbers are truncated.
fffffff seconds has a decimal precision of 7 digits. The remaining Numbers are truncated.
The first character (if present) of the AM/PM indicator, as defined in AMDesignator or PMDesignator.
The AM/PM indicator, if present, as defined in AMDesignator or PMDesignator.
z time zone offset (" + "or" - "followed only by hours). The number of one-digit hours has no leading zero. Pacific standard time, for example, is "-8".
zz time zone offset (" + "or" - "followed only by hours). The number of one-digit hours has a leading zero. For example, Pacific standard time is "-08".
zzz full time zone offset (" + "or" - "followed by hours and minutes). The number of hours and minutes of one 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). If the format pattern merges with the literal character or another format pattern, the '%' character can be omitted.
c where c is any character. Display characters as they appear. To display backslash characters, use "\".

Only the format schema listed in table 2 above can be used to create a custom schema; The standard format characters listed in table 1 cannot be used to create custom schemas. The length of a custom pattern is at least two characters. For example,

DateTime.ToString ("d") returns the DateTime value; "d" is the standard short date mode.
DateTime.ToString("%d") returns to a day in the month; "%d" is a custom pattern.
DateTime.ToString ("d ") returns a day in the month followed by a blank character; "d" is a custom pattern.

What is more convenient is that the above parameters can be combined at will, and will not be wrong, try more, you will find the time format you want
If you want to get June 2005 such format of time
You could write it like this:
date.ToString("yyyy MM month ", DateTimeFormatInfo. InvariantInfo)

Date conversion 2

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();//127756704859912816
Label4.Text = dt.ToLocalTime().ToString();//2005-11-5 21:21:25
Label5. Text = dt. ToLongDateString () ToString (); // November 5, 2005
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 (); // question mark is 1 time period

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 (); / / in November 2005
Label4. Text = dt. GetDateTimeFormats (' D) [0]. ToString (); // November 5, 2005
Label5.Text = dt.GetDateTimeFormats('D')[1].ToString();//2005 11 05
Label6. Text = dt. GetDateTimeFormats (' D) [2]. ToString (); // Monday 6, 2005 11, 05
Label7. Text = dt. GetDateTimeFormats (' D) [3]. ToString (); // Monday 6 November 5, 2005
Label8. Text = dt. GetDateTimeFormats (' M) [0]. ToString (); / / on November 5
Label9. Text = dt. GetDateTimeFormats (' f) [0]. ToString (); // on November 5, 2005, 14:06
Label10.Text = dt.GetDateTimeFormats('g')[0].ToString();//2005-11-5 14:06
Label11.Text = dt.GetDateTimeFormats('r')[0].ToString();//Sat, 05 Nov 2005 14:06:25 GMT

Label1.Text = string.Format("{0:d}",dt);//2005-11-5
Label2. Text = string. Format (" {0: D} ", dt); // November 5, 2005
Label3. Text = string. Format (" {0: f} ", dt); // November 5, 2005 14:23
Label4. Text = string. Format (" {0: F} ", dt); // November 5, 2005 14:23: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); / / on November 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); // November 5, 2005 6:23:23
Label14. Text = string. Format (" {0: Y} ", dt); / / in November 2005
Label15.Text = string.Format("{0}",dt);//2005-11-5 14:23:23
Label16.Text = string.Format("{0:yyyyMMddHHmmssffff}",dt);
[/code]

C# compares the two time scales

1. Experiments comparing the size of time


    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. Calculate the function of two time differences and return 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()+" hours "
                         +ts.Minutes.ToString()+" minutes "
                         +ts.Seconds.ToString()+" seconds ";
             }
             catch
             {             }
             return dateDiff;
         }

3. Realize the function of calculating DateTime 1-36 days = DateTime2


TimeSpan ts=new TimeSpan(40,0,0,0);
    DateTime dt2=DateTime.Now.Subtract(ts);
    msg.Text=DateTime.Now.ToString()+"-"+ts.Days.ToString()+" day \r\n";
    msg.Text+=dt2.ToString();


Related articles: