1
00:00:00,270 --> 00:00:06,000
High again in this lesson I'm going to show you how we can use the user interface to choose a date as

2
00:00:06,000 --> 00:00:07,640
well as comparing it.

3
00:00:07,740 --> 00:00:13,590
This is very useful for any situation that you have to write something to a database or if your application

4
00:00:13,590 --> 00:00:18,690
merely allows your users to choose a certain date for an appointment or anything of the sorts.

5
00:00:18,690 --> 00:00:27,390
So let's go out into scope and I'm going to start a new application and then call it dates app and the

6
00:00:27,390 --> 00:00:33,690
only item I need is my storyboard in the dates up is an object called you I'd date picture of you you

7
00:00:33,700 --> 00:00:42,900
I'd date picture of you when I add one of those on set all the constants for its not right off the bat.

8
00:00:42,920 --> 00:00:48,680
You can see that date because they actually have something called a mode and in them what you can choose

9
00:00:48,680 --> 00:00:55,610
it to be data and time so be date only time only or making the counter that counts time down.

10
00:00:55,610 --> 00:01:00,330
Very similar to what you have on your default app on your iPhone.

11
00:01:00,350 --> 00:01:06,110
So I'm working with other default one which is the date and time and I'm going to add a function for

12
00:01:06,110 --> 00:01:06,260
it.

13
00:01:06,290 --> 00:01:10,920
So I I open my assistance window don't need to view it.

14
00:01:10,960 --> 00:01:22,570
Lord what I do need is adding a new function in here that is an action let's call it date changed.

15
00:01:23,030 --> 00:01:27,210
So every time the value changes we're gonna be calling this function.

16
00:01:27,220 --> 00:01:33,760
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.

17
00:01:33,770 --> 00:01:35,750
And that's just to simplify my own life.

18
00:01:35,750 --> 00:01:42,730
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

19
00:01:42,760 --> 00:01:49,310
picture otherwise in here I have to make an if lit or a guard to make sure that the sender is of that

20
00:01:49,310 --> 00:01:50,300
type.

21
00:01:50,300 --> 00:01:56,390
So no they have done I'm going to say let's call this one received a date.

22
00:01:56,390 --> 00:02:02,550
I'm going to say that is send their dot date and let's just print this.

23
00:02:02,550 --> 00:02:07,200
Now you see what we get under and when I talk to you about how we have to modify it.

24
00:02:07,210 --> 00:02:09,690
So I'm going to say received date is.

25
00:02:11,150 --> 00:02:13,420
And it is the received date.

26
00:02:13,490 --> 00:02:19,880
So let's make a format I usually use whenever I want to print some value and I place that and then I

27
00:02:20,090 --> 00:02:21,500
print that value.

28
00:02:21,530 --> 00:02:25,130
So there's going to be a string and that's going to be an actual value.

29
00:02:25,170 --> 00:02:31,520
So let's run this application and now every time we choose a certain time or date on our interface we

30
00:02:31,520 --> 00:02:33,970
should be able to see it printed out for us.

31
00:02:35,540 --> 00:02:41,990
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

32
00:02:41,990 --> 00:02:46,790
say why it doesn't move.

33
00:02:47,380 --> 00:02:47,940
Okay.

34
00:02:48,110 --> 00:02:56,330
So I'm coming here and say let's push it to yesterday 521 and it says 16th of October.

35
00:02:56,710 --> 00:03:03,480
But the time here is actually not showing me the right time over there because this is based on a different

36
00:03:03,480 --> 00:03:06,020
standard and the client's timing here right.

37
00:03:06,060 --> 00:03:08,540
It really is 521 am.

38
00:03:08,630 --> 00:03:12,870
And if you're wondering a record this was very early in the morning because there's less noise in the

39
00:03:12,870 --> 00:03:13,590
streets.

40
00:03:13,590 --> 00:03:16,240
So the actual time is 5 21.

41
00:03:16,280 --> 00:03:18,230
We're showing you a totally different hour.

