1 00:00:08,010 --> 00:00:14,780 Hey, everyone, in this video, we are going to talk about something super cool, which is date and 2 00:00:14,780 --> 00:00:20,970 time in JavaScript, we can easily work with date and time using the date glass. 3 00:00:21,440 --> 00:00:27,620 Don't worry about understanding what a class is right now, although if you want, you can just go to 4 00:00:27,620 --> 00:00:31,640 Google and search for jazz classes. 5 00:00:32,480 --> 00:00:34,880 Then you have a lot of things to read. 6 00:00:35,850 --> 00:00:43,320 So you can understand better what classes are for now, just think of classes as an object, they are 7 00:00:43,320 --> 00:00:44,640 actually more than this. 8 00:00:44,640 --> 00:00:48,110 They are actually like a template for objects. 9 00:00:48,780 --> 00:00:55,680 But for now, just think of the date as an object which will have properties and methods and everything 10 00:00:55,680 --> 00:00:57,320 we've been working so far. 11 00:00:57,540 --> 00:01:03,680 So to create an object with this class, we need to use the new keyword. 12 00:01:03,690 --> 00:01:08,290 So that's the difference between a regular object and a class. 13 00:01:08,550 --> 00:01:13,650 This time we need to use the new keyword, so let's create a new variable. 14 00:01:14,700 --> 00:01:17,400 Actually, let's do an individual story called. 15 00:01:18,500 --> 00:01:20,090 Let me just copy this. 16 00:01:23,180 --> 00:01:25,310 So I'm just creating a variable. 17 00:01:26,210 --> 00:01:33,380 And I'm using the new keyword, and then I'm just using date opening and closing parentheses, so this 18 00:01:33,380 --> 00:01:37,970 means that I'm creating an object of the date class. 19 00:01:38,000 --> 00:01:42,680 So now let's just do console that log so we can see what's inside this object. 20 00:01:44,390 --> 00:01:51,020 So refreshing the page, as you can see, this is returning the date and time I'm recording this video. 21 00:01:51,110 --> 00:01:54,800 Now, just out of curiosity, let's do type of. 22 00:01:55,850 --> 00:01:57,140 And then Beit. 23 00:01:58,690 --> 00:02:02,630 Object, so, as you can see, this is just a regular object. 24 00:02:04,120 --> 00:02:11,650 But this is super cool because it just returned the current time and this is like the full version of 25 00:02:11,650 --> 00:02:12,120 the date. 26 00:02:12,130 --> 00:02:19,990 So as you can see, we have week, day, month, day, year, hours, minutes, seconds. 27 00:02:19,990 --> 00:02:27,040 Then we have the time zone and then the information about daylight saving time. 28 00:02:27,670 --> 00:02:29,530 So this is the complete date. 29 00:02:30,040 --> 00:02:35,430 And if you want, you can also get specific information. 30 00:02:35,440 --> 00:02:39,550 So let's say you just want to get the year what you can do. 31 00:02:40,020 --> 00:02:41,410 It's just to date. 32 00:02:43,240 --> 00:02:48,070 O'Bagy and then you can use a method called Get. 33 00:02:49,540 --> 00:02:50,410 Full year. 34 00:02:51,470 --> 00:02:57,230 So if you do this, then, as you can see, you are just going to get the ear back. 35 00:02:57,260 --> 00:03:04,040 So in this table you can see some more examples so you can get to be extra careful with this, because 36 00:03:04,040 --> 00:03:08,600 as you can see, the mouths, they start from zero to 11. 37 00:03:08,840 --> 00:03:15,590 So it's not from one to 12 like we are used to work with mouths is the only thing you just need to pay 38 00:03:16,130 --> 00:03:17,300 attention to. 39 00:03:17,600 --> 00:03:19,730 So for the rest here, there's nothing new. 40 00:03:19,730 --> 00:03:22,970 So for the hours we have it from zero to twenty three. 41 00:03:22,970 --> 00:03:23,870 We have minutes. 42 00:03:23,870 --> 00:03:24,680 Seconds. 43 00:03:25,610 --> 00:03:34,070 Milliseconds, then there's something cool here that is the get time method's let's try to do this so 44 00:03:34,070 --> 00:03:40,630 they Tobji instead of using the get foliar, let's use the get time method. 45 00:03:40,910 --> 00:03:43,700 So as you can see, we are getting a big number. 46 00:03:43,720 --> 00:03:44,660 So what is this? 47 00:03:45,080 --> 00:03:52,720 This is the number of milliseconds since the epoch, which is January 1st, 1970. 48 00:03:53,270 --> 00:03:56,420 So JavaScript uses the Unix epoch. 49 00:03:57,300 --> 00:04:04,610 Every programming language has an apoc, which is used to set the starting point of counting time, 50 00:04:04,920 --> 00:04:08,710 so this is useful to do calculations with date and time. 51 00:04:08,940 --> 00:04:16,770 So like I said, the Unix epoch, which is used by JavaScript, is January 1st, nineteen seventy zero 52 00:04:16,770 --> 00:04:19,440 zero zero UTC time. 53 00:04:19,650 --> 00:04:25,490 So this does not mean that we can't work with dates prior to 1970. 54 00:04:25,980 --> 00:04:27,060 We can do it. 55 00:04:27,390 --> 00:04:30,150 We're just going to have negative numbers. 56 00:04:31,280 --> 00:04:38,930 So anything after nineteen seventy, then we will have this positive number of milliseconds, so this 57 00:04:38,930 --> 00:04:44,100 is the number of milliseconds that have passed since the book. 58 00:04:44,930 --> 00:04:47,270 So now why is this useful? 59 00:04:47,480 --> 00:04:52,130 Why do we need a starting point to have this number of milliseconds? 60 00:04:52,430 --> 00:04:58,760 Because when we work with this, which is actually called a time stamp, so every time we work with 61 00:04:58,760 --> 00:05:03,580 milliseconds since the APOC, we can call it time stamp. 62 00:05:04,250 --> 00:05:13,050 And when we are working with time stamps, we have more flexibility to easily do calculations with dates. 63 00:05:13,640 --> 00:05:22,280 So if we want to get how many years have passed since the apoc, this is pretty simple to do because 64 00:05:22,280 --> 00:05:29,060 we have the number of milliseconds, then we just need to know how many milliseconds we have in one 65 00:05:29,060 --> 00:05:29,430 ear. 66 00:05:29,840 --> 00:05:30,500 So here. 67 00:05:31,870 --> 00:05:40,090 I have this table, so I know that one year has this number of milliseconds, so if I get all this number. 68 00:05:43,330 --> 00:05:48,940 And I just divided by the number of milliseconds in one year. 69 00:05:53,630 --> 00:06:00,020 We can see that this is something close to 50, which is accurate because I'm recording this video in 70 00:06:00,020 --> 00:06:05,990 2020, so from 1970 to 2020, we have 50 years. 71 00:06:06,470 --> 00:06:09,520 This has that because I'm in the middle of the year. 72 00:06:09,800 --> 00:06:17,630 So if I just want to get the number of years I could do like this and then just use the math. 73 00:06:20,830 --> 00:06:25,750 That floor, which is going to round number, is down. 74 00:06:26,920 --> 00:06:31,170 So now I know that 50 years have passed since the epic. 75 00:06:32,140 --> 00:06:38,890 And I know that maybe it's not very useful to get how many years since the apoc, but this is going 76 00:06:38,890 --> 00:06:41,520 to be useful to compare to different dates. 77 00:06:41,920 --> 00:06:44,400 So let's do a few tests with this. 78 00:06:44,410 --> 00:06:52,900 But first, let me teach you how to create specific dates, because so far we just created a new date 79 00:06:52,900 --> 00:06:56,410 object and we did not pass any argument. 80 00:06:56,410 --> 00:07:02,200 And when we do this, it just creates an object with the current date and time. 81 00:07:02,470 --> 00:07:05,920 But we can also use something specific. 82 00:07:06,100 --> 00:07:12,530 So if you want to create a specific date, you can pass from one to six argument. 83 00:07:12,940 --> 00:07:17,410 So let me just clear this real quick and now. 84 00:07:18,720 --> 00:07:27,660 Let's do a few tests, so I'm just going to do a new date again, and if we do this, we can see that 85 00:07:27,660 --> 00:07:31,580 it's creating this object with the current date and time. 86 00:07:31,920 --> 00:07:35,630 But let's try to use one numeric argument. 87 00:07:35,850 --> 00:07:40,140 So if we use just one, it's the millisecond since the apoc. 88 00:07:40,380 --> 00:07:46,850 So let's just get the number of milliseconds in one year just so we can see how this works. 89 00:07:54,480 --> 00:08:03,390 So if we do this, we can see that we created a date in 1971, which is exactly one year after the APOC, 90 00:08:03,630 --> 00:08:07,080 so here we can also use a different number of arguments. 91 00:08:07,110 --> 00:08:13,630 So if you use two numeric arguments, it is just going to consider it to be year and month. 92 00:08:14,100 --> 00:08:17,800 So let's try again with two arguments. 93 00:08:18,030 --> 00:08:23,430 So the year let's try, I don't know, 2010 and then the month. 94 00:08:23,460 --> 00:08:26,570 Don't forget that the month they go from zero to 11. 95 00:08:26,790 --> 00:08:32,310 So if I do this, as you can see, January 2010. 96 00:08:33,800 --> 00:08:39,110 Then we can do the same thing with three arguments, four or five up to six, you can read. 97 00:08:40,160 --> 00:08:41,870 All the details here. 98 00:08:43,100 --> 00:08:48,110 We can also bass strings as arguments in three different formats. 99 00:08:49,510 --> 00:08:56,950 So if we create this object and we pass the ISO date. 100 00:08:58,620 --> 00:09:08,670 Which is formed by the full year, so 2010, let's say, then two digits for the month, then it's like 101 00:09:08,670 --> 00:09:11,850 we know it's like January, it's just zero one. 102 00:09:12,060 --> 00:09:14,460 And then the day, let's say 18. 103 00:09:14,730 --> 00:09:17,820 As you can see, we just created a specific date. 104 00:09:18,420 --> 00:09:24,120 We can also use the short date, which is written like this or month day in here. 105 00:09:24,480 --> 00:09:27,680 And we can use the long date as well. 106 00:09:28,970 --> 00:09:35,930 Which is going to work like this, so April is going to be a PR April 10th. 107 00:09:36,860 --> 00:09:38,540 Two thousand and twenty two. 108 00:09:39,290 --> 00:09:40,260 So here you go. 109 00:09:40,310 --> 00:09:46,700 We just created this object, and now that we have this object, we can get any information we want 110 00:09:46,700 --> 00:09:53,420 from it so we can get the number of milliseconds since the Abuk, which is useful to do calculations, 111 00:09:54,230 --> 00:09:59,260 we can get specific information like just the month or the hours or minutes. 112 00:09:59,960 --> 00:10:05,120 So how can we use all this in a useful way? 113 00:10:05,690 --> 00:10:12,880 So there will be two very nice examples and one of them is going to be the challenge for this lesson. 114 00:10:13,430 --> 00:10:16,280 I want you to calculate date intervals. 115 00:10:17,340 --> 00:10:26,730 So let's say we have a product that was ordered on March 26, 2020, and it was delivered on April 2nd 116 00:10:26,730 --> 00:10:28,000 to 2020. 117 00:10:28,470 --> 00:10:36,910 I want you to use date object to calculate how many days does it take this product to be delivered. 118 00:10:37,650 --> 00:10:42,990 So just calculate this using date objects and show the number of days here. 119 00:10:43,590 --> 00:10:46,200 Just take your time, try to solve this. 120 00:10:46,200 --> 00:10:48,570 And in the next video, I'm going to fix this. 121 00:10:48,750 --> 00:10:55,590 And in the next lesson, we are going to talk about time methods in JavaScript and there will be another 122 00:10:55,590 --> 00:10:57,630 very cool challenge for you to do. 123 00:10:57,660 --> 00:10:59,250 So I'll see you in the next video.