C # date and time type DateTime string output format symbol and representative mode

One: DateTime is a type containing date and time in C #. When this type is converted into a string by Tostring (), it can be converted into a variety of string formats according to the parameters of the incoming Tostring ().

Two: classification

1. The parameters passed by DateTime calling Tostring () can be divided into two types: standard and custom:

(1) Standard: The system comes with a specific single character that can be converted into the format that has been set by the system.

(2) Customization: free combination of date and time codes (y, M, d, h, m, s, f) to show rich date formats.

2. Format type format source

The format of date and time can be set in the region and language (location: control panel → region and language) option in the Windows system.

The types that can be set are: short date, long date, short time, long time, etc. When calling ToString () for conversion, many conversion modes are combined through the above 4 categories. The following is a single character control printed out.

            string dtNow = DateTime.Now.ToString (); 
            DateTime dateTime = DateTime.Now; 
            Console.WriteLine ( " <> Date format without parameters: \ n \ t {0} \ n " , dtNow); 
            Console.WriteLine ( " <d> Short date format: \ n \ t {0} \ n " , dateTime.ToString ( " d " )); 
            Console.WriteLine ( " <D> Long date format: \ n \ t {0} \ n " , dateTime.ToString ( " D " )); 
            Console.WriteLine ( " <f> Long date and short time format: \ n \ t {0} \ n " , dateTime.ToString("f" )); 
            Console.WriteLine ( " <F> Long date long format: \ n \ t {0} \ n " , dateTime.ToString ( " F " )); 
            Console.WriteLine ( " <g> Short date short Time format: \ n \ t {0} \ n " , dateTime.ToString ( " g " )); 
            Console.WriteLine ( " <G> Short date long time format: \ n \ t {0} \ n " , dateTime .ToString ( " G " )); 
            Console.WriteLine ( " <m> month and day format: \ n \ t {0} \ n " , dateTime.ToString ( " m "));
            Console.WriteLine(" <M> month and day format: \ n \ t {0} \ n " , dateTime.ToString ( " M " )); 
            Console.WriteLine ( " <r> RFC1123 standard format: \ n \ t {0} \ n " , dateTime.ToString ( " r " )); 
            Console.WriteLine ( " <R> RFC date format: \ n \ t {0} \ n " , dateTime.ToString ( " R " )); 
            Console.WriteLine ( " <t> Short time format: \ n \ t {0} \ n " , dateTime.ToString ( " t " )); 
            Console.WriteLine ( "<T> Long-term format: \ n \ t {0} \ n ", dateTime.ToString ( " T " )); 
            Console.WriteLine ( " <s> Date format: \ n \ t {0} \ n " , dateTime.ToString ( "" )); 
            Console.WriteLine ( " <u> Common date format: \ n \ t {0} \ n " , dateTime.ToString ( " u " )); 
            Console.WriteLine ( " <U> Long date of the meridian long date long format: \ n \ t {0} \ n " , dateTime.ToString ( " U " )); 
            Console.WriteLine ( " <y> Year and month format: \ n \ t {0} \ n " , dateTime.ToString("y"));
            Console.WriteLine("<Y>年月格式:\n\t{0}\n", dateTime.ToString("Y"));
            Console.ReadKey();

The running result is:

 

 

The following are custom format types:

 // Custom format type:
             // Character conforms to: y (year), M (month), d (day), h (hour), m (minute), s (second), f (millisecond), can be seen Only the month is capital M, the others are all lowercase.
            // two digits after the yy year
             // yyyy 4-digit year
             // MM two-digit month
             // mm minutes
             // dd two-digit day
             // ddd day of the week
             // dddd day of the week
             // hh 12-hour hour
             // HH 24-hour hours
             // ss two-digit seconds
             // ff the first two digits of the // // fff the first three
             digits of the
             // // ffff the first four digits
             // 
            // Example:
            DateTime dt = DateTime.Now;
            Console.WriteLine("<无参数格式>\n{0}", dt.ToString());
            Console.WriteLine("<yy/MM/dd hh:mm:ss ff>\n{0}", dt.ToString("yy/MM/dd hh:mm:ss ff"));
            Console.WriteLine("<yyyy-MM-dd hh:mm:ss fff>\n{0}", dt.ToString("yyyy-MM-dd hh:mm:ss fff"));
            Console.WriteLine("<yy年MM月dd日 hh:mm:ss ffff>\n{0}", dt.ToString("yy年MM月dd日 hh:mm:ss ffff"));
            Console.WriteLine ( " <yyyy MM month dd day dddd week ddd hh: mm: ss ff> \ n {0} " , dt.ToString ( " yyyy year MM month dd day dddd week ddd hh: mm: ss ff " )); 
            Console.ReadKey ();

The running result is:

 

Guess you like

Origin www.cnblogs.com/daitu/p/12725623.html