C# DateTime Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
You can use the ToString()
method of the DateTime
object to convert it to a string in a specific format. To convert a DateTime
object to the "yyyyMMddHHmmss" format in C#, you can use the following code:
DateTime inputDate = DateTime.Now; string formattedDate = inputDate.ToString("yyyyMMddHHmmss");
To convert a string value with the "yyyyMMddHHmmss" format to a DateTime
object in C#, you can use the DateTime.ParseExact()
method. Here's an example:
string dateString = "20220327124530"; // March 27, 2022, 12:45:30 PM DateTime result = DateTime.ParseExact(dateString, "yyyyMMddHHmmss", CultureInfo.InvariantCulture);
In the above example, we have a string value with the "yyyyMMddHHmmss" format representing a date and time. We use the DateTime.ParseExact()
method to parse the string and convert it to a DateTime
object. The method takes the string value to be parsed, the format string to match the string value, and a CultureInfo
object that provides culture-specific formatting information.
To remove the time portion of a DateTime
object in C#, you can use the DateTime.Date
property to get a new DateTime
object with the same date as the input, but with the time component set to midnight. Here's an example:
DateTime inputDate = DateTime.Now; DateTime dateWithoutTime = inputDate.Date;
To convert a DateTime
object to a string in the year format in C#, you can use the ToString()
method with the "yyyy" format specifier. Here's an example:
DateTime inputDate = DateTime.Now; string yearString = inputDate.ToString("yyyy");
To convert a DateTime
object to a string in the month format in C#, you can use the ToString()
method with the "MM" format specifier. Here's an example:
DateTime inputDate = DateTime.Now; string monthString = inputDate.ToString("MM");
To convert a DateTime
object to a string in the day format in C#, you can use the ToString()
method with the "dd" format specifier. Here's an example:
DateTime inputDate = DateTime.Now; string dayString = inputDate.ToString("dd");
To convert a DateTime
object to a string in the 12-hour or 24-hour format in C#, you can use the ToString()
method with the "hh" or "HH" format specifier for the hour component, depending on the desired format. Here's an example:
DateTime inputDate = DateTime.Now; string hour12String = inputDate.ToString("hh"); // 12-hour format string hour24String = inputDate.ToString("HH"); // 24-hour format
To convert a DateTime
object to a string in the minute format in C#, you can use the ToString()
method with the "mm" format specifier. Here's an example:
DateTime inputDate = DateTime.Now; string minuteString = inputDate.ToString("mm");
To convert a DateTime
object to a string in the second format in C#, you can use the ToString()
method with the "ss" format specifier. Here's an example:
DateTime inputDate = DateTime.Now; string secondString = inputDate.ToString("ss");
To convert a DateTime
object to a string in the second fraction format in C#, you can use the ToString()
method with the "fff" format specifier. Here's an example:
DateTime inputDate = DateTime.Now; string fractionString = inputDate.ToString("fff");
To convert a DateTime
object to a string in the A.M. or P.M. format in the 12-hour format in C#, you can use the ToString()
method with the "tt" format specifier. Here's an example:
DateTime inputDate = DateTime.Now; string ampmString = inputDate.ToString("tt");
To get the time zone from a DateTime
object in C#, you can use the DateTimeOffset.Offset
property to get the time zone offset. Here's an example:
DateTime inputDate = DateTime.Now; TimeSpan offset = TimeZoneInfo.Local.GetUtcOffset(inputDate); string timeZoneString = offset.ToString();
To convert a DateTime
object to a string in the month/day number format without a leading zero and a 2-digit year in C#, you can use the ToString()
method with the "M/d/yy" format specifier. Here's an example:
DateTime inputDate = DateTime.Now; string monthDayYearString = inputDate.ToString("M/d/yy");
To convert a DateTime
object to a string in the month/day number format with a leading zero and a 4-digit year in C#, you can use the ToString()
method with the "MM/dd/yyyy" format specifier. Here's an example:
DateTime inputDate = DateTime.Now; string monthDayYearString = inputDate.ToString("MM/dd/yyyy");
To convert a DateTime
object to a string in the month/day number format with the hour, minute, and second components in C#, you can use the ToString()
method with the "MM/dd/yyyy HH:mm:ss" format specifier. Here's an example:
DateTime inputDate = DateTime.Now; string monthDayTimeString = inputDate.ToString("MM/dd/yyyy HH:mm:ss");
To convert a DateTime
object to a string in the month/day short name format in C#, you can use the ToString()
method with the "MMM d" format specifier. Here's an example:
DateTime inputDate = DateTime.Now; string monthDayShortString = inputDate.ToString("MMM d");
To convert a DateTime
object to a string in the month/day name format in C#, you can use the ToString()
method with the "MMMM d" format specifier. Here's an example:
DateTime inputDate = DateTime.Now; string monthDayNameString = inputDate.ToString("MMMM d");
To convert a DateTime
object to a string in the month/day name format with the hour, minute, and second components in C#, you can use the ToString()
method with the "MMMM d HH:mm:ss" format specifier. Here's an example:
DateTime inputDate = DateTime.Now; string monthDayTimeNameString = inputDate.ToString("MMMM d HH:mm:ss");
In addition to the custom format specifiers we've covered above, C# provides a number of standard format specifiers that you can use with the ToString()
method to format a DateTime
object. Here are some examples:
DateTime inputDate = DateTime.Now; string shortDateString = inputDate.ToString("d"); // Short date format string longDateString = inputDate.ToString("D"); // Long date format string fullDateTimeString = inputDate.ToString("F"); // Full date and time format string generalDateTimeString = inputDate.ToString("G"); // General date and time format string sortableDateTimeString = inputDate.ToString("s"); // Sortable date and time format string universalSortableDateTimeString = inputDate.ToString("u"); // Universal sortable date and time format string yearMonthString = inputDate.ToString("Y"); // Year and month format
C# DateTime format string: In C#, a DateTime format string is used to specify the desired format when converting a DateTime object to a string.
DateTime myDateTime = DateTime.Now; string formattedDateTime = myDateTime.ToString("yyyy-MM-dd HH:mm:ss"); Console.WriteLine($"Formatted DateTime: {formattedDateTime}");
Custom DateTime format in C#: You can create a custom DateTime format string to represent the date and time in a specific way.
DateTime myDateTime = DateTime.Now; string customFormattedDateTime = myDateTime.ToString("MMMM dd, yyyy HH:mm:ss"); Console.WriteLine($"Custom Formatted DateTime: {customFormattedDateTime}");
Formatting DateTime to string in C#:
Using the ToString
method to format a DateTime object as a string.
DateTime myDateTime = DateTime.Now; string formattedDateTime = myDateTime.ToString(); Console.WriteLine($"Formatted DateTime: {formattedDateTime}");
Standard date and time formats in C#: C# provides standard format specifiers for common date and time formats.
DateTime myDateTime = DateTime.Now; string shortDateFormat = myDateTime.ToString("d"); string longTimeFormat = myDateTime.ToString("T"); Console.WriteLine($"Short Date Format: {shortDateFormat}, Long Time Format: {longTimeFormat}");
C# DateTime to string with specific format:
Using the ToString
method with a specific format string.
DateTime myDateTime = DateTime.Now; string customFormat = "yyyy-MM-dd HH:mm:ss"; string formattedDateTime = myDateTime.ToString(customFormat); Console.WriteLine($"Formatted DateTime: {formattedDateTime}");
Parse DateTime with custom format in C#: Parsing a DateTime from a string with a custom format.
string dateString = "2023-01-15 14:30:00"; DateTime parsedDateTime = DateTime.ParseExact(dateString, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture); Console.WriteLine($"Parsed DateTime: {parsedDateTime}");
DateTime format specifier in C#: Using format specifiers to represent different components of the DateTime.
DateTime myDateTime = DateTime.Now; string year = myDateTime.ToString("yyyy"); string month = myDateTime.ToString("MM"); Console.WriteLine($"Year: {year}, Month: {month}");
Culture-specific DateTime formatting in C#: Formatting a DateTime based on a specific culture.
DateTime myDateTime = DateTime.Now; CultureInfo customCulture = new CultureInfo("fr-FR"); string formattedDateTime = myDateTime.ToString("F", customCulture); Console.WriteLine($"Formatted DateTime (French culture): {formattedDateTime}");
Short date and long date format in C#: Representing a DateTime in short and long date formats.
DateTime myDateTime = DateTime.Now; string shortDateFormat = myDateTime.ToString("d"); string longDateFormat = myDateTime.ToString("D"); Console.WriteLine($"Short Date Format: {shortDateFormat}, Long Date Format: {longDateFormat}");
Formatting DateTime for file names in C#: Creating a string representation of a DateTime suitable for file names.
DateTime myDateTime = DateTime.Now; string formattedForFileName = myDateTime.ToString("yyyyMMdd_HHmmss"); Console.WriteLine($"Formatted for File Name: {formattedForFileName}");