The DateTime Data Type in C#: Everything you need to know
Tags: dotnet, csharp, beginners, dotnetcore
Published: 2023-05-17 15:01:46

...

## Hey there! ✋🏻
If you're looking for a beginner friendly guide on date and time in C#, look no further, this post is for you, let's get started right away!

## Why should you learn how to use Date and Time though you may ask? Well

Dates are everywhere, wether you are filling out a form, signing up to a social media app, creating a new email, they're ultimately ubiquitous.

In this post, I'll be explaining the **DateTime** data type in C#, from creating a DateTime object, to using its properties and methods, then seeing how we can format dates.

Here is an outline of the points we're going to cover in this post:

🔫 What's the DateTime data type
🔫 How to create a DateTime object
🔫 Using the DateTime properties and methods
🔫 How to format dates


## 1: What's the DateTime data type?
DateTime is a **struct**, a value type like **int, double, float etc..**. It's used to create an instance of time, we an use it to represent the current time, or a specific date and much more.

## 2: How to create a DateTime object?
We create a new instance simply like how we do with any other type, we use the **new** keyword. This example demonstrates instantiating a DateTime object.

```
// When you invoke the constructor, provide the date you want to create like this
// (year, month, day, hour, minute, second)
var date = new DateTime(2023, 5, 17, 10, 33, 50);

```

This line creates a new DateTime object that refers to a random hour on the day on which I'm writing this post, you can do a console write line to output the date to the console and see how it looks, the output should look something like this:
`5/17/2023 10:33:50 Am`

We can create other date objects using the DateTime properties and methods, which's what we'll touch on next.

## Using the DateTime properties and methods
Let's take a look at some useful properties, we'll begin with the **Now** property, this property gets the current date and time on the computer that's being used, here's an example:

```

var now = DateTime.Now;
//VALUE: 5/17/2023 4:34:25 PM

```

The output is representative of the time on my device, referring to the time of writing the example.
However, you may ask that how did we use the DateTime struct without creating a DateTime instance? Well, this property is static and therefore we are not supposed to create a new object to access it.

The returned date from the **Now** property will differ based on your time zone and perhaps you may want to use something more standardized. Enter the **UtcNow** property. UTC stands for **C**oordinated **U**niversal **T**ime, which is a world standard, to use it, similar to the Now property we used earlier, like this:

```

var utcNow = DateTime.UtcNow;
//VALUE: 5/17/2023 1:47:24 PM

```
That's the current UTC time while I'm writing this.

Another one is the **Today** property, which gets the date of the current day, irrespective of the time, the hourly time will be set to **12:00:00 AM**:

```

var today = DateTime.Today;
//VALUE: 5/17/2023 12:00:00 AM

```

Furthermore, you can find some useful properties, for instance, **DayOfWeek**, this returns the name of the current day, if I call this property now, it'll return **Wednesday**.
**DayOfYear** returns the number of the day in the year, **137** as for the ongoing day.

Another property is **TimeOfDay**, let's look at it in code:

```

Console.WriteLine(DateTime.Now.TimeOfDay);
//OUTPUT: 17:01:22.8994140

```
This property gets the time that has elapsed since midnight.

If you want to experiment with the other properties, just create a date instance, and call the properties on it to get a feel for yourself.

**Now we'll move to some DateTime methods**
To begin with, we'll see how we can add days to an existing date, let's add a day to the current day which recall we can acquire through the **Today** property:

```

var now = DateTime.Today;
//VALUE: 5/17/2023 12:00:00 AM

var tomorrow = now.AddDays(1);
//VALUE: 5/18/2023 12:00:00 AM

```
Similarly, we can add years, months, hours, seconds, even microseconds and more.

We've seen how to add time to a date, how about subtracting? Well, same methods, just use negative numbers, simple as that! 
Want to a subtract a day, this'll do it;
`var yesterday = DateTime.Now.AddDays(-1);`

Alright, let's move on!

## How to format dates?
We can use some built-in methods in the DateTime struct to format dates, let's take a look at them, I'll put them all in one code block with their respective outputs for the sake of reducing clutter:

```

var now = DateTime.Now;

Console.WriteLine(now.ToShortDateString());
//OUTPUT: 5/17/2023

Console.WriteLine(now.ToShortTimeString());
//OUTPUT: 5:11 PM

Console.WriteLine(now.ToLongDateString());
//OUTPUT: Wednesday, May 17, 2023

Console.WriteLine(now.ToLongTimeString());
//OUTPUT: 5:12:55 PM

```
The developers who wrote these methods named them quite descriptively, the names are clear enough to speak for the output. The DateTime instance I created isn't obligatory though, we could've straight away used the **Now** static property to get our desired formatted output, but just to shorten the statements a bit I decided to take this route.

There's still more to formatting, we can use custom date time formatting as well, let's have a look at this snippet: 

```

var randomDate = new DateTime(2014, 8, 21);

Console.WriteLine(randomDate.ToString());
//OUTPUT: 8/21/2014 12:00:00 AM

Console.WriteLine(randomDate.ToString("dd-MM-yy"));
//OUTPUT: 21-08-14

Console.WriteLine(randomDate.ToString("dd/MM/yy"));
//OUTPUT: 21/08/14

Console.WriteLine(randomDate.ToString("MM/dd/yyyy"));
//OUTPUT: 08/21/2014

```

These are just few formats, there're plenty of ways which we could've used to format our date. If you're interested in checking out the rest of the available formatting options, you can refer to this link: 
https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings

 
That's about it for this post, I hope you enjoyed and learned something new, if you have any questions or topics you want me to write about, drop them in the comments!

## Bye For Now👋🏻





...