Sentry Answers>C#>

In C# how to convert a string to a DateTime, and format a DateTime as 'YYYYMMDDHHMMSS' format

In C# how to convert a string to a DateTime, and format a DateTime as 'YYYYMMDDHHMMSS' format

Richard C.

The Problem

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:

Click to Copy
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?

The Solution

To instantiate a DateTime, pass the constructor a number for each part of the date, from year to milliseconds:

Click to Copy
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.

Convert a String to a Date

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:

Click to Copy
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.

Convert a Date to a String

You can use these custom format components to create a format for printing dates to strings:

Click to Copy
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");.

Convert a Date to a String that is Culturally Aware

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:

Click to Copy
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.

  • SentryC# Error and Performance Monitoring
  • Syntax.fmListen to the Syntax Podcast
  • Syntax.fm logo
    Listen to the Syntax Podcast

    Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.

    SEE EPISODES

Considered “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.

© 2024 • Sentry is a registered Trademark of Functional Software, Inc.