DateTime Format In C# - C# Tutorial And Source Code
Có thể bạn quan tâm
A date and time format string defines the text representation of a DateTime value that results from a formatting operation . C# includes a really great struct for working with dates and time.
DateTime dt = new DateTime(); DateTime dt = new DateTime(2000, 1, 10);You can assign the DateTime object a date and time value returned by a property or method.
DateTime dt1 = DateTime.Now; //returns current date and time DateTime dt2 = DateTime.Today; // returns today's dateStandard date and time format strings
A standard date and time format string in C# uses a single format specifier to define the text representation of a date and time value. A standard or custom format string can be used in two different ways:
- To define the string that results from a formatting operation.
- To define the text representation of a date and time value that can be converted to a DateTime value by a parsing operation.
Following table shows patterns defined in DateTimeFormatInfo and their values for en-US culture.
Standard formats are typically used when you need a fast string representation of your DateTime object based on current culture.
C# Standard date and time Example using System; namespace CStandardDateTime { class Program { static void Main(string[] args) { DateTime dt = DateTime.Now; Console.WriteLine(String.Format("{0:t}", dt) ); // ShortTime Console.WriteLine(String.Format("{0:d}", dt) ); // ShortDate Console.WriteLine(String.Format("{0:T}", dt) ); // LongTime Console.WriteLine(String.Format("{0:D}", dt) ); // LongDate Console.WriteLine(String.Format("{0:f}", dt) ); // LongDate+ShortTime Console.WriteLine(String.Format("{0:F}", dt) ); // FullDateTime Console.WriteLine(String.Format("{0:g}", dt) ); // ShortDate+ShortTime Console.WriteLine(String.Format("{0:G}", dt) ); // ShortDate+LongTime Console.WriteLine(String.Format("{0:m}", dt) ); // MonthDay Console.WriteLine(String.Format("{0:y}", dt) ); // YearMonth Console.WriteLine(String.Format("{0:r}", dt) ); // RFC1123 Console.WriteLine(String.Format("{0:s}", dt) ); // SortableDateTime Console.WriteLine(String.Format("{0:u}", dt) ); // UniversalSortableDateTime Console.ReadKey(); } } } output
Custom date and time format string
A custom date and time format string consists of two or more characters. Date and time formatting methods interpret any single-character string as a standard date and time format string. In formatting operations , custom date and time format strings can be used either with the ToString method of a date and time instance or with a method that supports composite formatting .
DateTime dt = new DateTime((2000, 1, 10) Console.WriteLine("The date is " + dt.ToString("MMMM dd, yyyy") + "."); output The date is January 10, 2000.The following table describes various C# DateTime formats .
| Format |
|---|
| DateTime.Now.ToString("MM/dd/yyyy") |
| DateTime.Now.ToString("HH:mm") |
| DateTime.Now.ToString("hh:mm tt") |
| DateTime.Now.ToString("H:mm") |
| DateTime.Now.ToString("h:mm tt") |
| DateTime.Now.ToString("HH:mm:ss") |
| DateTime.Now.ToString("dddd, dd MMMM yyyy") |
| DateTime.Now.ToString("dddd, dd MMMM yyyy HH:mm:ss") |
| DateTime.Now.ToString("MM/dd/yyyy HH:mm") |
| DateTime.Now.ToString("MM/dd/yyyy hh:mm tt") |
| DateTime.Now.ToString("MM/dd/yyyy H:mm") |
| DateTime.Now.ToString("MM/dd/yyyy h:mm tt") |
| DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss") |
| DateTime.Now.ToString("MMMM dd") |
| DateTime.Now.ToString("yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss.fffffffK") |
| DateTime.Now.ToString("ddd, dd MMM yyy HH’:’mm’:’ss ‘GMT’") |
| DateTime.Now.ToString("yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss") |
| DateTime.Now.ToString("yyyy MMMM") |
- C# Types
- C# boxing and unboxing
- C# DataTypes
- C# type conversions
- C# Access Modifiers , CSharp Access Specifiers
- How to CultureInfo in C#
- How do I solve System.InvalidCastException?
- Difference between readonly and const keyword
- Difference Between Task and Thread
- Object reference not set to an instance of an object
- How to Convert Char to String in C#
- How to Convert Char Array to String in C#
- How to convert int to string in C#?
- C# - Operating System Information
- Start and Kill Processes in C#
- Random Number Generator in C#
Từ khóa » Hh Mm Ss C#
-
C# DateTime Format
-
C# DateTime To "YYYYMMDDHHMMSS" Format - Stack Overflow
-
Custom Date And Time Format Strings - Microsoft Docs
-
DateTime.ToString Method (System) - Microsoft Docs
-
How To Format The Hh:mm:ss Separators Of A TimeSpan | Phrase
-
DateTime Format In C# - Code Maze
-
DateTime Formats In C# - TutorialsTeacher
-
C# ToString("hh:mm:ss") Code Example - Code Grepper
-
How To Convert A DateTime Object To A String In C#
-
DateTime Format: Hh:mm:ss Tt : Date Time Format
-
How To Convert Seconds Into Hh:mm:ss In C#? - QA With Experts
-
C# DateTime Format:Working With Date &Time Format In C#
-
Extracting Time From DateTime In Hh:mm:ss Format In C# - Forget Code
-
C# – String To Date Time “hh:mm:ss” Or “dd.mm.yyyy Hh:mm:ss” Format