If you already have a subscription, you can sign in.
Enjoy free content straight from your inbox 💌
00:00
JavaScript comes with a built-in date class that we can use to manage daytime values. We can create a new instance of the date object by using the date constructor. If we don't pass it any arguments, it's going to get initialized to the current date. For example, this particular piece of code is running on Friday 10th, March, 2023. If we use the date two stringing method, it renders it out in the current time zone, but the date itself is actually independent of any time zone and it is a particular point in time. It is actually tracked from U T C and if we log it out by itself, we can see the U T C equivalent of this particular date value.
00:34
Since we are in G M T plus 11, the 5:00 PM is going to be equivalent to 6:00 AM in U T C. Now this particular string that we are looking at is called the TZ string as indicated by the presence of a T and a Z. Everything before the T is the ear month and day, and after that we have the R minute, seconds and milliseconds followed by the Z. The TZ string is actually an IO standard, so the date object also comes with a function called two io o string, which actually gives us the string. That console log is logging out the date.
01:09
Constructor also accepts values in the local time zone so we can initialize a new local date and here we are initializing a new local date for the EA 2025, January 1st with the Rs minutes and seconds at 1330 and 15. This constructor should make obvious sense except for the one thing that you should always remember, which is that month within JavaScript is tracked with index zero, so January is going to be zero as we are seeing over here, and December is going to have the value 11. The values that we are providing over here are in the local time zone, so the R 13 for GMT plus 11 is going to be the UTC two,
01:46
which is what we will see when we log this value out without applying any formatting or using the two stringing function. Just like other JavaScript objects created from JavaScript classes, the type of operator for this will give us the string object and if you want to check if a particular object is an instance of date, we can use the built-in instance of operator and of course today is an instance of date. The date object provides various methods to access its sub-components, which can be used to format the date to our liking. As an example, consider the new air 2050 in local time zone.
02:20
Our mission should be choose to accept it is to display the date in local time zone with the format Y Y M M D D R R, minute, minute, second. Second. Now, in order to pad the different values, for example, the day one to become a zero one, we are going to create a nice utility function that looks at the number. If it is less than 10, then it pads a zero before it, otherwise it simply converts it to a string. With this simple utility in place, let's start creating our format function. Our function will take the date object and format it to the desired string. First up,
02:53
we can get the four digit ear from the date object by using the get full year method and then we convert this numeric value to a string by simply using two string. Next we are going to generate the month as mm, and for that we use the date get month function and as you mentioned, the month is returned with a zero waste index, so we need to increment it by one for a more human presentable format. Once we have that value, we rapid without pad zero utility. The remaining portions of our format are going to be pretty easy. They are simply going to be padded zero versions of the get date for the day, the get rs for the R get minutes for the minutes and get seconds for the
03:31
seconds. Once we have these values, the final result is going to be simply a concatenation of these into a nice string that gives us the desired format. Let's verify that this function behaves the way that we want by using the date value that we have, passing it to format and then logging that out to the console. And if you run this code, you can see that it matches our expectations. We created a date in local time zone for 2050 new year and we get that presented out in the desired format for the user. A programming term worth knowing is epoch. It is the absolute point in time, which is midnight January 1st, 1970 at U T C zero.
04:08
A key way that developers interact with dates is a milliseconds since epoch and this millisecond value is also called Unix time and also called the timestamp. I want to point out the time in U T C plus 11. So here I am creating the new year 2030 plusing in the R 11, so it is indeed zero zero in U T C I can get the timestamp for this value by using the built in date get time method. We can actually create a new instance of the date by using this timestamp. Unlike the year, month, day version of the constructor, when we only pass in the timestamp,
04:41
the timestamp number by itself is by definition from U T C and the date constructor accepts it as U T C as well. So irrespective of what your machine time zone is, if you use the same number, you will get exactly the same string, which is 2030 new year. At U T C. A key use case for the timestamp value is that it allows you to do date math quite easily. As you mentioned, the timestamp value is in milliseconds, so if you wanted to calculate what date it would be one day before the new year 2030, we can actually do that by calculating a new timestamp and subtracting the
05:16
required number of milliseconds for 24 hours. Once we have this new timestamp, we can pass it to the date constructor and now of course we can look at what that value is going to be. And of course it is going to be 2029 December 31st. The JavaScript date object transparently accepts and presents values in the local time zone, but it also provides some utilities to create dates in U T C time and provide information about the local time zone. Here I'm creating a date in the local time zone for 2025 New year at 11:00 AM of course, since I'm at plus 11,
05:50
using the R 11 gives us midnight. At U T C, we can get the RS in the local time zone as we saw when we were doing data formatting by using the built-in get RS method. And of course for this particular time the local RSS would be 11. The data object actually also provides US methods to get the U T C version of this time. For example, we can get the U T C Rs and of course for this particular time it's going to be zero, just like get ours is local and get U T C R is the U T C version. We have U T C versions of the other formatting functions as well. For example, get U T C date, get U T C minutes, get U T C seconds. As we can see,
06:27
the date constructed takes values in the local time zone, but we can actually create a date at A U T C point in time as well by using the builtin date UTC static method. This method takes the same values as the date constructor, however it accepts them at U T C time zone. So here we are creating a date at U T C R zero. This method also only returns a timestamp and does it actually create a date object, but as we know, we can convert a timestamp to a date object quite easily by passing it to the date constructor. Now time is absolute,
06:59
and actually this is the same point on the dimension of time as R 11 in our local time. So if we log it out, you can see that it is the same value as the one we created before. In addition to allowing us to create time in U T C values transcript also offers us a simple method to get the time zone offset. Compared to U T C, there is a method called get time zone offset offered by the date object that gives us the difference of U T C minus local in minutes. Since we are in time zone plus 11, local is going to be ahead by 11 hours or 6, 6 0 minutes and
07:35
therefore the minute offset local time zone is going to be minus 6 6 0. Use this value however you see fit. For example, let's just create a formatting function that displays the current time zone. In plus or minus RR minute, we start off by creating a new data object and capturing the result of get time zone offset. If the offset is greater than zero, it means that local is less than U T C and therefore we will use minus. Otherwise we will use the plus sign. The minute remainder after we remove the RS would simply be the absolute value of the offset remainder 60. And for the R value,
08:10
we will simply divide by 60, take the absolute and then use math flow to get rid of the remainder as that is already accounted for by the minutes. With these values in place, we simply return the formatted string sign S colon minutes, and now for the moment of truth, if we log out the formatted time zone for the current time zone of plus 11, we should see plus 11 colon zero. And if we run this code, you can see that it matches our expectations. The date class comes with a static now method that provides the current numeric timestamp and a common use case for this function is simple timing or
08:45
performance testing. We've looked at timestamps before and we know that we can get the current timestamp by creating a new date object and using the get time method. The Date object comes with a static now method that does exactly the same thing but with lesser neater code. And of course the value of the timestamp is the number of milliseconds since epoch the date. Now method gives us convenient access to the number of milliseconds at the current point in time. So let's use it to create a nice utility function that can be used to measure the performance of a particular callback function.
09:18
We take an approach string for logging and then a callback function. We start off by noting the current milliseconds using date now and then we run through a loop for a hundred thousand times each time invoking the past in callback. After this loop terminates, we note on the milliseconds again by using data now and now we have the start and the end and we simply log out the approach took N minus start milliseconds. For our example, we will compare the performance of the various ways you can create nont arrays of size 10,000. We kick off our test by first logging the race car.
09:54
One approach to creating a non empty array is to pass the size to the array function and then use the fill method. Array size creates an empty array with empty slots which cannot be used for iteration and working the fill method fills those slots up with undefined. So we can use functions like for each and map. Another approach to creating an non empty array is to first create that empty array with array size and then using the built-in spread operator. Finally, after both of these functions return, we will log out the racing flag. Now let's kick it off and see which one wins. On my machine fill takes about 1.7 seconds and spread takes about
10:31
1.4 seconds and I've tested it on other machines as well. Spread is always faster than fill and this is one of the reasons why in our JavaScript lesson on arrays, we recommended that we use the spread operator to create non empty arrays. How JavaScript dates get serialized by Jason is actually quite nice and it makes sending and displaying date values across devices in different regions. Quite simple. For user interface purposes, let's create a date in our local time zone of U T C plus 11 for 2049. January 1st are 11. Now of course if we log it out in our local time zone,
11:06
you can see that it is 11:00 AM on January 1st, 2049, but we can also get the ISO version of the state, which is always going to be normalized to U T C and of course at plus 11 in our time zone at U T C, it is going to be R zero. The JavaScript date object also overrides the two JSON method to do exactly the same thing as the two ISO string method. This means that when JavaScript serializes the date object to json, all dates get serialized to U T C TZ date strings and in reverse we can actually create a date from the TZ date string as well.
11:42
So here is that same point in time and of course if you log it out, that is what we see. But if we use the two stringing method, we get that value formatted for the local time zone, which of course is going to be be 11:00 AM. Now that we are familiar with the two js method that gives us the TZ string and the date constructed that accepts that t Z string. Let's look at a JSON example. We create an object blade runner that has a property here that points to the date 2049 New Year at 11:00 AM in the local time zone. If you want to send it off to a server using json, we can use JSON string gify and this gives us a string such that the ear is
12:18
going to point to the TZ string that we saw before. There is no ambiguity about this particular point in time because it is always in U T C and it doesn't matter where the server is located or where the user sent it from. On the flip side, when the server sends it out to someone, we would use adjacent do pass to pass it into an object. And of course this particular string value is going to get serialized to a string, but we can convert it to a date object quite easily by passing the string to the date constructor. And now we have a date object that points to the same absolute point in time and we can render it out however we want. For example,
12:53
when we render it out using two stringing, it renders it in the local time zone and of course, because we are 11 hours ahead, the user's perception of the time over here would be 11:00 AM In case you have a notice, I do have a passion for time because after all time is the most valuable asset that we have. As always, thank you for spending your time with me and I will see you in the next one.