42
00:03:18,240 --> 00:03:21,540
And that's because it is not showing me the local time.

43
00:03:21,540 --> 00:03:24,860
So that's the first thing we have to we have to essentially fix.

44
00:03:24,890 --> 00:03:32,880
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

45
00:03:33,170 --> 00:03:33,750
received.

46
00:03:33,750 --> 00:03:44,130
Date the date that we have that description with locale and the local for it is Ennis locale dot Collins.

47
00:03:44,190 --> 00:03:51,630
So I'm going to say convert this time and into a string and show it to me with the current localization.

48
00:03:51,630 --> 00:03:54,950
Show it to me with this time that I am in it right now.

49
00:03:55,290 --> 00:03:58,350
So let's go ahead and print this value a string.

50
00:03:58,360 --> 00:04:04,290
Now he's going to be so why change it to another minute ahead.

51
00:04:04,290 --> 00:04:11,120
It becomes Wednesday October 17 2018 at 524 which is the right time.

52
00:04:11,220 --> 00:04:13,890
So the next thing becomes as you can see.

53
00:04:13,890 --> 00:04:15,510
Wednesday October 17.

54
00:04:15,510 --> 00:04:16,650
Blah blah blah.

55
00:04:16,650 --> 00:04:19,330
Hope they get their specific values.

56
00:04:19,380 --> 00:04:24,360
What if I wanted to check if the user has chosen the month of November.

57
00:04:24,420 --> 00:04:30,480
Or if I wanted to see if the user has chosen the day that is the 20th or whatever the reason that you

58
00:04:30,480 --> 00:04:33,840
had you wanted to break it into the components of the date.

59
00:04:33,960 --> 00:04:36,950
And that's exactly why it's called it's called the components.

60
00:04:36,990 --> 00:04:38,140
So I'm going to come in here.

61
00:04:38,150 --> 00:04:44,930
Let me lower this little bit to get the components I first need to get the crayons calendar so I'm going

62
00:04:44,930 --> 00:04:53,310
to say calendar becomes calendar dot current and then I'm going to go ahead and say there are two different

63
00:04:53,310 --> 00:04:55,700
ways of doing this I'm sure you'll both of them.

64
00:04:55,710 --> 00:05:01,470
The first one I'm going to say late day or hour or anything of the sort that I'm looking for.

65
00:05:01,530 --> 00:05:09,500
Be calendar on my calendar dot component and the component I want to get it from calendar component.

66
00:05:09,570 --> 00:05:11,640
And from a certain date.

67
00:05:11,730 --> 00:05:15,410
So the calendar component that they want to get is that day.

68
00:05:15,420 --> 00:05:19,280
In this case yes it is day for a second I doubted that.

69
00:05:19,290 --> 00:05:23,220
And then the date that I want to get it from is the received dates.

70
00:05:23,880 --> 00:05:31,350
So this is how we can get their specific value of their date and if they printed that I would say that

71
00:05:31,380 --> 00:05:39,450
print day is day and it should show us that the specific date that this calendar is set out is this

72
00:05:39,450 --> 00:05:40,530
current date.

73
00:05:42,690 --> 00:05:44,100
And now when we change this.

74
00:05:44,130 --> 00:05:47,220
It says days 18 which is correct.

75
00:05:47,220 --> 00:05:51,810
Now the other day I wanted to show you if you wanted to get multiple components at once.

76
00:05:51,810 --> 00:06:00,900
So I would say let let's call them comps that becomes calendar my calendar not it's actually called

77
00:06:00,900 --> 00:06:02,270
Date component.

78
00:06:02,280 --> 00:06:03,090
If I'm not wrong.

79
00:06:03,120 --> 00:06:05,100
Yes date components.

80
00:06:05,100 --> 00:06:06,710
It has two values in it.

81
00:06:06,750 --> 00:06:12,570
One is which is the date you're trying to get from one that is the received date and is asking me for

82
00:06:12,570 --> 00:06:15,390
a set of components for the calendar.

83
00:06:15,690 --> 00:06:20,100
So I'm going to say give me a set in the set given me calendar.

