In C# how to convert a string to a DateTime, and format a DateTime as 'YYYYMMDDHHMMSS' format
Richard C.
—In C#, how do you convert a string into a DateTime object? For example, how do you convert a specific string format like the one below into a date:
var dateString = "2023-12-11 14:36:05,390";
Also, given a date, how do you format it as a string, such as 20231211143605
in the form YYYYMMDDHHMMSS
?
To instantiate a DateTime, pass the constructor a number for each part of the date, from year to milliseconds:
using System; public class Program { public static void Main() { var date = new DateTime(2023,12,11, 14,36,5, 390); Console.WriteLine(date); // 12/11/2023 14:36:05 } }
The parameters after day are optional. Excluding them will set your day’s time to midnight.
Instead of constructing a DateTime manually, you can pass it a string. We recommend always using ParseExact
to validate the format of your date string:
using System; using System.Globalization; public class Program { public static void Main() { var dateString = "2023-12-11 14:36:05,390"; try { var date = DateTime.ParseExact(dateString, "yyyy-MM-dd HH:mm:ss,fff", CultureInfo.InvariantCulture); Console.WriteLine(date); // 12/11/2023 14:36:05 } catch { // handle incorrect string error } } }
Here we specify the exact format of the string, and we ignore any regional differences, like commas versus periods for decimal placeholders, with InvariantCulture
. If your string is expected to have commas, the parse will disallow periods.
Do not use DateTime.Parse(dateString);
for any important work because the string you have might be in an unexpected format that parses into a DateTime without error, but is not the date you expected.
If you have a complex custom date format, you can specify it exactly using .NET custom string components. Note that M
is month and m
is minute. H
is the 24-hour format for hours and h
is the 12-hour format.
You can use these custom format components to create a format for printing dates to strings:
using System; public class Program { public static void Main() { var date = new DateTime(2023, 12, 11, 14, 36, 5, 390); var dateString = date.ToString("yyyy-MM-dd HH:mm:ss,fff"); Console.WriteLine(dateString); // 2023-12-11 14:36:05,390 } }
So a form like YYYYMMDDHHMMSS
can be represented as date.ToString("yyyyMMddHHmmss");
.
If your application is going to be used in multiple countries that have different defaults for displaying the date, it’s better to use a standard .NET date format and specify the country, than to use a custom format string. Your custom format won’t change from country to country automatically and may confuse users. Here’s how to specify a country-aware standard format:
using System; using System.Globalization; // to use CultureInfo public class Program { public static void Main() { var date = new DateTime(2023, 12, 11, 14, 36, 5, 390); var us = new CultureInfo("en-US"); var usString = date.ToString("f", us); Console.WriteLine(usString); // Monday, December 11, 2023 2:36 PM var uk = new CultureInfo("en-GB"); var ukString = date.ToString("f", uk); Console.WriteLine(ukString); // Monday, 11 December 2023 14:36 } }
You can get the country your application is currently running in using CultureInfo.CurrentCulture;
.
The full list of standard format strings is documented here.
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.