Date and time values are crucial in applications for various purposes, such as displaying timestamps, scheduling events, or formatting data for reports. In C#, the DateTime structure is used to work with date and time values. You can format these values into human-readable strings using the ToString method.
The format string used with the DateTime.ToString method allows you to customize how the date and time are displayed. Here we are going to look at some examples of format strings and the resulting formatted date and time:
1. "yyyy-MM-dd HH:mm:ss": This format string displays the date and time in the format "Year-Month-Day Hour:Minute:Second."
C# Code:
// C-sharp program to print current date and time using System; public class HelloWorld { public static void Main(string[] args) { DateTime dateTime = DateTime.Now; string formattedDateTime = dateTime.ToString("yyyy-MM-dd HH:mm:ss"); Console.WriteLine(formattedDateTime); } }
2023-09-02 14:46:04
C# Code:
// C-sharp program to print current date and time using System; public class HelloWorld { public static void Main(string[] args) { DateTime dateTime = DateTime.Now; string formattedDateTime = dateTime.ToString("dd/MM/yyyy"); Console.WriteLine(formattedDateTime); } }
25/09/2023
C# Code:
// C-sharp program to print current date and time using System; public class HelloWorld { public static void Main(string[] args) { DateTime dateTime = DateTime.Now; string formattedDateTime = dateTime.ToString("MMM dd, yyyy"); Console.WriteLine(formattedDateTime); } }
July 24, 2023
C# Code:
// C-sharp program to print current date and time using System; public class HelloWorld { public static void Main(string[] args) { DateTime dateTime = DateTime.Now; string formattedDateTime = dateTime.ToString("hh:mm tt"); Console.WriteLine(formattedDateTime); } }
04:40 PM
Basic Format Specifiers.
- "d": Short date pattern (e.g., "7/25/2023").
- "D": Long date pattern (e.g., "Monday, July 25, 2023").
- "t": Short time pattern (e.g., "3:30 PM").
- "T": Long time pattern (e.g., "3:30:45 PM").
- "f": Full date and short time pattern (e.g., "Monday, July 25, 2023 3:30 PM").
- "F": Full date and long time pattern (e.g., "Monday, July 25, 2023 3:30:45 PM").
- "g": General date and short time pattern (e.g., "7/25/2023 3:30 PM").
- "G": General date and long time pattern (e.g., "7/25/2023 3:30:45 PM").
- "yyyy-MM-dd": Custom date format (e.g., "2023-07-25").
- "HH:mm:ss": Custom time format (e.g., "15:30:45").
Custom Format Specifiers.
- "yyyy": Four-digit year (e.g., "2023").
- "MM": Two-digit month (e.g., "07" for July).
- "dd": Two-digit day (e.g., "25").
- "HH": Two-digit hour (24-hour clock, e.g., "15").
- "hh": Two-digit hour (12-hour clock, e.g., "03").
- "mm": Two-digit minute (e.g., "30").
- "ss": Two-digit second (e.g., "45").
- "fff": Milliseconds (e.g., "123").
C# Code to Customize Data and Time Format.
// C-sharp program to customize current date and time using System; public class HelloWorld { public static void Main(string[] args) { DateTime now = DateTime.Now; // "7/25/2023" string shortDate = now.ToString("d"); // "Monday, July 25, 2023" string longDate = now.ToString("D"); // "3:30 PM" string shortTime = now.ToString("t"); // "3:30:45 PM" string longTime = now.ToString("T"); // "Monday, July 25, 2023 3:30" string fullDateTime = now.ToString("F"); // "2023-07-25 15:30:45" string customFormat = now.ToString("yyyy-MM-dd HH:mm:ss"); Console.WriteLine(shortDate); Console.WriteLine(longDate); Console.WriteLine(shortTime); Console.WriteLine(longTime); Console.WriteLine(fullDateTime); Console.WriteLine(customFormat); } }
09/02/2023
Saturday, 02 September 2023
15:22
15:22:07
Saturday, 02 September 2023 15:22:07
2023-09-02 15:22:07
No comments:
Post a Comment