84
00:06:20,100 --> 00:06:21,090
Not that kind.

85
00:06:21,330 --> 00:06:32,380
The actual calendar class that component not a year as well as calendar dot component dot months.

86
00:06:32,600 --> 00:06:38,340
And you can just keep adding to it whatever type of components that you need and then you simply can

87
00:06:38,340 --> 00:06:48,360
go ahead and say print again say comps year is comps.

88
00:06:48,470 --> 00:06:49,070
Yes.

89
00:06:50,010 --> 00:06:56,380
And then obviously now when we print is going to show me the year based on this value is giving me a

90
00:06:56,520 --> 00:07:00,850
warning is thing that I'm not sure if this value exists because an optional one.

91
00:07:00,870 --> 00:07:05,170
So we have to essentially force unwrap it but I'm going to do that in a second.

92
00:07:05,190 --> 00:07:12,660
So Knight says copier is optional 2018 and I could just say force on wrap.

93
00:07:12,870 --> 00:07:18,540
Now the next thing I would talk about is formatting a date and we use the formatting simply because

94
00:07:18,570 --> 00:07:24,990
we want to be able to restore these values as a string to somewhere on the disk or a backend under one

95
00:07:24,990 --> 00:07:29,280
to be able to fetch those values and compare them against other dates.

96
00:07:29,280 --> 00:07:31,720
So let's go in and see how we're going to do that.

97
00:07:31,740 --> 00:07:35,660
I'm going to say let this call this one date for mother.

98
00:07:35,700 --> 00:07:39,340
That becomes a date for mother.

99
00:07:39,430 --> 00:07:42,910
I mean let me call this one my date format.

100
00:07:42,930 --> 00:07:49,580
So it's a little bit more distinct then the next thing is your newly created date format that object.

101
00:07:49,590 --> 00:07:51,960
It has to have a format of its own.

102
00:07:51,990 --> 00:07:57,790
So I'm going to say date for Mother not date format is actually becoming this not you.

103
00:07:57,810 --> 00:08:08,850
I'm going to say yeah years months months day a hour hour minute minute and seconds seconds.

104
00:08:08,850 --> 00:08:12,900
Now there are so many many formats out there and you can just google for them.

105
00:08:13,080 --> 00:08:18,450
But the thing that is important is whatever format that you write in that's the same format that you

106
00:08:18,450 --> 00:08:19,540
have to read in it.

107
00:08:19,560 --> 00:08:24,710
Because otherwise the values that you have written and your reading will not be the same.

108
00:08:24,720 --> 00:08:30,210
So whatever format you use you have to kind of stick with it for that reason and make a wise choice

109
00:08:30,210 --> 00:08:30,770
about that.

110
00:08:30,780 --> 00:08:35,940
If your application is in a way that the only thing that matters is the year and the month then don't

111
00:08:35,940 --> 00:08:37,390
worry about all the other ones.

112
00:08:37,560 --> 00:08:43,260
If it is about the various specifics I can then go for milliseconds on the nanoseconds.

113
00:08:43,320 --> 00:08:46,560
I think there is something called a nanosecond let me check that first.

114
00:08:46,560 --> 00:08:47,540
I can for you.

115
00:08:47,790 --> 00:08:49,340
This calendar.

116
00:08:49,350 --> 00:08:53,480
Nothing that calendar dot component dot.

117
00:08:53,490 --> 00:08:54,410
Now as I can.

118
00:08:54,450 --> 00:08:54,800
Yes.

119
00:08:54,810 --> 00:09:00,780
If you want to be very very specific your application is about for instance making a bid on an item

120
00:09:00,930 --> 00:09:01,670
for a sale.

121
00:09:01,680 --> 00:09:03,510
Then it becomes very important.

122
00:09:03,540 --> 00:09:08,370
So for the way you are doing make sure that you find the right format for your application and then

123
00:09:08,430 --> 00:09:12,190
stick with it for the entirety of that application.

124
00:09:12,200 --> 00:09:17,040
No they have the form either they can go ahead and say let's produce a string out of it.

125
00:09:17,040 --> 00:09:23,310
So let date formatted string festering.

126
00:09:23,670 --> 00:09:32,160
It becomes my date form either my date form either though this string of and then the dates that I want

127
00:09:32,160 --> 00:09:41,490
to push into it is obviously the date that we have which is the one I appear received a date so now

128
00:09:41,610 --> 00:09:45,030
this is the value that we can actually properly a store.

129
00:09:45,030 --> 00:09:51,810
We can write it to our backend or to our local disk and then we can get it back and reformatted to be

130
00:09:51,810 --> 00:09:53,160
an actual date.

131
00:09:53,310 --> 00:09:56,340
Before I do that let's print it to see what we have in it.

132
00:09:56,340 --> 00:09:59,810
I'm going to copy paste this and say date format.

133
00:09:59,820 --> 00:10:08,320
There is they'd form others so they'd form either a string is they'd formatted a string and run this

134
00:10:08,320 --> 00:10:09,490
application again.

135
00:10:09,550 --> 00:10:16,930
Increase my console size and once my app is actually loaded I go ahead and choose another time.

136
00:10:16,990 --> 00:10:23,540
It says format advisor this 20 18 10 17 a space five that 747 perfect.

137
00:10:23,560 --> 00:10:27,730
So this is the value I can actually compare against.

138
00:10:27,730 --> 00:10:30,180
Now this is obviously a string.

139
00:10:30,220 --> 00:10:39,880
It doesn't understand 17th of the month of the year it understands 2 0 1 8 dash one zero dash one 7

140
00:10:39,910 --> 00:10:42,450
as if it was a string of characters.

141
00:10:42,580 --> 00:10:49,850
So the important thing now becomes how do they get the string of characters such as this one and convert

142
00:10:49,860 --> 00:10:53,320
it into a date actually to make things more interesting.

143
00:10:53,350 --> 00:10:58,700
I'm going to make one of these myself manually and then we're going to combat it.

144
00:10:58,780 --> 00:11:02,230
So when I actually copy this so I don't make any mistakes.

145
00:11:02,410 --> 00:11:10,190
I'm going to come in here and say let dummy date which is a string will be this value.

146
00:11:10,330 --> 00:11:11,920
So this is now a string.

147
00:11:11,920 --> 00:11:13,600
It's not an actual date.

148
00:11:13,630 --> 00:11:20,050
So I'm going to change that to be 20 25 for instance so many years from now.

149
00:11:20,080 --> 00:11:25,930
So here I'm going to have to convert this dummy date to be an actual date.

150
00:11:26,020 --> 00:11:28,810
So let's go ahead and see how we will be doing that.

151
00:11:28,870 --> 00:11:35,110
I'm gonna go in here and say let's dummy date I'm gonna change that value to a string.

152
00:11:35,200 --> 00:11:37,110
So this is the actual date value of it.

153
00:11:37,120 --> 00:11:45,230
That's the string value of a dummy date which is a date to become let's say my date form either dot

154
00:11:45,260 --> 00:11:53,680
the date from a string and the string value is the dumbest string I remember my date format there is

155
00:11:53,680 --> 00:12:00,100
using this format because that's the built in date format of it and obviously when it is trying to read

156
00:12:00,100 --> 00:12:07,070
this value it's looking at this type of format and saying Can I convert that into a date.

157
00:12:07,210 --> 00:12:10,510
Is of course giving a little error or saying that this value is optional.

158
00:12:10,510 --> 00:12:11,760
Are you sure it exists.

159
00:12:11,770 --> 00:12:12,840
And I'm going to say yes.

160
00:12:12,840 --> 00:12:14,850
Force unwrap it now.

161
00:12:14,890 --> 00:12:20,410
The next thing I want to do is I want to be able to compare this state to my dummy date with the actual

162
00:12:20,410 --> 00:12:23,570
date that we had which is the received the date.

163
00:12:23,690 --> 00:12:33,270
And like I said dates are comparable and equitable in three if you would simply say if dummy date is

164
00:12:33,700 --> 00:12:39,150
received the date then go ahead and save print.

165
00:12:39,310 --> 00:12:42,100
Same date.

166
00:12:42,130 --> 00:12:48,250
Otherwise say different dates.

167
00:12:48,350 --> 00:12:54,040
Now obviously these two have to be showing us that different days because this is set to several years

168
00:12:54,130 --> 00:12:55,020
after that.

169
00:12:55,180 --> 00:12:56,650
But I'm going to try it anyway.

170
00:12:58,500 --> 00:13:01,550
And then my simulator loads up then I choose a new date.

171
00:13:01,560 --> 00:13:05,610
We should be printing so many things as well as doing a comparison.

172
00:13:05,880 --> 00:13:11,070
So it says different dates simply because one of them is the date set.

173
00:13:11,070 --> 00:13:18,600
At this current time which is this value twenty eighteen ten 17 61 forty two twenty three the other

174
00:13:18,600 --> 00:13:22,440
one is actually set at several years from now.

175
00:13:22,500 --> 00:13:28,890
Now the other thing I wanted to show you is this this date again the value that we have in the dummy

176
00:13:28,890 --> 00:13:29,490
dates.

177
00:13:29,580 --> 00:13:32,360
If I want it printed we will get this.

178
00:13:32,400 --> 00:13:36,220
Let's go ahead and say print let me copy that.

179
00:13:36,540 --> 00:13:46,470
Say dummy dates print it and let's see what we get in it so let's change that.

180
00:13:46,530 --> 00:13:49,440
And now here is my dummy date.

181
00:13:49,560 --> 00:13:57,010
So as you can see the dummy date even though it was set at 537 it's actually showing us twelve thirty

182
00:13:57,010 --> 00:14:03,450
seven and that's simply because the date is being read properly from wherever it is and it is shown

183
00:14:03,450 --> 00:14:09,990
in the default time zone which is a GMT time zone and if you wanted to convert it to your timezone you

184
00:14:09,990 --> 00:14:16,860
have to again do this thing which is getting the law called the current value of the local locale which

185
00:14:16,860 --> 00:14:20,340
is the local time value of the local time zone.

186
00:14:20,340 --> 00:14:26,920
Now this might seem a little work you're on but it actually is useful simply because if your users are

187
00:14:26,930 --> 00:14:32,430
all over the world and they're using different time zones they still have to be writing in only one

188
00:14:32,430 --> 00:14:36,450
format simply because otherwise there is no comparison.

189
00:14:36,450 --> 00:14:43,200
Right now whatever the time is in here where I am in Canada in Vancouver is several hours before what

190
00:14:43,200 --> 00:14:47,600
is in Europe and many many hours before what is in Australia for instance.

191
00:14:47,610 --> 00:14:54,660
So this system makes sure that when we all write to the same network or the same backend all U.S. counts

192
00:14:54,660 --> 00:15:00,900
for everything that they have to do in terms of comparing these dates against each other they all are

193
00:15:00,900 --> 00:15:02,490
written in the same format.

194
00:15:02,490 --> 00:15:08,280
So if have to use right two different things at this very moment from two different parts of the world

195
00:15:08,610 --> 00:15:11,770
the dates are going to be similar or identical.

196
00:15:11,940 --> 00:15:17,670
And that's actually a very useful thing because it makes sure that in terms of sorting and the accuracy

197
00:15:18,550 --> 00:15:21,900
I was essentially icons for that and takes care of that for you.

198
00:15:22,410 --> 00:15:26,520
So that was a quick introduction are a little bit longer than quick.

199
00:15:26,520 --> 00:15:28,610
It actually became about 15 20 minutes.

200
00:15:28,730 --> 00:15:34,500
What a introduction to how to use dates and how to use date form others and how to compare them against

201
00:15:34,500 --> 00:15:34,950
each other.

202
00:15:35,370 --> 00:15:37,680
Let's keep it up to here and move on to our next.
