C# date & time conversion CheatSheet
Maiko Miyazaki
Posted on April 28, 2021
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.
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 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.
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.
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.
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.
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.
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.FindSystemTimeZoneById
to 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.
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.
Download PDF file
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
Posted on April 28, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.