DEV Community

Cover image for C# date & time conversion CheatSheet
Maiko Miyazaki
Maiko Miyazaki

Posted on

C# date & time conversion CheatSheet

When dealing with data, we often find ourselves handling date and time. Although it is not particularly difficult, I often find myself googling the same conversion over and over again.

This post will share a cheat sheet of date and time conversion to eliminate such little time-consuming procedures and explain each technique in the following sections.

Open in PDF

C sharp datetime conversion cheat sheet

Table of Contents

1️⃣ String to DateTime object
2️⃣ String that is not parsable with Parse() to DateTime object
3️⃣ DateTime Object to String
4️⃣ String to String
5️⃣ DateTime Object to Unix Timestamp in Seconds
6️⃣ Unix Timestamp in Seconds to DateTime Object
7️⃣ UTC timezone String to DateTimeOffset object in a different timezone
8️⃣ 24-hour time String to 12-hour time String
9️⃣ Download PDF file


String to DateTime object

Input Example: String "31-12-2021"
Output: Datetime object "31/12/2021 00:00:00"

We can use DateTime.Parse() to create a DateTime object from a string. Parse method takes target string as an argument.

String to DateTime object with parse method


String that is not parsable with Parse() to DateTime object

Input Example: String "31122021"
Output: Datetime object "31/12/2021 00:00:00"

We can use DateTime.ParseExact() to create a DateTime object from a string that is not parsable with Parse() method. ParseExact() takes three arguments -- target string, a string to explain the format, and Format Provider which pecifies format of the DateTime conventions associated with language, region, calendar, etc.

String to DateTime object with ParseExact method


DateTime object to String

Input Example: DateTime object "31/12/2021 00:00:00"
Output: String "2021-12-31"

We can use ToString() methodto convert a DateTime object into String. The method takes desired string format as an argument.

DateTime object to String


String to String

Input Example: String "31122021"
Output: String "2021-12-31"

To convert a string into a different format of a string, we first need to convert it into a DateTime object. Then we can use ToString() methodto specify a format and convert it into a string.

String to String


DateTime Object to Unix Timestamp in Seconds

Input Example: DateTime Object "31/12/2021 00:00:00"
Output: Long 1640908800 (Unix Timestamp in seconds)

To convert DateTime Object into Unix Timestamp in seconds, we first need to convert the DateTime into a DateTimeOffset object. Then we can use ToUnixTimeSeconds() methodto convert it into a timestamp in Long.

DateTime Object to Unix Timestamp in Seconds


Unix Timestamp in Seconds to DateTime Object

Input Example: Long 1640908800 (Unix Timestamp in seconds)
Output: DateTime object with "31/12/2021 00:00:00"

To convert a Unix timestamp (in seconds) into a DateTime object, we first need to convert the timestamp into a DateTimeOffset object. Then we can use DateTime propertyto get the DateTime object.

Unix Timestamp in Seconds to DateTime Object


UTC timezone String to DateTimeOffset object in a different timezone

Input Example: String "31-12-2021 08:33:00 -0400" (New York timezone)
Output: DateTime object "31/12/2021 21:33:00 +09:00" (Tokyo timezone)

There are two main steps to convert a UTC timezone String into a DateTimeOffset object in a different timezone.

Firstly, we need to calculate the UTC offset of the output timezone. To do so, we can use TimeSoneInfo.FindSystemTimeZoneByIdto get the TimeZoneInfo object, and then calculates the offset using GetUtcOffset() method.

Next, we create a DateTimeOffset object from the input string using DateTimeOffset.ParseExact(). After that, convert the DateTimeOffset using ToOffset() method with the offset calculated in the first step.

UTC timezone String to DateTimeOffset object in a different timezone


24-hour time String to 12-hour time String

Input Example: String "20:00"
Output: String "08:00 pm"

We first need to convert String into a DateTime object. Right after, 12-hour time string can be created using ToString method.

24-hour time String to 12-hour time String


Download PDF file

Open PDF


Thanks for reading. If you have any opinion or question, please leave a comment below! In the meantime, please follow me on Linkedin @Maiko Miyazaki or Twitter @Maiko Miyazaki

Resources

Converting between DateTime and offset - Microsoft docs
DateTimeOffset.ToOffset - Microsoft docs
List of timezone IDs for use with findtimezonebyid - Stackoverflow

Top comments (2)

Collapse
 
canro91 profile image
Cesar Aguirre

Thanks Maiko, this is something I always have to look up.

Collapse
 
maikomiyazaki profile image
Maiko Miyazaki

Thanks for your comment Cesar! I hope this helps 😊