DateTime and TimeSpan are actually the structs under the hood. DateTime is used to point the specific time and timespan is used for the duration. Both are immutable (which can’t be change) means that if we initialize the DateTime or TimeSpan variable then we can’t change its value if we need to increment the value or adding days or time then we need to create another variable.
DateTime
As we already discussed intellisense is a very beautiful tool in the Visual Studio through we can learn programming. We have don’t any need to know which parameter was, what value we need to place here but Intellisense automatically guides us how to insert the value here.
Look how intellisense guides us how to insert the data in datetime object.
static void Main(string[] args)
{
DateTime dateTime = new DateTime(2018, 5, 12, 23, 58, 59);
// it returns the complete information of date and time
DateTime dateTime1 = DateTime.Now;
Console.WriteLine(dateTime1); // 4/2/2018 6:49:07 PM
// it just returns the information of date
DateTime dateTime2 = DateTime.Today;
Console.WriteLine(dateTime2); // 4/2/2018 12:00:00 AM
// it returns the date of today
DateTime dateTime3 = DateTime.Today.Date;
Console.WriteLine(dateTime3); // 4/2/2018 12:00:00 AM
DateTime dateTime4 = DateTime.Now.Date;
Console.WriteLine(dateTime4); // 4/2/2018 12:00:00 AM
// But if we try to get the hour then
int h1 = DateTime.Today.Hour;
Console.WriteLine(h1); // 0
// it is returning 0 because program can't justify the exact value
// of hours, today.
// Here program can understand the number of hour because of 'Now'
int h2 = DateTime.Now.Hour;
Console.WriteLine(h2); // 19
bool leapYear = DateTime.IsLeapYear(dateTime.Year);
Console.WriteLine("This Year is Leap Year {0}", leapYear);
// To add the days in dateTime variable
DateTime dateTime5 = dateTime1.AddDays(2);
Console.WriteLine(dateTime5);
int days = DateTime.DaysInMonth(dateTime1.Year, dateTime1.Month);
Console.WriteLine("Total days in month: {0}", days); // 30
// To find out the day of the week
DayOfWeek day = DateTime.Now.DayOfWeek;
Console.WriteLine(day);
Console.ReadLine();
}
This is how datetime works. In short, datetime is used for pointing the data and time. We get the date time information from DateTime object.
We don’t any need to add .ToString() with all of the objects to print them on the console. Because we’re passing the variable into Console.WriteLine() and it (.ToString()) is by default here.
To print tomorrow and yesterday day,
We don’t any need to add .ToString() with all of the objects to print them on the console. Because we’re passing the variable into Console.WriteLine() and it (.ToString()) is by default here.
To print tomorrow and yesterday day,
static void Main(string[] args)
{
DateTime dateTime = DateTime.Now;
// Tomorrow
Console.WriteLine(dateTime.AddDays(1).DayOfWeek); // Tuesday
// Yesterday
Console.WriteLine(dateTime.AddDays(-1).DayOfWeek); // Sunday
Console.ReadLine();
}
Custom Date And Time Format Strings
As we need to print the date and time on the console in specific format so we need to manually attach the .ToString() and custom format specifier with it.
As we can see how intellisense helps us to specify the format here and telling us the results as well. All this is the beauty of Visual Studio Intellisense feature and Resharper. Now we need to just specify the letter or letters in .ToString()
As we can see how intellisense helps us to specify the format here and telling us the results as well. All this is the beauty of Visual Studio Intellisense feature and Resharper. Now we need to just specify the letter or letters in .ToString()
static void Main(string[] args)
{
DateTime thisDate = new DateTime(2018, 8, 17);
Console.WriteLine(thisDate.ToString("d"));
Console.ReadLine();
}
To print all the date patterns on the screen.
static void Main(string[] args)
{
if (DateTimeFormatInfo.CurrentInfo != null)
foreach (var pattern in DateTimeFormatInfo.CurrentInfo.GetAllDateTimePatterns('d'))
{
Console.WriteLine("{0}", pattern);
}
Console.ReadLine();
}
TimeSpan
Timespan represents the duration. And we can add this exact time of duration in our datetime object.
static void Main(string[] args)
{
DateTime today = DateTime.Today;
TimeSpan day = new TimeSpan(1, 2, 26, 41);
DateTime newDateTime = today.Add(day);
Console.WriteLine(newDateTime);
Console.ReadLine();
}
This is how it works.Note:
Do you notice when we’re working with DateTime then to add days, hours, minutes we were using datetime.AddDays, datetime.AddHours But now we’re working with Timespan we just use the .Add()
today.Add(day);
DateTime and TimeSpan are Immutable
As we already know that DateTime and TimeSpan both are immutable. When we initialize their values then we can’t change them later on. Look this piece of code doesn’t work as you expect.
static void Main(string[] args)
{
DateTime date = new DateTime(2010, 4, 29, 10, 25, 00);
TimeSpan t = new TimeSpan(1, 0, 0, 0);
date.Add(t);
Console.WriteLine("A day after the day: " + date);
Console.ReadLine();
}
Here ‘date’ value doesn’t change actually. And another thing is Add() returns a new DateTime object which we need to catch. So to resolve above problem, we’ve 2 solutions.
static void Main(string[] args)
{
DateTime date = new DateTime(2010, 4, 29, 10, 25, 00);
TimeSpan t = new TimeSpan(1, 0, 0, 0);
// 1st solution
date = date.Add(t);
Console.WriteLine("A day after the day: " + date);
// 2nd solution
Console.WriteLine("A day after the day: " + date.Add(t));
Console.ReadLine();
}