# C# Convert a String to DateTime and Format It Back

> Use DateTime.ParseExact with an explicit format string and InvariantCulture to parse a date string, then ToString with a custom format to convert it back

**URL:** https://sentry.io/answers/in-c-sharp-how-to-convert-a-string-to-a-datetime-and-format-a-datetime-as-yyyymmddhhmmss-format/

---

## 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:

```c#
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:

```c#
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:

```c#
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](https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings). 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:

```c#
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:

```c#
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](https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings).

---

*Source: [sentry.io/answers/in-c-sharp-how-to-convert-a-string-to-a-datetime-and-format-a-datetime-as-yyyymmddhhmmss-format/](https://sentry.io/answers/in-c-sharp-how-to-convert-a-string-to-a-datetime-and-format-a-datetime-as-yyyymmddhhmmss-format/)*
