C# – Working with Date and Times

If you follow me a bit,  you are aware of that I jump directly into the subject without saying much so let’s go on.

In order to create a new DateTime object 

DateTime myDateTime = DateTime.Now;

DateTime myDateTime  = DateTime("12/01/1980");

 

Formatting ….. many options;

myDateTime.To______();

Retrieving parts;

myDateTime.Year  // int

myDateTime.Hour // int

myDateTime.DayOfWeek //Monday

myDateTime.DayOfYear //int 1981

 

DateTime Math:

myDateTime.AddHour(3)

myDateTime.AddMinutes(-5)

“Chaining” = using multiple methods together with . operator

myDateTime.AddHour(3).AddMinutes(-5).ToString();

 

 

 

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: