Welcome back.
All right.
Now it's time to learn a bit about date time.
As I said in the introduction, daytime is a really important struct that we will use essentially all
the time.
So let's make sure we know what we are talking about.
All right.
So let's get started.
In this video you will learn about DateTime, which allows us to use time and to get to time to get
a difference between a specific date and another date and so forth.
And therefore we just need to use the date time class.
And the date time class is automatically available if you have the system namespace included, which
is the case pretty much in any program that we're using, right.
So let's go ahead and use date time and create a date time object.
So I'm going to call that object date time.
And it's called date time because it not only gives us the date, but it also gives us the time.
So it gives gives us all the information about a date and the time up to the milliseconds.
So let's go ahead and create a date time object, which we did here.
And now we initialize it.
And let's say I want to only have the year, month and day.
Then I can just enter something like 2018, five and 31, which is my birthday or was my birthday this
year.
So I'm using that in order to find out how many days I've lived.
For example, we will write a little program later on which will tell us how many days we've been on
this earth, which is pretty interesting because I looked into how many days I've lived, and I figured
out that very soon I'm going to be 11,111 days old.
And funnily, a friend of mine, a very good friend from childhood, he will be 11,111 days old on the
11th of November.
And in Cologne, where I come from, Carnival is pretty big and it starts on 11th at 11:00 and 11 minutes.
So that's on in November.
So it's pretty funny that he has this very crazy date pretty much for his 11,111th day.
So maybe you figured out what the interesting day for you is, or a cool day for you is to celebrate.
So you won't only celebrate your birthday, but maybe also your 10,000th day or your 15,000th day on
earth, or however old you are.
So maybe you figured something out.
All right, enough side talk.
Let's get back to date time.
So I have created this little date time object, and now I can write something onto the console.
And I'm going to say something like, my birthday is and I enter the birthday here.
And what do I give over or hand over I give over my date time object.
Now.
My birthday this year was in 2018, but I was born in 1988.
So if I want to be more precise, of course I have to enter that.
All right, so let's run it.
Control F5 in order to run this application and keep it open.
There we are.
My birthday is.
And there you see that the structure is 531 2000 or 1988.
And then it even gives us the hour, minute and second and am here.
So we didn't specify that, but it gives all this information because we asked for the date time and
it contains this information even though we didn't specify it ourselves.
And if we create a date time object, let me go back here.
Then we can go ahead and have multiple different overloads.
So let me create one.
As you can see there are eight overloads.
So one which is just an empty date time.
Then we have one with long ticks and ticks, a date and date and time expressed and the number of 100
nanosecond intervals.
Then we have the date time kind.
So you can also define the kind of date time that you have int year and month and then day.
That's the one that we used.
Then you can also specify the hour minute and second and also the date time kind here as well in the
different overload.
And here you can even go as far as specifying the millisecond.
And finally also with the date time kind.
Now you might wonder what this date time kind is, and it's a property which gets a value that indicates
whether the time represented by the instance is based on local time, Coordinated Universal Time, UTC,
or which is called UTC or neither.
All right.
So let's get back to our example.
And I'm going to enter or I'm going to go back to my values that I had here.
And now I'm going to show you how you can display the current date.
So right today on screen.
So if you want to have the date of today you can simply use date time dot today.
What that will do is it will give you the current date.
So let's run that again.
Control F5.
There we are.
Today is the 12th of June.
And well the time is not correct here but well it still has the current time or it still has the time
in here if you want to have the specific time.
So right current time on screen then you use date time dot now.
So there's this property gets DateTime object that is set to the current date and time on this computer
expressed as local time.
And let me run that again.
And there you are.
It's one 5030 5 p.m..
All right.
So if you want to display the current time or the time that is specified in the on the computer, in
your application or in your program, you can simply use Datetime.now.
Now you also can get the day of tomorrow so you can get the following day, but therefore you need to
create a little workaround.
So there is no property which says tomorrow.
Let's check it out.
DateTime dot two more row.
As you can see, it doesn't have that.
It has two day and you can generally check out which properties there are now, today and UTC now,
which is this kind that I've just shown you.
And then there are also methods so you can find out whether it's a leap year.
Then you can specify the kind days in month.
So how many days are in a specific month and year.
Because as you know, February can have either 28 or 29 days.
Then March has 31 days, April has has 30 days and so forth.
So this can be very useful.
And there's a method for that.
Now let's assume we want to have the day which is or the date of tomorrow.
And and in order to get that we will need to write a little method here.
It's going to be static and it's going to return date time.
And I'm going to call it get tomorrow.
And what it will do is it will simply return date time dot today.
And now today we can add a day.
So you can see we can add seconds to six years, we can compare it and so forth.
And the one that I want is I want to add days.
As you can see under add there are multiple different solutions here or options.
And I'm going to use ad days and say one.
So I'm just going to add one day two tomorrow and I'm going to return that.
So now we can go ahead and use that method in order to apply the day of tomorrow or the date of tomorrow.
So I'm going to create a new date time object and I'm going to call it tomorrow.
And that will be my tomorrow, which I get by using the get tomorrow method.
So there we are now.
Our date time tomorrow can be used in order to be displayed.
So let's just display tomorrow.
Tomorrow will be the and then the tomorrow object.
Now let's display that again.
And there we are.
Tomorrow will be the 13th of June 2018.
Now for you as a little challenge right onto the screen, which day of the week we have.
So it should be pretty simple.
I haven't used it yet, but you can simply go ahead and check out the methods and properties of DateTime
to get that.
So please go ahead and try that.
All right I hope you tried it.
So let's go ahead and write it onto the console.
Today is and then we use today.
So date time dot today dot day of week.
Get the day of the week represented by this instance.
All right.
So let's print that onto the screen.
And there we are.
Today is Tuesday perfect I hope you could manage to do that.
Now let's assume we want to get the first day of a specific year so we can go ahead and use static again.
Date time.
So I'm creating a method here and it's going to be get first day of year.
And in here we could either use a date time and just show the date for the first day of that year.
Or we can make it very easy and use an integer.
So in here I'm just going to enter a year and it will return a date time.
So it will return date time with year one and one.
Because the 1st of January is the first day of the year.
So with this one, I'm going to get the first day of a year, and I can simply say of which year I want
to have the first day.
And that's pretty neat, because now we can go ahead and display the first day of a specific year.
So I'm just going to say first day of year.
And that will be let's say 1999.
And there we are 1st of January 1999.
You have seen that.
You can find out the day of the week, but you can also find out the days of the month as as I also
stated.
Let's check it out.
Let's create a variable called days and it will be date time dot days in month and let's say February
in 2000.
How many days did that day have?
And now I'm going to write that onto the console and days in Feb February 2000 were so and so.
Now I want to go ahead and check it out for other years as well.
So now let's check out 2001.
So I'm just going to say days, Days and month 2001.
And it's going to be February 2001 and now 2004.
So let's check that out again and it will be 2004.
So let me print it onto the screen.
And there we are.
As you can see 2000 is a leap year and 2004 as well.
So in 2001, 2002 and so forth, February only has 28 days, and each time that it's dividable by four,
it has 29 days, even though that's not exactly precise.
So there are years where it's skipped.
So every I think 100 years it's skipped, and then once every 400 years it's skipped and so forth because
the well, the Earth doesn't actually need 365 days in order to fly around or to make a whole circle
around the sun, it needs a little more.
So it needs roughly a quarter day more.
but it's not exactly a quarter day more.
It's something like 0.24 days on top.
That's why it has to be.
Well, as you can see, skipped.
Well you can't see it here, but it has to be skipped anyways.
All right.
So now let's check out how we can get the current time but in a specific manner.
So let's go ahead and do that.
I'm going to create a date time which I'm going to call now.
And that will be Datetime.now.
And now I want to display the current minute.
So I'm going to write something like minute is zero and that will be now dot minute.
So there is this property called minute.
Then there is one that is called hour and also seconds.
And let's display that they die now and it says minute is six.
So it's 2:00 and six minutes.
And that's what you can see.
Minute six.
All right.
Now a little challenge for you display the time in this structure.
So so many o'clock then and so many minutes and Z seconds.
So something like 5:00 and 21 minutes and 30s.
So maybe 5:00.
So and so many minutes and so many seconds.
So please go ahead and display that onto the console.
All right I hope you tried it.
And I'm going to say so o'clock then.
So minutes and.
Seconds and that will be now dot hour.
Now dot minute and now dot second.
All right.
So let's display that or let's run that to see what the current time is.
And there we are, 14:00, eight minutes and 22 seconds.
That's the same thing that you have here.
So 2:00 eight minutes, 22 seconds.
All right.
Next we're going to write a little program which is going to tell us how much time is between now and
a time that we enter.
So and the date that we enter to be precise.
So write a date in this format and it's going to be y y y standing for year year year month month day
day.
So that's what we're going to display to the user.
And then we get it as input which will be console dot read line.
And then we check if that worked out.
So if we can make a date time out of whatever was entered.
So as you can see, date time has a try parse method which now can go ahead and try to make sense out
of the input that the user has entered.
And it's going to have an output which will be date time.
And now I will use that date time.
So I'm going to write date time onto the console.
And I'm going to create a time span.
And that time span will have the days passed.
So however many days have passed since the date that was entered or will pass until that day comes that
was entered.
So what I do now is I use the now variable that we created earlier on.
So this now and I'm going to subtract.
So there's this method subtract.
And what it does it subtracts the specified date and time from the instance.
So I want to subtract the date time.
That's the output that we get.
So whatever the user has entered which in our case will be we and will be taken and that will be well
used in order to calculate this days past.
And now finally I want to display that.
So I'm going to say days passed since and it will be this past dot days.
So this time span also has a days property which we can use in order to get the days component of the
time interval represented by the current time span structure.
All right.
So if it didn't work out we're just going to say wrong input.
All right let's try that.
Let's run it right a date in this format.
So let's say I want to know what happens since 1776.
Then month it will be July zero seven and the day will be zero four.
So that's the Independence Day.
And now let's see how much time has passed since.
And there you are, 88,366 days.
So that's a perfect example of a great celebration on the 88,888th day.
Isn't that a great idea?
If nobody does that, you should pick that up and create a big eight party, which will which will,
uh, be in roughly a year and three months.
So that should be something like October or September 2019.
And if you watch this afterwards, then you miss the opportunity to create a party which will be on
the 88,888th day after the Independence Day.
All right, so now a little challenge.
I know this video is long, but bear with me.
So please go ahead and write a program where somebody can enter his birthday and it will calculate the
days passed since.
And it's not a difficult one.
It's pretty simple actually, because now you've seen how it works.
So please go ahead and try that.
All right I hope you did.
So the only thing that I'm going to change is I'm actually changing the text right to date in this format.
And write your birth date.
Date.
Is it birth date?
Probably it's birth date in this format.
So now it's the same thing.
But now it says now days passed since.
But something like you are this amount of days old, you are so and so days old.
Now let's test that and write your birth date in this format.
All right.
1990 or 88, in my case, May and 31.
And I am 10,969 days old, and you can go ahead and share your days on earth with us if you want to.
Or you can share your plans for the big celebration of the Independence Day at 8880 Days Later.
All right, so I hope you liked this video, and I hope you can get some cool ideas out of this and
see you in the next video.