Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to convert C# DateTime to "YYYYMMDDHHMMSS" format?
Converting a DateTime object to the "YYYYMMDDHHMMSS" format in C# is commonly needed for timestamps, file naming, and database operations. This format provides a compact, sortable representation of date and time without separators.
The ToString() method with custom format strings allows you to convert DateTime objects to any desired string format using specific format specifiers.
Syntax
Following is the syntax for converting DateTime to "YYYYMMDDHHMMSS" format −
DateTime dateTime = DateTime.Now;
string formattedDate = dateTime.ToString("yyyyMMddHHmmss");
The format specifiers used are −
yyyy− Four-digit yearMM− Two-digit month (01-12)dd− Two-digit day (01-31)HH− Two-digit hour in 24-hour format (00-23)mm− Two-digit minute (00-59)ss− Two-digit second (00-59)
Using DateTime.ToString() Method
Example
using System;
class Program {
static void Main() {
DateTime d = DateTime.Now;
string dateString = d.ToString("yyyyMMddHHmmss");
Console.WriteLine("Current DateTime in YYYYMMDDHHMMSS format: " + dateString);
// Using a specific date
DateTime specificDate = new DateTime(2023, 8, 15, 14, 30, 45);
string specificDateString = specificDate.ToString("yyyyMMddHHmmss");
Console.WriteLine("Specific DateTime: " + specificDateString);
}
}
The output of the above code is −
Current DateTime in YYYYMMDDHHMMSS format: 20240115143027 Specific DateTime: 20230815143045
Using Different DateTime Sources
Example
using System;
class Program {
static void Main() {
// Using DateTime.Now
string nowFormat = DateTime.Now.ToString("yyyyMMddHHmmss");
Console.WriteLine("DateTime.Now: " + nowFormat);
// Using DateTime.UtcNow
string utcFormat = DateTime.UtcNow.ToString("yyyyMMddHHmmss");
Console.WriteLine("DateTime.UtcNow: " + utcFormat);
// Using DateTime.Today with time
DateTime todayWithTime = DateTime.Today.AddHours(9).AddMinutes(15).AddSeconds(30);
string todayFormat = todayWithTime.ToString("yyyyMMddHHmmss");
Console.WriteLine("Today with specific time: " + todayFormat);
}
}
The output of the above code is −
DateTime.Now: 20240115143027 DateTime.UtcNow: 20240115193027 Today with specific time: 20240115091530
Common DateTime Format Variations
| Format String | Example Output | Description |
|---|---|---|
| yyyyMMddHHmmss | 20230815143045 | YYYYMMDDHHMMSS format |
| yyyy-MM-dd HH:mm:ss | 2023-08-15 14:30:45 | ISO 8601 format |
| MM/dd/yyyy HH:mm:ss | 08/15/2023 14:30:45 | US format with time |
| dd/MM/yyyy HH:mm:ss | 15/08/2023 14:30:45 | European format with time |
Using Custom Method for Reusability
Example
using System;
class DateTimeHelper {
public static string ToTimestampFormat(DateTime dateTime) {
return dateTime.ToString("yyyyMMddHHmmss");
}
public static string ToTimestampFormat() {
return DateTime.Now.ToString("yyyyMMddHHmmss");
}
}
class Program {
static void Main() {
// Using helper method with current time
string currentTimestamp = DateTimeHelper.ToTimestampFormat();
Console.WriteLine("Current timestamp: " + currentTimestamp);
// Using helper method with specific date
DateTime customDate = new DateTime(2023, 12, 25, 18, 45, 30);
string customTimestamp = DateTimeHelper.ToTimestampFormat(customDate);
Console.WriteLine("Custom timestamp: " + customTimestamp);
}
}
The output of the above code is −
Current timestamp: 20240115143027 Custom timestamp: 20231225184530
Conclusion
Converting C# DateTime to "YYYYMMDDHHMMSS" format is accomplished using the ToString("yyyyMMddHHmmss") method. This compact format is ideal for timestamps, file naming, and sorting operations where a standardized date-time representation is required.
