High again in this lesson I'm going to show you how we can use the user interface to choose a date as
well as comparing it.
This is very useful for any situation that you have to write something to a database or if your application
merely allows your users to choose a certain date for an appointment or anything of the sorts.
So let's go out into scope and I'm going to start a new application and then call it dates app and the
only item I need is my storyboard in the dates up is an object called you I'd date picture of you you
I'd date picture of you when I add one of those on set all the constants for its not right off the bat.
You can see that date because they actually have something called a mode and in them what you can choose
it to be data and time so be date only time only or making the counter that counts time down.
Very similar to what you have on your default app on your iPhone.
So I'm working with other default one which is the date and time and I'm going to add a function for
it.
So I I open my assistance window don't need to view it.
Lord what I do need is adding a new function in here that is an action let's call it date changed.
So every time the value changes we're gonna be calling this function.
Now I'm going to go back to my coding area and in here I'm going to make a little change to my function.
And that's just to simplify my own life.
I'm going to say send there is a new I date picture of you because I know it is going to be a UI data
picture otherwise in here I have to make an if lit or a guard to make sure that the sender is of that
type.
So no they have done I'm going to say let's call this one received a date.
I'm going to say that is send their dot date and let's just print this.
Now you see what we get under and when I talk to you about how we have to modify it.
So I'm going to say received date is.
And it is the received date.
So let's make a format I usually use whenever I want to print some value and I place that and then I
print that value.
So there's going to be a string and that's going to be an actual value.
So let's run this application and now every time we choose a certain time or date on our interface we
should be able to see it printed out for us.
So now I have my interface loaded and if I go up here and show my console I'm going to go in here and
say why it doesn't move.
Okay.
So I'm coming here and say let's push it to yesterday 521 and it says 16th of October.
But the time here is actually not showing me the right time over there because this is based on a different
standard and the client's timing here right.
It really is 521 am.
And if you're wondering a record this was very early in the morning because there's less noise in the
streets.
So the actual time is 5 21.
We're showing you a totally different hour.
And that's because it is not showing me the local time.
So that's the first thing we have to we have to essentially fix.
So I'm going to come in here and say let's I'm going to call this one date a string is going to become
received.
Date the date that we have that description with locale and the local for it is Ennis locale dot Collins.
So I'm going to say convert this time and into a string and show it to me with the current localization.
Show it to me with this time that I am in it right now.
So let's go ahead and print this value a string.
Now he's going to be so why change it to another minute ahead.
It becomes Wednesday October 17 2018 at 524 which is the right time.
So the next thing becomes as you can see.
Wednesday October 17.
Blah blah blah.
Hope they get their specific values.
What if I wanted to check if the user has chosen the month of November.
Or if I wanted to see if the user has chosen the day that is the 20th or whatever the reason that you
had you wanted to break it into the components of the date.
And that's exactly why it's called it's called the components.
So I'm going to come in here.
Let me lower this little bit to get the components I first need to get the crayons calendar so I'm going
to say calendar becomes calendar dot current and then I'm going to go ahead and say there are two different
ways of doing this I'm sure you'll both of them.
The first one I'm going to say late day or hour or anything of the sort that I'm looking for.
Be calendar on my calendar dot component and the component I want to get it from calendar component.
And from a certain date.
So the calendar component that they want to get is that day.
In this case yes it is day for a second I doubted that.
And then the date that I want to get it from is the received dates.
So this is how we can get their specific value of their date and if they printed that I would say that
print day is day and it should show us that the specific date that this calendar is set out is this
current date.
And now when we change this.
It says days 18 which is correct.
Now the other day I wanted to show you if you wanted to get multiple components at once.
So I would say let let's call them comps that becomes calendar my calendar not it's actually called
Date component.
If I'm not wrong.
Yes date components.
It has two values in it.
One is which is the date you're trying to get from one that is the received date and is asking me for
a set of components for the calendar.
So I'm going to say give me a set in the set given me calendar.
Not that kind.
The actual calendar class that component not a year as well as calendar dot component dot months.
And you can just keep adding to it whatever type of components that you need and then you simply can
go ahead and say print again say comps year is comps.
Yes.
And then obviously now when we print is going to show me the year based on this value is giving me a
warning is thing that I'm not sure if this value exists because an optional one.
So we have to essentially force unwrap it but I'm going to do that in a second.
So Knight says copier is optional 2018 and I could just say force on wrap.
Now the next thing I would talk about is formatting a date and we use the formatting simply because
we want to be able to restore these values as a string to somewhere on the disk or a backend under one
to be able to fetch those values and compare them against other dates.
So let's go in and see how we're going to do that.
I'm going to say let this call this one date for mother.
That becomes a date for mother.
I mean let me call this one my date format.
So it's a little bit more distinct then the next thing is your newly created date format that object.
It has to have a format of its own.
So I'm going to say date for Mother not date format is actually becoming this not you.
I'm going to say yeah years months months day a hour hour minute minute and seconds seconds.
Now there are so many many formats out there and you can just google for them.
But the thing that is important is whatever format that you write in that's the same format that you
have to read in it.
Because otherwise the values that you have written and your reading will not be the same.
So whatever format you use you have to kind of stick with it for that reason and make a wise choice
about that.
If your application is in a way that the only thing that matters is the year and the month then don't
worry about all the other ones.
If it is about the various specifics I can then go for milliseconds on the nanoseconds.
I think there is something called a nanosecond let me check that first.
I can for you.
This calendar.
Nothing that calendar dot component dot.
Now as I can.
Yes.
If you want to be very very specific your application is about for instance making a bid on an item
for a sale.
Then it becomes very important.
So for the way you are doing make sure that you find the right format for your application and then
stick with it for the entirety of that application.
No they have the form either they can go ahead and say let's produce a string out of it.
So let date formatted string festering.
It becomes my date form either my date form either though this string of and then the dates that I want
to push into it is obviously the date that we have which is the one I appear received a date so now
this is the value that we can actually properly a store.
We can write it to our backend or to our local disk and then we can get it back and reformatted to be
an actual date.
Before I do that let's print it to see what we have in it.
I'm going to copy paste this and say date format.
There is they'd form others so they'd form either a string is they'd formatted a string and run this
application again.
Increase my console size and once my app is actually loaded I go ahead and choose another time.
It says format advisor this 20 18 10 17 a space five that 747 perfect.
So this is the value I can actually compare against.
Now this is obviously a string.
It doesn't understand 17th of the month of the year it understands 2 0 1 8 dash one zero dash one 7
as if it was a string of characters.
So the important thing now becomes how do they get the string of characters such as this one and convert
it into a date actually to make things more interesting.
I'm going to make one of these myself manually and then we're going to combat it.
So when I actually copy this so I don't make any mistakes.
I'm going to come in here and say let dummy date which is a string will be this value.
So this is now a string.
It's not an actual date.
So I'm going to change that to be 20 25 for instance so many years from now.
So here I'm going to have to convert this dummy date to be an actual date.
So let's go ahead and see how we will be doing that.
I'm gonna go in here and say let's dummy date I'm gonna change that value to a string.
So this is the actual date value of it.
That's the string value of a dummy date which is a date to become let's say my date form either dot
the date from a string and the string value is the dumbest string I remember my date format there is
using this format because that's the built in date format of it and obviously when it is trying to read
this value it's looking at this type of format and saying Can I convert that into a date.
Is of course giving a little error or saying that this value is optional.
Are you sure it exists.
And I'm going to say yes.
Force unwrap it now.
The next thing I want to do is I want to be able to compare this state to my dummy date with the actual
date that we had which is the received the date.
And like I said dates are comparable and equitable in three if you would simply say if dummy date is
received the date then go ahead and save print.
Same date.
Otherwise say different dates.
Now obviously these two have to be showing us that different days because this is set to several years
after that.
But I'm going to try it anyway.
And then my simulator loads up then I choose a new date.
We should be printing so many things as well as doing a comparison.
So it says different dates simply because one of them is the date set.
At this current time which is this value twenty eighteen ten 17 61 forty two twenty three the other
one is actually set at several years from now.
Now the other thing I wanted to show you is this this date again the value that we have in the dummy
dates.
If I want it printed we will get this.
Let's go ahead and say print let me copy that.
Say dummy dates print it and let's see what we get in it so let's change that.
And now here is my dummy date.
So as you can see the dummy date even though it was set at 537 it's actually showing us twelve thirty
seven and that's simply because the date is being read properly from wherever it is and it is shown
in the default time zone which is a GMT time zone and if you wanted to convert it to your timezone you
have to again do this thing which is getting the law called the current value of the local locale which
is the local time value of the local time zone.
Now this might seem a little work you're on but it actually is useful simply because if your users are
all over the world and they're using different time zones they still have to be writing in only one
format simply because otherwise there is no comparison.
Right now whatever the time is in here where I am in Canada in Vancouver is several hours before what
is in Europe and many many hours before what is in Australia for instance.
So this system makes sure that when we all write to the same network or the same backend all U.S. counts
for everything that they have to do in terms of comparing these dates against each other they all are
written in the same format.
So if have to use right two different things at this very moment from two different parts of the world
the dates are going to be similar or identical.
And that's actually a very useful thing because it makes sure that in terms of sorting and the accuracy
I was essentially icons for that and takes care of that for you.
So that was a quick introduction are a little bit longer than quick.
It actually became about 15 20 minutes.
What a introduction to how to use dates and how to use date form others and how to compare them against
each other.
Let's keep it up to here and move on to our next.