Learn all about time handling in modern web development with JavaScript. In this comprehensive course, you will learn about the notorious quirks of the native date object, master global formatting using the powerful Intel API, and explore strategies for handling tricky concepts like epoch time, time zones, and daylight saving time. You will also get a look at the upcoming temporal API comparing it side by side with current methods and learning how to implement it using today's polyfills. Tapus created this course. Hello everyone, welcome back. If you have worked with JavaScript for any length of time, you have almost certainly ran into dates and time. And if you have worked with dates and time for any length of time, you have certainly thought about throwing your laptop across the room. Dates and times are one of those topic that looks so simple on the surface. But once you deploy your application on the production and somebody your user access that application from another side of the globe, they start seeing something off. Maybe by a day, maybe by few hours, maybe things are broken on February 29th and you start scratching your head wondering why is this happening. So I thought why not address that problem. Hence we are starting a complete comprehensive course on JavaScript date and time. This is going to be a long course. That's the reason I'm breaking into multiple parts. In the part one, we are going to demystify everything about dates in JavaScript. We are not going to talk about only APIs rather we are going to talk about the why part the background part the fundamental part and that most of the developers miss out today and that's the whole reason for introducing a lot of production level bug by the end of this part one you will have a rock solid mental model to address any date and time related issues in JavaScript with confidence so let's go and before we get started here is something about me I am Tapus a software engineer with 20 plus years of real world experience in building and debugging production system. On this channel, we go beyond tutorial. We master fundamentals, we fix real bugs and we think like engineers. If you are someone who is serious about leveling up your dev career, just hit the subscribe. Most people think that date and time are simple linear things. If the clock hits 3 p.m., it means it's 3 p.m. and end of the story. But when you are writing software that has to be used by your user from different parts of the globe on different devices they are in different time zones considering daylight savings appear and etc. My friend time is not that simple. It gets complicated very very soon. Let me take an example. Let's assume that you are building a calendar app. A user in New York schedules a meeting for 900 a.m. tomorrow. And then another user in London accepts that meeting. A third user in Tokyo joins the meeting to see who actually join at 9:00 a.m. So here what does 900 a.m. means for each of these individual who are trying to attend this meeting from different geographic location. What does even tomorrow means to this Tokyo guy? These are not age cases. These are everyday's problem and to understand this and to fix bugs around this you have to understand date and time ground up. That's exactly we are going to do in this course. But what is the core problem? The core problem is time is relative. What does it mean? It means there is no single now moment that everyone on this earth can agreed upon simultaneously. See the earth is round and sun that falls and shines on different parts of the globe on different times and we humans what we have done we have divided the planet into different time zones so that we can make use of the civil time useful to our daily life. So it means the current moment now has a completely different time representation depending on where you are at this point on the earth geographically. While recording this video, it's 3:00 p.m. in New York, it's 8:00 p.m. in London, it's 9:00 p.m. in Berlin, and it is 4:00 a.m. next day in Tokyo. That's exactly what we are talking about. Same moment, different representation. And this is the exact problem that we face so many issues with date and time in programming. See the computers were invented with globally distributed usage in mind. But the first operating system that was invented was designed for a single machine and for a single location. Early unique systems stored the time in local time and when internet eventually started growing this started creating the problem because your local time may not be same as my local time. This become a serious problem and to add to that there are few more problems and this is the part that you must note down at least the pointers. The first problem time zones don't just add and subtract a whole hours. It might be in minutes like 30 minutes, 45 minutes. For example, if you take India and Bangladesh, they have the time zone offset difference of just 30 minutes. Maybe for some other countries 15 minutes, 45 minutes. Okay, so these are the things that you have to keep in mind when you are dealing with date and time. Point number two, daylight savings can move the dates forward or backward. And the more problematic part is daylight saving is not applicable for all the countries on the globe. Point number three, I'm not sure if you're aware that government can change the time zone rules. Sometimes they can do it with very very little time notice. Point number four, all of us know that the months are having different lengths. Some months are in 28 days, sometime 29 days, sometime 30 days, sometime 31 days. And there is something called leap year, right? For that we get one day extra in 4 years. we have February 29th in every four years. So when you're dealing with date and time in programming, you have to consider all these pointers so that it doesn't break anything in your application for a customer because if the date and time is broken, it's actually going to cost money. We all know right time is money. Now with all this knowledge that we have gathered, it's time that we learn the meaning of certain jarens, certain words, certain phrases. For example, epoch time, UTC, EST. There are variety of things that we'll be learning and you must understand each of these thing very well to use date and time in your JavaScript programming effectively. Let us first start with epoch time. You would have heard about this already. What is epoch time? The epoch time exist because of this particular thing that time is relative. We spoke about it, right? My now is not equal to your now. If we are not situated in the same geographic location, it may not be it may be entirely different. But to calculate the date and time, programmers needed a universal reference point. Let's create one. Let's assume that this is that universal reference point that everyone on this earth, every programmer, every software developer on this earth will refer this as a starting point. And this is a single unambiguous number that represents a specific moment in time. Regardless wherever you are situated on this earth, we should always treat this as a starting point. And this particular starting point is called epoch. Now what is this value or which particular date and time this particular epoch is set to? It is set to 1st of January 1970 at exactly 000000 hours. So from this moment every second that passes gets a unique number in milliseconds. It means that the time stamp zero is January 1st 1970 at exactly 000000 hours of 1 million is about 11.5 days later. Another example, a time stamp of this big number could signify the year 2023. Now, how is this happening and what are these numbers? We are going to learn everything. But for now, remember that for everyone, every programmer on this earth, we need a single unambiguous reference point. And this reference point is starting from January 1st, 1970 exactly at 000000 hours. And once you go in future, you go in future in milliseconds. And with that milliseconds value from this reference points which is standing on zero you can calculate what is the amount of time that you have progressed. If you can calculate it in milliseconds you can convert it into second. You can convert it into the minutes then to hour then to days and so on. Milliseconds we also symbolize it with ms in programming. Now can you guess why this representation in JavaScript is in milliseconds not in second? Why? Because there are many operations we do in programming that may happen in milliseconds. For example, measuring performances, those animation frames. That's the reason JavaScript use this tweak to represent this time offset always in milliseconds. Now, this might come in your mind as it came in my mind also. Why January 1st, 1970 is a starting point? Why exactly? Honestly, um what I found with my research that it is somewhat an arbitrary number. The Unix operating system was being developed in 19607s that time and engineers needed a starting point that time. That's how the year 1970 came into the picture. It came into the picture more because of the simplicity than anything else. So if you have ever wondered like why it is January 1st 1970, there is really no deeper thought and meaning. It probably it most probably is because some folks thought that that point in time this is the simplistic thing to do. The next big question, what's the difference between a time stamp and a time zone? Often developers get confused with that. A time stamp is an absolute point in time that is independent of location. Here the time stamp is zero. You see over here zero, that universal point. Here the time stamp is 1 million. Here the time stamp is this big number. So this is what the time stamp is. It's a single point absolute number. And this time stamp absolute point in time is always measured from January 1st 1970 at exactly 000000 hours. So here this number is a time stamp. Zero is a time stamp. This big number is a time stamp. And this time stamp in JavaScript is always always represented in milliseconds number. Then what is a time zone? Time zone is a different kind of rule set by human set by us to represents different kind of time for a specific region. Again, I'll go deeper into time zone when we start coding. But remember the difference between the time stamp and time zone. Time stamp doesn't have any time zone. To calculate a particular time zones time, you will need time stamp value. We will learn all these things together. So by definition, time stamp is what? It represents a specific moment in time and the unit is UTC. A time zone is something which is a defined rule by us for a specific region to communicate the time and the time differences between various regions. This is the difference. So we got another term now is called UTC. What is UTC? UTC actually stands for coordinated universal time. Now if you take this coordinated universal time and if you put the abbreviation it should be like C U T but why it is UTC. So if you go back to the history as I was researching that abbreviation never matches only because the English and French people when they were deciding this they came together and they agreed upon something called UTC that's the primary reason and from then it become the primary standard for the whole world to use for time stamp so imagine UTC is the master clock of the internet it keep ticking it keeps ticking starting from this zero as it keeps ticking we tends to go to the future a future time a future date a future day a future hour like that this UTC doesn't have any daylight saving it doesn't have any leap year it doesn't have any notion it just keep shifting kept ticking to take us to the future and that's the whole reason that when you talk about a time stamp this particular milliseconds value we always put this UTC as a unit to say that how much offset from this zero we have come to the future so that we can conclude a particular day, a particular date, a particular time. I hope the UTC is clear. Time stamp is always in UTC. Why? Because time stamp doesn't have any time zone. I mean universally there is no New York time stamp. There is no Tokyo time stamp. There is just one number universally available. And using that number you calculate the date and time and etc. Now to bring these things technically this is the whole reason when you will be saving your date and time information into a database you must be saving it in UTC. It means you will be saving a time stamp and when you retrieve that and display that information on the UI you will be converting this timestamp information into that particular UI's location's local time. What does it mean? Imagine we have two users. First user accessing the application from India. The second user accessing the same application from France. There is a centralized DB which is storing the information. Now this application deals with date and time. What we must be doing is like any date and time information we store in the DB. It must be stored in the epoch format means this millisecond format in the UTC time. Okay. This is why we call UTC time. Whenever we call UTC time or the epoch time, consider them as the same thing we're talking about. We're talking about the time stamp, not the exact time. Okay? So, always store in epoch time in UTC. But when we are retrieving this and showing it in our application, if we show this epoch time, it will be a time like this doesn't make any sense to any user. That means you have to convert this time to something that makes sense to this user. Maybe what you will be doing, you have this epoch time. So with this epoch time you can calculate from January 1st 1970 which is like zero get the millisecond then convert that millisecond with your own programming language to a particular date and time that's fine but another additional thing you have to do you have to take care of which particular local this particular user is using this application from where is your client what is the time zone of your client. So when you display you have to display this epoch number converted into that particular time zone where your user is accessing the application from. This is how if you handle date and time your date and time will be universal and it will be without any kind of bug. Maybe the epoch time representation is something like this number and this number when we try to display it could be 12:35 p.m. in p.m. in India and morning 8:5 a.m. in France and if you want to talk about like why it is 12:35 p.m. in India at the same time it is 8:5 a.m. In France that is where the time zone comes into picture because in India we follow a time zone which is called Indian standard time which is nothing but IST and here this particular time in France is another time zone called CCT. Okay. So this is the time zone value. So it means that wherever you are storing this data from you are storing this in UTC and then when you are retrieving and going to display that you have to translate that into the client specific time zone that particular time. So this is how you have to handle date and time. We are going to see a lot of them with coding in this part and in the subsequent part. There are few more terms you need to consider learning. You know about the UTC. Now what is GMT? is UTC and GMT are same. For the programming context, you can take UTC and GMT are same. There are tiny differences that we are not bothered to learn at this point in time. Let's understand UTC and GMT are same. From the time zone perspective, both UTC and GMTs are in this particular time zone offset. So, we'll be using them interchangeably in our daily programming. What is local time? Local time is nothing but the time that you are seeing on your clock. Now on my clock there is a time that I'm seeing being in India. In your clock you might be seeing a completely different time than what I am seeing right now and this local time is always driven by the time zone that we are in. Now there are different kind of time zone and different kind of time zone offset and every time zone offset in the world is defined as an offset from this UTC. So if UTC starts with 000000 here are some examples. For example, New York which is having this EST time zone is UTC minus 5. That means it is behind. London is follows the UTC and GMT. So it is UTC plus 000000. You are seeing that India is + 530. Nepal is + 545. Japan is + 9. In case you are interested, this is one time zone map. You can see this world map and it time zone. For example, India I told like + 5:30. So it falls over here. Right? If I click over here any of the location it's on plus 530 like that you can move move on the different part of the world. Select any country and try to find out what is the time zone that it is falling into. So this is UTC which is 0000. Now from this side of the earth it will only increment on this side of the earth it will only decrement. That's how you can find different offset and on those offset there will be a name for individual time stamp. That's how it is. If you are interested in time zone and you want to kind of play around more with it, the world's time zones are maintained by the AA which is internet assigned number authority and they call it as TZ database. You can always come here, you can download and you can start looking into it. By the way, I provided all this link into the description of this video. So you can go ahead and check it out any point in time. Now the question is how JavaScript handle these time zones? What's the support? Unfortunately, very very unfortunately it's a frustrating truth that the built-in support in JavaScript is about only two time zones is about UTC and your local time zone for rest all the time zone we are talking about you have to do it in little bit hard way we are going to learn many of these things in the next part of it but I want to provide you enough hint that what you're going to deal with in JavaScript now I think we had enough theory we have learned a lot about date time history some of these jarens terms how time works how time zone works what are the problem why this difficulty it's time to start doing some code isn't it open up your favorite code editor I'm just going to open up VS code for myself let's start coding as part of 40 days of JavaScript repository I have created a new folder called day 41 you will find it again from the tapascript organization this particular repository please check it out I have created a simple HTML so that I can attach this JavaScript over here the Java basket file where I'm going to write all my code. So far I just have a console.log file. Let's go live with this HTML. I can see this HTML rendered over here. And if I open up the Chrome debugger tools, I'm able to see this console.log that is coming from my JavaScript file saying JavaScript date and time which is here. So I'm all set to start coding. The first thing that we'll start with is called date object. The JavaScript date object is a built-in object that represents a single moment in time. Whenever you are doing new date in JavaScript to create an instance of the date object, you get the time of that particular moment. So you can do like new date. This returns instance of the date object of that particular moment which we can call it as now. So I've created a date object over here. Let me now access it. For example, I'll do console dot log of now. I see the complete date and time representation of that moment when I have printed or executed that console.log of now. Just take a note of this time 141459. If I just refresh, it will change it changed to 141548 because that console.log of now got executed when I refresh the browser. So, it now captured the time of that particular moment. Right? So now is always about capturing the time of that particular moment along with the date and all other information. GMT plus 5:30 is the time zone. You remember UTC or GMT is 000. So from there 5:30 means 5 and a half is nothing but the IST Indian standard time zone. So new date gives me the single moment in time. However, you need to understand that internally it stores that single moment of time in a single number and that number is represented in millisecond since January 1st 1970 0 UTC correct. So the number that is printed that particular moment number with the date time zone etc. But internally JavaScript stored that number in a millisecond. The same millisecond that we have seen from that particular point of January 1st 1970. Okay, that's how JavaScript manage it internally. Now the question is how do you get that millisecond number that internal representation of this particular moment that we have seen getting logged into the console.log to get that JavaScript provides a static method called now. Let's do a console.log of date.now. So if you invoke this particular static method on date object you get the millisecond representation of that moment which was human readable way represented using this particular now variable. So if I see both this console.log log I will clearly know the first one is a human readable representation of the current moment and this is that millisecond representation the U in UTC remember and this is the difference between your this moment of time and that January 1st 1970 0 UTC that's the difference in a millisecond time of course there is another way you can get the same representation before date dot now got added how people used to get you have this now over here right so you can do console dot log of now dot get time. If you call this get time method on this particular object instance, this is going to give you the exactly same value in milliseconds. The date object constructor can also take an argument. When the date object constructor takes an argument like this, what it returns? It means that you are now giving that timestamp in UTC passing that to the date object to get a date representation back. So now if I do a console.log of D1 what it is going to return the D1 is going to return the human readable date time format along with the time zone for this particular time stamp. I can see it over here like it's Monday January 15, 2024 sometime. So guys question for you and if you know about object references or if any of you have gone through my 40 days of JavaScript course already you should answer this. Is it true or false this console.log log what it is going to log is going to be false right what we are doing here we are equating the reference to the different objects here D1 is a reference to one object in memory here D2 is a reference to another object in the memory we are trying to equate those two references even though the value that is stored on those memory location are exactly same but the references are different right we know that we have learned enough about it like memory management and all the things so here it is going to be a false. Then how are you going to compare if these two dates are same or not? The best way to compare is take D1 call get time on top of this and equate it of course triple equals D2 dot get time. Okay, this is going to give you a true or false. If it is true then we're talking about the same time. If it is false then we're talking about the different time. Why? Because D1.get Get time is of course going to give you a millisecond representation in UTC of that particular date and time. G2.get time also going to do the same. Now you're comparing from a zeroth point to a certain offset in millisecond. From the zerooth point to another offset in millisecond. Now you are comparing and saying two numbers that these two time are exactly the same time or different time. There are four ways you can create dates in JavaScript. One way that we have learned is using the new date this constructor without passing any argument. Okay, this is one way. Second way we pass is like you can pass the milliseconds since the epoch time which is the zero. This is the millisecond difference and you get a date. This is very useful for reconstructing a date from a stored timestamp. I have already communicated that you should be storing the date and time in the UTC timestamp in your database. So imagine that you're retrieving this time stamp from your database. You can pass it to this new date constructor and you can actually get the date. What happen if you put a negative value over here? I'll leave it to you. Try out a negative value and see what happens. It goes actually before epoch but see what it returns. Very interesting thing. Okay. Then what is the third and fourth thing? Let's do the third and fourth. Third one is with date string. You can create a date like new date and then pass a date string like this. This this particular format is called ISO format. It is Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y and then you have your mm and then the DD. Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y is nothing but the year then month and the date. You can also pass the date string in this format. When you pass in this format which has Z 1 Z or Z at the end, it means it is in the UTC time. Okay. Or you can also pass it in a human readable format like this January 15, 2024. So you can use date string as an argument to this date constructor and then you can get the date back. However, you have to use it with certain care right. So what are the problems? Let's take an example. Let's take a date like this. Const date only equals to new date of one ISO date. Okay great. Now the problem with this is this new date. If you pass an ISO date like this, it treats this particular string as UTC midnight. Now if your user's time zone is behind UTC, behind UTC means UTC minus something. Let's say Americans which is UTC minus something. Now this will display as the previous date. Now if you use a date string like this without the time, it may give you wrong representation of the date depending on your client's time zone. Now how to fix it? To fix it, always append the time and the time zone with it. So the unambiguous way of doing this would be like this. same date but I have appended t 000000 plus the UTC time. If you do that then things will be taken care properly based on the local time and the time zone. These are tiny things but you have to remember and that is how you won't be falling in the trap of making mistakes while creating the date. Third way is done. Now what is the fourth way? The fourth way is using the year, month and day as arguments. If you know that which year, which month and which day, you can use them as an argument and you can create it. But here there are certain mental load. For example, month starts with a index. It means January is zero, December is 11th. This is the month index. So you have to know the month index, day index and all this thing. And you have to pass those index not the actual name to construct the date. For example, if I want January 15th, 2024, I'll be passing 2024 the actual year. January is zero because the first month starts with zero and then the date is 15. Month index is something that you have to remember it starts with zero. What if you need something like this March 20th 2024 at 3:30 45 seconds P.M. If you need something like this but you are asked to create the date object instance using this particular methodology where you can pass the year, month and day. How are you going to do this? It's bit tedious but for our learning sake say we'll say as detailed date equals to new date. First you pass 2024 which is year then you pass the month month index of March is 0 is January 1 is February 2 is March so two then you pass 20 as the date then you pass 15 means 3 p.m. 12 + 3 3 p.m. Then you pass 30 and then you pass 45. Oh my god, this is too much. Now a tricky question for you. What does overflow going to log? If I do console.log of overflow with the variable name itself, I have indicated it. This is going to be 1st December 2024 or something else. Remember the month index starts with zero. That means January is zero. So whenever you are giving like 12, 13, it overflows to the next year automatically. So this is going to be January 1st 2025 because till 11th it was in 2024 December. The moment it become 12 it automatically overflows to next year which is the first month January. So it will be January 1st 2025. So sometime you might be tricked in interviews with questions like this. Be prepared. JavaScript's date object provides a bunch of getters and the setters methods. First let's talk about the getter methods. And in getter methods also there are two types of methods. One is for local time, another is for the UTC time. Local getters return the value in user's local time zone and the UTC getters return the value in the UTC. Always you need to know which one you need. Let us first get accustomed to all this methods. First let us create a date object instance. So I'm creating a date object instance by passing this string value. Now with this you can know like what exactly it can represent, right? It represents 15th of January 2024 at 3:00 a.m. and the Z means it's in UTC. Okay, so it's 3:00 a.m. UTC. Now let's talk about the local getter methods. This means based on machines time zone. Fair enough. Now these are the methods you can do. Date do.get full year is going to give you the full year representation which is like 2024 in this case. date.get month is going to return the month's index. It will return zero because this is where we are talking about the January month and zero means January, 11th means December. We have learned about that. This is going to return the day of month between 1 to 31. Okay. So this is going to return me 15. Day dot get day. This is get date and the next one is get day. Date dot get day. It's going to return the day of the week. Zero starts with Sunday and six is Saturday. Okay, this is index you have to remember. Get hours is going to return the hours from between 0 to 23. But get minutes is going to return the minutes between 0 to 59. Get seconds going to return the seconds between 0 to 59. These are very important the starting index. Okay, for day of the week start index is zero. Hours, minute, seconds, all start index is zero. Month start index is zero. These are the things you have to keep in mind. Then dot get milliseconds is of course the milliseconds between 0 to 9999 and date.get time we have learned about that milliseconds since epoch. This is one thing we learned in the beginning itself. All these methods are going to give you information based on your machine's local time. While local getters are based on machine time zone the UTC getters are not bound to any time zone. These are time zone independent. Exactly the same methods like get full year you will do get UTC full year. get month you will do get UTC month get UTC date just put UTC in the capital letters that's what the UTC version of the method so it's going to return the year in UTC going to return month is UTC again these rules are same a zero index day of the month day of the week etc so we have learned there are two varieties of method now this information are there everywhere is there in the MDN documents or any other content but how is it going to impact you that's what the main question let's take an example to understand the impact let's consider this scale the scale L is like this UTC we know it is 0 0.00 0 0 so anywhere we go left it is minus so at this point this is UTC minus1 here UTC -2 minus 3 and so on and the right side it is UTC + 1 UTC + 2 and UTC + 3 UTC + 5 and so on now let's take a country USA let's take a city New York and New York is at UTC minus 5 so let me circle this for you so we are at UTC minus 5 now in New York this is UTC minus 5. Let's see a time at UTC. What it will be at the new year at UTC minus 5. So a time that will take as 3:00 a.m. for now. Okay. Let's take a time called 3:00 a.m. at UTC. What it will be the time in New York. 3:00 a.m. 1 hour pass. It is 2:00 a.m. here. It is 1:00 a.m. here. It is midnight 12 over here. It is 11:00 p.m. over here. And it is 10:00 p.m. over here. Right? So 3:00 a.m. over here is 1000 p.m. Correct? If I also add the date, if it is like 11th Jan 2026 3:00 a.m. at UTC, it means in UTC minus 5, it is going to be 10 p.m. But on the previous day, which is like 10th Jan, you getting the point? So the current moment if it is 3:00 a.m. at UTC this is going to be that that particular moment will be 1000 p.m. previous night at New York which is at UTC minus 5 correct this is how the scale representation is very clear again telling for anyone getting confused at UTC 3:00 a.m. means over here it is 2 a.m. minus 1 over here is 1:00 a.m. Over here is if we go back 1 hour from 1:00 a.m. it means 12 a.m. midnight. Then if I go one more hour, it is 11 p.m. If I go one more hour, it is 10 p.m. Right? So that's how the scale works. Now why all this story? Now if you do date dot get UTC hours, which is supposed to give us the hours in UTC, what will you get? You will get three. So this is going to return you three. But if you do date hours, this is the hour that is in the [clears throat] local time. So being in New York, if you execute date hours, it is going to give you 22, which is nothing but the 10 p.m. So this is going to return you 22. 22 means 10 p.m. Correct? So you're seeing the difference. When you need the UTC hours being in any location on the globe, you will go forget UTC hours. But if you need the local hours for that particular place from where you are running this application, you will go for simply get hours. This is just an example. Similarly, if you do dot get UTC date, what you are going to get? You are going to get 11 because it is running on 11th Jan. But if you do date, you will get local date. You will get 10. That's the difference. These are the confusions. You write the method, you know, to implement something in your code. But it all depends where exactly that program is running, which time zone it is running, what kind of method you have used. These are the small nitty-gritty guys. That's what come back as a big thing in production. So please understand these things taking your time even if this video is longer spend time pause it understand what I'm teaching practice it by yourself try to run it by yourself sketch like this I'm sure that these things will be imprinted in your mind and brain very very quickly so that's all about the getters local time getters and the UTC time getters now we are going to learn about setters just as there are getter methods there are setter methods as well and just like getters setters also come with two varieties. One is for the local and other is for the UTC. One important thing though, setters mutate the date object in place. What does it mean? It means whenever you're using any of the setter methods is going to change the actual date object and they do not return a new date. This is very very important because some bugs come because of that. Once you use a setter method on a date, you might expect that this is going to return you a new date. No, it is going to mutate the existing date. I have a date now which I have initialized with the ISO format date. You can see over here and if I do a date dot2 ISO string is going to return me the exactly same string. Right now after this if you just do date dot set full year and do 2025 you had a get full year to get this 2024. Right? Now if you do dot set full year 2025 what it is going to do? It is going to change this exact date by making this 2024 to 2025. Okay. So remember this. Here is the list of setter methods. Exactly the opposite of get month starts from zero index. So 0 1 2 3 4 5 means June date set the date to 20th date. 8 hours 18 means it is 6 p.m. in the local because there is no UTC over here. So like that this setter methods will go and there are equivalent UTC setters. So what are the different UTC setter methods are available? You have set UTC full year, set UTC month, set UTC date, set UTC hours. Right? So these are the things available as a setter methods. Again and again I'm saying please avoid mutation. Let me write it over here so that you remember when you see this class code later. Avoid mutation. Always always clone your date object. Please guys remember this. Let's take an example of this so that you remember it for the longer time. Let's say we have a function. What this function does this function add days that means you take today's date and date it add a number of days to this right let's say original is 2024 15th of January now what will happen if I do this in the next line add days I have this original date and I pass the number of days to add is 7 let's take this in a variable const called future okay now can you tell me what will be the output of this to console log if I do original.2 date string future.2 two day string what will be the output what two date string returns if it is 2024 15th January it will return Jan 15 something like this representation Jan 15 so if this is what two date string returns what will be original string what will be future dot2day string you might say that okay original2 string which is like this this is going to return Jan 15 now we have added seven more days over here so this is going to return Jan 22nd 15 + 7 is 22nd No, you will be making a mistake if you assume that as we have modified the same date here with date dot set date method we have modified the original reference already. So both original and future is now modified to Jan 22nd. This is the mistake. This is where you have mutated the date by mistake at the first time itself. So please don't do this. So how can you now change this add days function to save from it? Of course, first and foremost thing is create a new date instance from this date and then use this guy to set the date and then you can actually return this new guy new instance. That's all right. So if you now call this then this is going to be Jan 15 and this is going to be Jan 22nd and it is now not mutated. Okay, I hope you understand that. So whenever you are dealing with date you have to ensure that this mutation never happens because if the mutation happens it might create a complete mess with your date and time. Let us get familiar with few more things around getters and setters. Okay the one of the main problem that comes from the zero index months that zero means January 11 means December. We tend to make many common mistakes. For example take this date is Christmas right? 25th of December. Now one of the common mistake that happens is when we do console.log date.getmon most of the time developers tends to feel that it is going to return 12 but this is going to return 11 because month's index starts from zero which is January 11th is December. So the correct way to always deal with if you want 12 always add one. So if you want 12 to be displayed anywhere you should always do + one to this then only your display value will be 12. though the API has returned you 11. Please remember that. One principle that you can follow, you can always create an array of months like January, February, March, array in JavaScript is also zero index. It means the index starts with zero. So month of zero is going to return you January. Now let's say you are in the month of December. So date.get month is going to return what? 11. And pass date.get month. And imagine you are in December now. So date.get month is going to return 11. So it become months of 11 over here. 11th index is nothing but the December because array starts with a zero index 0 1 2 3 4. If you go like this it is December. So this is going to return you December. So this is one way you can actually manage the months in your code. Same thing happen for the days of week also because that's also starts with zero index. Zero means Sunday, one means Monday and so on. So similar kind of thing you can apply over there. Let me talk about one more goes. All these things are from my personal experiences. So sharing as much as I can. Let's imagine you are building a function for today's components. Okay. And this is your function. Now can you tell me what is the problem with this function? Simple one. Get today's component instantiating a new date. So the got a new date instance. And then now dot get full year the current moments full year current moments get month plus one we did and then the current get date and we are having an object. This object we are creating. What is the problem with this one? As we understood before all the get methods having two variety one is the UTC and is the local. So this get full year get month get date as it doesn't have that UTC in its name is going to return all this value in your machine's local time zone value. Now imagine this get today component whatever the return value you are directly saving into a database through an API. So if you're in New York from there if you are client side executing this one you end up saving this object in your machines which are which is in New York that particular machines year month day that values with those values but if you remember this diagram in database we are supposed to store everything in UTC right in epoch format everything is in UTC we are not supposed to store any of the local time representation right that's the exact thing that's the exact mistake that you are doing here by using the get full year get month get date if you have such use case where you have to store this to the database not for the display purpose always always use get UTC full year get UTC month get UTC date understanding why you have to do UTC versus local and when and where you have to do it this is how you have to keep things in your mind and once you know these fundamentals nobody's going to stop you can write date and time code like a boss another idea where you got to be a bit careful when you want to parse or validate dates. Okay, for example, of course, this is going to give you exception because this is not a valid date and date objects handles that. So once you do console.log of this bat is going to say is invalid date. Question for you, what is this line number 123 going to print? Console.log is nan bat is not a number bad. What is going to give? Of course, it's going to give true, right? Because it's an invalid date and it's not a number. Is going to give you a true. Now the question is how can you effectively check if a date is valid date or not. One way that you can check is function is valid date. Let's take a date and you can return if the date instance of the date object. This is one. And if it is not of is not a number the check that we have just now did we can do like date do.get time right? So if dead dot get time which always returns that milliseconds in UTC time and that is always going to be a number if it is a number not of is not a number is a number right so if it is a number and if it is the instance of a date then you can say that is a valid date now as you have this is valid method you can make use of this is valid method to make the date parsing even more stronger especially the cases where your date is passed with a string representation and you have to parse that. So you can write a method like this. See this is a safe parse method which take a string date string. Basically first you check whether the date string is there or not. If it is not there you can return null. Then you create a new date. We know this is one of the ways you can create a date. Now once you get the date object you are passing through this is valid date. If it is a valid date you return the date otherwise return a null. So having a utility function like that is always good to handle the parsing of your date string. So this is something that I have been using and I'm just teaching you some of the good parts of handling date the correct way because dates are just numbers underneath. So date arithmetics will be just about adding and subtracting the milliseconds. Right? When you are dealing with dates and then you want to change the date by adding subtracting the milliseconds. It means that you want to go to a particular seconds or minutes or hour or a day. Right? So what I do generally I create an object like this. This is my millisecond object. I define a 1 second means it is like 1,000 milliseconds. I define 1 minute means you know 1,000 millconds multiplied by 60 so as hour and so as day. So it means anywhere in your code if you want to do some arithmetic operation like subtraction or addition for a day you can do ms dot day same for hour ms dot hour ms dot minute and ms dot second. Let us first create a date let's say con today equals to new date. Now how can you move it to in 7 days like from today to 7 days. How can you move it? You can add 7 days. I already have day. You can see over here. How can you do 7 days? MS dot day multiplied by 7 is going to be 7 days. Now I have today. So if I do today do get time, I'm going to get the millisecond representation of today. If I add 7 days to it, this total milliseconds is going to take me to the 7th day. So I can now write the logic like in 7 days equals to new date today dot get time plus 7 into ms dot day. Got it? If I have to do yesterday simple today do get time minus 1 into ms dot day I go one day back right very simple. Now you can always create a utility function or utility method like add days which can take a date take the offset days that you want to go and then you just pass them. Remember here I didn't mutate this date. I have created a new instance of the date. This is what we have learned not to mutate because you are going to use the set methods. Okay. So this is about adding and subtracting the time. How can we calculate difference between dates? One is 1st January 2024. Another is 31st of December 2024. How can you do a difference between them? Okay, we can do something like const milliseconds equals to end minus start. What it is going to give me? It is going to give me the millisecond difference between these two dates. If I calculate and print this diff ms on console.log, it will be something like this number. This big number. Now once you get this big number in millisecond, you can write your own logic to convert into useful units. So for example, if you want to convert this big number, the difference number in difference in days. Then how will you do the number that you have? You can divide by what? If it differs by days means in 1 day there are 24 hours in 1 hour there are 60 minutes in 1 minute there are 60 seconds and in 1 second there are thousand millconds right so you will be writing a logic like taking this t ms divided by 24 hours in a day 60 minutes in an hour into 60 seconds in a minute into 1,000 milliseconds in a second if you do that you get the this difference means there are how many days between this 1st January 2024 to 31st of December 2024. Similarly, if you want the difference in weeks, what you'll be doing? You get the difference of days, divide by 7 difference in week. So, this is how you go about finding differences. Right? Now, you can write a method which basically can return you all these different differences in different kind of units like days, weeks, hour, minutes, whatever the ways that you want. Now, here is a task for you guys. Can you create an H calculator with all this learning that we have? Okay, don't take uh the help of AI in the beginning. Okay, try to write and then whatever the code you write, give it to any LLM and ask the LLM whether the logic you have written is good enough or there are any improvements required or not. And whatever you get back from LLM, learn from that like what are the improvements required. But if you have written a logic which doesn't need any further improvements, great. Also you can come to our discord and share about your logic. Would love to look into it. So what we are going to do? You are going to create a calculate age function. If I pass any birthday for example say 199065. So it means that 5th June 1990. If I pass that you have to return what is the age whether age is 33 34 whatever the age that you have to return. So calculate age takes a birthday a date and it will return what is the age of that person based on this birthday. Can you write the function for this? So this is your task. Now let us get into one of the problematic area in managing date and time which is leap year. But before we talk about leap year, let's talk about a concept called solar year. A solar year is the time that it takes earth to orbit the sun. So you have sun in the middle and then there is earth. So is the solar year is the time that takes earth to orbit the sun and the solar year value is approximately 365.2425 days. This is not exactly 365 okay little more than that.2425 days. Now to keep our calendar aligned with earth's orbit we periodically add one extra day and that extra day is nothing but the f 29th. This day is called as lip day and the year this 5th 29th fall into we call it as leap year. Now this might sounds very straightforward that okay if there is a February 29th in a year we'll call it as leap year. But the calculation for leap year is not that straightforward. Let me write the pseudo code or or rather the algorithm for it. Okay. So a year will be a leap year if there are two or three conditions. What is the first condition? Year is divisible by four. If you can divide the year by four, year is always a number like 1900, 2024, 2026. If you can divide it by four, then it is going to be a leap year. This much we know but there are few other conditions. The conditions second is except it is also divisible by 100. If you can divide the year by 100 even though it can be divided by four it is not a liar. Okay. So this is where the complexity comes and unless it is also divisible by 400. This is what makes it lipier again. Okay let me make you understand with certain example. How about 2024? 2024 is a lipier because it can be divided by four. How about 1900? 1900 is not a lipier because it can be divided by 100 but not by 400. How about 2,00? 2,000 was a lipier because it can be divided by four. It is divided by 100 but still it can be divided by 400 also. So this is the rule. The rule is it can be divided by four except it can also be divided by 100 unless it can be divided by 400. So this is exactly the algorithm to know if a year is lipier. So coming to JavaScript, how are you going to write a logic to find out if a year is a lip year or not? Let us do that. Lip year. So function is lip year. Let's take the year that we'll be passing over here. So return true or false. So for that purpose if the year is divisible by four and how do we know if it return zero with the mod? Say divisible by four. and year divisible by 100. This is the case of not because except or year divisible by 400 and this is the case that we have to satisfy. So the first case is this and the second case is this. I think we can omit this parenthesis. Yeah. So if a year is divisible by four and the year is not divisible by 100 or the year is divisible by 400 in this case we can call a year is a leap year. Now we can run this. Okay I'm correcting the spelling of the function. Okay now we can run this with different test cases. If we pass 2024 yeah it is true because it's divided by four. This is where it will be true. 1900 false divide by 100 but not by 400. 2002 divide by 400 2023 false because it is not divided by four itself. When we talking about date and time you have to deal with one more beast and that beast is DST daylight saving time. What is DST or the daylight saving time? So daylight saving time is a practice that some of the countries follows. For example, the US follows some part or I'll say the maximum part of Europe follows but again many of the other countries like India, Japan, they don't follow it. It's a practice of advancing your clock by 1 hour during the summer months so that darkness falls later in the day and this is the whole idea of shifting by 1 hour daylight from the morning and it is when most of the people are asleep so that the evening more time can be make use of right that's the whole purpose of it now as all the countries are not really observe BST together it's very hard that for a particular go you have to keep in mind that you have to handle this DST. Now there are few things that you need to understand that this clock get advanced. Now the clock also comes back. It is not like once advanced it will always run in the advanced mode. No. So in a year when the summer start in a particular month it gets advanced and when a particular month comes again it gets again reseted. So how this happens? For example, in the US, if we see right now, you know, when we I'm making this video in the month of March, DST actually DST starts on the I think second Sunday in March. So what happened when DST starts at this time clock spring forward 1 hour at 2:00 a.m. Okay, at 2:00 a.m. Remember this, it just spring forward at 2:00 a.m. Skipping to 3:00 a.m. This is when the advancement happened. And when it ends so it ends in November precisely the first Sunday of November and very interesting on this day how it get reset clock falls back at 2:00 a.m. repeating this is very interesting repeating 1 a.m. to 1.59 a.m. twice. So when DST starts on the second Sunday in March, 2:00 a.m. will be skipped to 3:00 a.m. get advanced and to readjust in November on the first Sunday of November, it repeats 1:00 a.m. to 159 twice. So that it gets readjusted. Okay. So this is how the GST works. Now this can create nightmare. If you're not aware of this logic, if you're not aware, there is something called DST exist. So at times you might have to keep this into account to check whether the time range when your operation is happening is DST applied or not. If DST is applied you will do certain things in certain way. If DST is not applied you will do certain things in a different way. So I'll tell you how JavaScript handles this. Let's talk about DST advancement. When DST start, right? So DST starts like by skipping 2:00 a.m. to 3:00 a.m. on 2nd Sunday of March. Let's take a date hypothetically. Let's take a date say 10th March. 10th March when say DST starts. Okay. So when I create a new date passing all the year, month, date, time, everything like this, you know this is one of the ways we can create date. This gives me March 10, 2:30 a.m. Correct. Now if I just run this code in the US eastern time zone this time on this date doesn't exist because when DST starts 2 a.m. skips to 3:00 a.m. Then where is the 2:30 coming from? Okay. So JavaScript will automatically shift it forward to 3:30 a.m. It will shift it forward. So for example if you take this in a variable say const skip equals to new date this and if you do skip dot get hours you will not get two you will get three javascript will take care of this shift forward so shifting is good please remember this let's talk about the falling back on November right and it happens between 1:00 a.m. to 1:59 a.m. So 1:00 a.m. to 1:59 a.m. on that particular day of November, the first Sunday of November happens twice. This is how the DST works on those time zones. But unfortunately, JavaScript cannot distinguish between these two 1:30 a.m. So, it picks the standard time. Nondst time is what is going to pick. So, this is something that you need to remember when you are dealing with DST for any specific region using JavaScript. Now you can go ahead and check how to determine if it is DST or not. Right? You can just do any LLM search, any Google search, anything of this particular thing is like how to detect DST. You will realize the logic for that you are going to understand without any difficulty now because you understand all the basic methods that JavaScript provides you and on top of that using those methods you can do multiple things. For example, one of the thing is like you can check whether it's a DST or not. Check out this method. Do some research about it. So before we end this part, let's revise some crucial points. First point we spoke, always store as UTC or time stamp, right? Never the local time. Second, compare using time stamp not by the actual date value. Then always parse ISO 8601 string, the one with the Z or the Z, right? Because that's the UTC format. Never ever muted the date directly. Always create an instance and then mute. Months are zero index. January is zero. December is 11. Always add one for display. Right. Add days with set date not the raw millisecond. This is DST safe. And the last one use UTC getters on the server not the actual getters because that gives you local time. So hey that's all about part one. In part one we have covered a lot of things. We understood why date and time is really complicated. The history behind it, how to take care of things, how to create dates, how to do the arithmetic operations, the gators, setters, differences, then the leap years, then the DSTs, the good practices, some of the gotchas. There are a lot of stuff that we have covered. These are the foundations guys. If you know this foundation, you are going to do great with date and time in JavaScript. My request would be please practice some of these things, right? because I didn't want to make this video for like 3 four hours with so much of examples and practice because those examples and practice are available on internet. You just need to remember these fundamentals that we have ran through together. Coming in the part two, we are going to make it in the professional way. That is where you will be learning how to do things like you know 7 days ago, 3 days after all this kind of debt calculation. You don't need any library is provided by JavaScript. There is an API called INTL. Using that we'll be doing lot of stuff and I'm also going to get you introduced to the new temporal API. So if you found this video till now the part one, drop a like and subscribe. Today I want to welcome you to part two. But if you haven't done the part one, my high recommendation would be to finish that and then come to part two because we are going to build a few things on top of whatever you learned in part one. In part two today we are going to talk about JavaScript's one of the most underappreciated factor that is INTL API. Believe me it's an underrated stuff that JavaScript has. Many of the developers don't use it because they are not very much aware of it and they end up using some external libraries that library go out of date. They have to manage migrate and all these things they need to do. Instead of that JavaScript natively supports you everything that you need to do with date and time format. Yes, it's including time zone, including locals, including other geographics. Today, in this video, we are going to master all of this. Trust me, by the end of this video, you will be able to format any date and time in JavaScript in any language in any local without any external libraries. Let's go. The Intel API, short for internationalization API, is a built-in JavaScript namespace that provides language sensitive operation. By the way, in lip is not just for dates. It's for number formatting. It's for currency formatting. It's for pluralization and more. But for our series, we will limit our discussion to date and time. We have an in-depth documentation provided by MDN on INL. You can always come take a look into it. Go deep dive. However, in this video, my objective is to pick the one that will be heavily used. Pick the one that you need in your daily life to fix things, to format things with date and time. So if you just focus on part two, I think you will be able to get to know most of it. If there are any deltas, this documentation is always there for you. Today's session, we're going to code a lot. That's the reason I've opened up my code editor and running the browser side by side. So that whenever I change some code, you can see that immediately in the right side browser step tool. Here's a simple HTML through which I'm running this index.js file and I'm going to code everything inside this index.js file. This initial console.log log you are seeing like js intl API session is already appearing over here proving that whatever I'm going to code over here is going to reflect one quick thing I have just dstructured the log from the console object so that I don't have to write console.log log everywhere. I can just write log. It's easy. Less typing. All right. So, let's get started with our Inel API. Before the Inel API ever existed, formatting date and time for the international users was a nightmare. You have to think about it. You have to manually look up how each country format their dates and time. Then you build a lookup table for translating the month names, the right to left languages like Arabic. manage different kind of calendar systems. Think about it how how how much difficult it is. It was an enormous amount of work that every team had to do from the scratch. I have spent more than 20 years in software engineering and development. I have done it myself. I know how hard it was. Just take a look into this different kind of date and time format. For example, in United States, you call out the date formatting like this. First the month, then the date and then the year. and it is like 2:30 p.m. But when you go to UK, it changed. The formatting now become dd mm y and now it's a 24 hours clock. If you go to Germany, instead of slash, it become dot and you see it's in their native language. If you go to Japan, the formatting again change year comes in the front. So for every countries, for every local, every time zone, there could be a possibility of different date and time format. Now imagine that you managing all these formats for geographies locally in a code like a map like a table like a JSON object keep maintaining it it's a nightmare thank god we have INL API today the INL API is defined by the uni code what is CLDR clr is the common local data repository like a database which is world's largest database for local aware formatting rules this is where all different kind of formatting rules will be stored. So whenever you use INTL in JavaScript whether it's on the browser or on the NodeJS side the runtime consults this particular database to produce the correct output for any local under incl. The primary method is datetime format. Datetime format constructor takes two arguments. The first one is the area of locals or a single local. The second is a configuration options. By the way, both these arguments are completely optional. You may or may not want to pass them. But we will see a lot of examples where passing the local and passing the options are very very powerful thing for you to convert a date with a particular date and time format. So let's take this new intl time format. I'll copy it and paste it over here. I don't need any of these arguments to start with. Now what it returns? It returns a formatter because we are doing a date and time format. Right? So we can do const formatter equals to new intel date and time format. Once you get this formatter on formatter you can invoke a method called format to format the date and time actuary. So what we can do we can do fmt that's the formatter. We can invoke a method called format and we can pass any date to format. Let me put this entire thing inside a lock because this fmt dot format is going to return me the formatted date. Okay, I have to do const here spelling mistake. So I got this particular date that I have passed over here formatted in my current local. Fair enough. Similar to this, I can just pass date now which is the current moment of time in the milliseconds. We have learned that. So this is going to return me the same thing because we're talking about just the date. I can even pass any milliseconds in date representation. You can see I'm passing this particular milliseconds which is going to give me the date but in 2025. Okay. And if I don't pass any date to the format function then it is just going to format the today's date. Okay. So all this usage now you know of format. Once you have a formatter from intlatime format you can invoke the format function on top of that or the method on top of that and then can pass any date that you want to format. Now this date time format I have used without any local and any options. Now let's see some example with locals. So first get this new intlatime format and pass the local enus. Let's take this in a variable called US fmt means US formatter. Similar to this let us create UK formatter, German formatter and Japanese formatter. Same thing just pass the different local. Again locals and the options both are optional. I am just passing locals now so that I can show you the advantage of it. Now let's take a date. So in UTC today's date time is 2:30. You can see that. Let's start formatting. First we will use this US formatter. Once we use the formatter now you know we can invoke the method called format. And over here we can pass the date right. So we can pass this particular date. You see over here in EN US it is showing me date in this particular format. All right. Just to make it more clear I'm just adding the string. So with US formatter I'm getting the date formatted in this way. So similarly if I try out all these different other formatterers with UK formatter UK formatter dot format pass the same date. D formatter. Pass the same date and see the log see the differences. In US it is mm dd Y. In UK it is dd mmm y. In DE it is dot like dd.m m doty y. In Japanese it is y / m/d completely different format. You don't have to really remember which format which local supports. You just pass the local create your formatter. On top of that call the format function pass your date. It is going to return you the formatted date for this local. how easy it is. By the way, coding with me, right? Don't just look what I am coding. It's not going to be of any use. Please open your favorite code editor or open your browser, go to dev tools, go to the console tab and start coding everything that I'm coding. Pause the video wherever you want, look into the things that you're coding, what kind of output it is giving, grasp it and then only look into the next section. Okay? Without you do hands-on, going through any tutorials is of waste of time. All right? Now the third part in the third part we are going to learn about this options. So when we do intlate time format we can pass the locals. We have seen that example. Now we'll be seeing the example like what kind of options we can pass. So first let us take a time. The time we have taken again today's time and then we will get the formatter. Let's take it in a variable called dtmt which is nothing but date formatter. Now here two things we can pass. First we can pass in US. What it is? It's a local and if I just do log of dt fmt you know it what it is going to give right it is going to give me this date time format object if I do dot format on top of that because that's what I have to pass to format the date now it is going to give me the formatted date in US local now I want to learn certain options let's say that the option I am interested in right now is how to style the date that I'm getting from Here I can actually style the date. Okay, how can I do date style? I can pass a option called date style and pass different kind of value for it. For example, full. Now let's see what it gives. It gives me the same date but formatted into a full date format where it says this day is Sunday completely March 15, 2026. Complete date in a elaborated full format. There are other format for example instead of full if you can just go long. L ng if I do you see this going to change it's going to change to March 15 2026 now the full format was telling Sunday Sunday gone now it's saying March 15 2026 if I just do short instead of all this now it is going to tell me 315 2026 the format that I was getting by default so what does it mean where exactly these options are very handy if you want to get a very elaborated date format formatted in a particular local go for the full format then if you Don't need that. Go for medium format. Go for long format. Go for short format. Okay. So I have given you the full over here. I have showed you the long short. Please try long, medium, short. Everything as and when you're trying stuff. So this is great. DT format worked. Now can I do same thing for the time format? Yeah. I'm just going to copy this guy. Okay. For you. I'm just going to copy this from here. And then I'm going to paste it the same thing. Okay. It's going to crash for some time. It's fine. And I am going to change this variable name dtm dt fmt to tm fmt means time format. Okay. And instead of date style let me just say time style. And here I will pass tm fmt dot format. Now do you see this output? It's giving the time in the full format fully formatted 80045 p.m. with Indian standard time time zone also exactly the similar way how we have tried different other styles. For example, if I go long, see the change the IST has gone. Now it become GMT plus 5:30. If I just do short, see the change, it just become 8:00 p.m. You saw that? So now based on whatever style you need for your application, you can just do it just by passing some of these options. So please play around with it. That's why I'm saying it is very very useful. Now can we combine both of these? Yes, we can combine. Let's do that. Let's do con fmt same new intel. Time format and here I'm going to pass some options. What are the options I'm going to pass? If I have to combine them, I want for example style in short, time style in short. So I will pass both the formats like this and after that I will just do log of fmt dot format pass the date. Now you will see a new output where date and time both are formatted into the short format. Oh you want date in full time in short. Yeah why not make date is full time is short. Yeah is full now time is short. Oh you want this also in full? Yeah let's do that. You see that this also in full. This is one thing that you do all full you know where you do mostly for the logging whenever you want to log something you generally go for everything kind of full full this way okay this formatting is done now can I repeat this for other local yeah I can do it do for D I've created a D formatter same I intel dead time format but the local I have passed DE for janman and date style full time style short I have used full and short and I'm printing over here you see over here see the difference the Sunday here in English but over here in that particular language right that's the advantage see the march over here and see how they call merch right can I do the same thing with Japan oh yes I have created now Japanese formatter passing jp and make it like full and short and when I log this by formatting the date you see it's coming all in Japanese and this is how you need it you cannot represent a date and time in English if you're serving a Chinese or the Japanese user they may not be that much accustomed with English language always you should not be assuming that rather it should be formatted into their native into their local language that's exactly we are doing here with date and time formatting okay now let's see some more practical examples so this far we have learned that we can do intlat time format can pass the local can pass the options to format a date now this is like bit verbose you have to do this inl deadet time format every time to get a formatter then you have to call a format method on top of the formatter you have to pass the date. Instead of doing all this circus, you know what we can do? We can write a single thing. For example, we can write something like con fmt equals to this is a function. It will return a function. Pass the local and then pass the options opts. Let's use an arrow function over here. And what it returns? It returns the new intl dot datetime format. And then you pass the same local same opts over here. On top of that you call your format function or the format method and pass the date. Now this behaves like a oneliner replacement of all those things we were doing before isn't it? So fmt is a function now because this is what we have assigned a particular function. It's an arrow function. This functions return what? It returns the formatted date for you altogether. is calling new intel.et date time format passing the same local same opts also invoking the format method on top of it passing the date that you want to pass and then returning you the actual formatted date right that's what this FMT function is going to do you can even pass this date as a parameter and can pass it over here that is also fine but as you have hardcoded the date over here I'm going with this version how can I use it so now you can do fmt first pass the local enus and then you can pass any options two options you learned so far date style and time style. I'm going to teach you a few more important options and options you can pass as an object. You can say in the date format I want the month to be long, day to be numeric and year to be numeric as well. Okay. And now this is going to return me the formatted date, right? Because fmt is a function which can take a local which is like this and a bunch of date formatting options that you have passed and it is going to return me the formatted date. So if I just pass everything inside our favorite log is going to print that formatted date for you. You can see March 15, 2026. The month is in long. It says March day is in numeric 15 year is in numeric. Okay. From long can I change to short over here? Month become short. You see it become m from March. When I used long it was March M. But when I do small it become M. So like that you can change and you can take up the one that you are looking for. Okay. Now you want to compact the date little bit more. Let's use another format. So to compact the date I have used month as short which is M day as numeric. So a compact date where in a UI you have only small real estate to show certain date format. Yeah, you can go ahead and use this compact format for that you are not doing any string operation. You're not cutting trimming any string rather you're utilizing a particular date and format given by Intel advantage because now the same compact date if I just do copy paste and I can do like de- see this I'm getting for another local all together. So you don't have to fix it using any kind of eelss in your code. You don't have to do string operation. You don't have to keep any lookup table for different languages. Just one line of code is going to take care of everything. Let's take some more example. You want to display month and year only. Yeah. In US or any other local monthlong year numeric. See this only March 2026. How about showing the day of the week. So this time I made the weekday as long as an option. I can see Sunday month long day numeric. So you can play around all these options exactly what you want. And this is where guys that MDN website what I have showed you in the beginning will be very helpful. This all all these options you don't have to really keep it in your mind. You can always go to the MDN website and you can see which format will be useful for your use case. Just pick that up pass it as an option. That's it. Similarly, we can also format the time. This is where it is resulting into a time formatting. You see it's a 24 hours clock. Why it is a 24-hour clock? because I specifically saying 12-hour clock is false. If you just remove this, this is going to be 12-hour clock is going to say 8:00 p.m. So if you want to print it in the 24 hours clock, just pass this as an option hour 12 as false. Hour I made two digit minute I made two digits. See how it is. I was talking about right that when you want to log something log means uh not only on console.log log but also in a log that's at the server side and log is a beautiful thing that gives a footprint of what exactly happened and what exactly happened also needs when it has happened. So it means that time is very important when certain things happen when you're logging certain things. So here is a full timer for log that you can use here you are saying we are formatting in this local but year is numeric month is twodigit day is two digit hour minutes everything in two digit and it's a 24-hour clock. So if you see over here this is how the date and time is printed. So for every occurrences in your log if you print the log in this format that's awesome. Same thing same format if you just take it you know you just copy it paste it over here just change the local to Japanese like JA - JP and you can see how things have changed right the formatting has changed at least in the date case it has changed. So these are the different other powerful use cases, practical use cases where you can use the options to tackle each of the fragment of the date and time in the date and time formatting. Amazing. In the part one, we touch base this one briefly. I'm just going to reiterate again. Many times there are confusions between time stamp and time zone. I've explained that there are also confusion between locals and the time zone. Okay, let me define them. A local which we have seen as EN - US like J- JP all those things the local actually determines how information should be displayed a particular language or a date and time or the currency format etc. While the time zone defines what time is based on a location relative to the UTC. Okay. So far whatever we have passed over here all are local because we are talking about the formatting. We are talking about the formatting. These are all like one certain format of data and type. Now I'm going to talk about time zone. Time zone means we are now going to talk about a particular place in your world time zone map. You remember like from the part one we had UTC. If you go right side it is UTC + 1 + 2 + 3 like that. If you go in left side it was -1 -2 -3. India is in + 530. Somewhere in New York we saw like it was - 9. So all those things we have learned in the part one. That's the time zone. Now how can we actually see how time zone influence some of this date time format that's the objective to show. So for that let us first take a date in UTC. This is a date creating a new date and this is a date in UTC because this ends with Z right? We have learned that in the part one. Ends with Z means it's a UTC. Now we will take our same formatter function. It actually returns a formatted string. Okay. Now let's define a few options. I have defined a few time options that the time style is short and dead style is also short. Now if I do fmt fmt returns a formatted string. I give the local enus and then I pass the options. Before I was passing the option directly over here the object. Right now I have just declared at the top so that I can utilize it in multiple places. That's how that's all about it. Then I will just pass this inside my favorite log function so that I can log it. is logging 3/16/26 1:30 a.m. What is my date? If I look into the date, it's 15th of March 2026, 8:00 at UTC. Same date I am formatting over here using the option called time style and date style. But the output I'm getting is the next day 16th of March and it's 1:30 a.m. in the early morning. Why? Here it is just 15th of March 8:00 p.m. Here it is early morning 1:30 a.m. next day. Why? Because this time is the UTC time. I am doing everything here from India. My browser is running on India. India's time zone is UTC + 5:30. If UTC is zero, then plus 5:30 from there. If it is 8:00 in the night on 15th of March and if you add 5 and 1/2 hours means plus 5:30 5 and 1/2 hours you cross 15th of March you go to the next day 16th of March and you go to exactly 1:30 a.m. So if you're not passing any time zone over here, the formatter function automatically takes the time zone my browser is set to and it formats the date for that particular time zone. That powerful it is and that's the impact of the time zone over here. Now what if programmatically you have to change this time zone. Yes, that provision is also there. If you want to change the time zone programmatically, if you don't want that this particular date time format to be displayed on the time zone where your client is, the client's browser's time zone, then you can pass the time zone information as an options over here. Let's see that. So this is pretty much local time zone. Okay, this is local time zone. Now if I want it in American time zone like American New York what I can do I can take the same log for a moment but in the time options I can dstructure it normal JavaScript right to add a property to an existing object and I can pass the time zone as America New York. Now do you see what is the time is 4 p.m. So if you convert 15th of March night 8:00 at UTC to the time zone of America New York you go - 9 you go 9 hours behind that's the reason it is 4:00 p.m. over here 9 hours behind than the 8:00 p.m. Okay. So this is how it is. So in local time zone, this is the time that's my local time zone. This is in America and New York time zone. Let's try few more. Now I'm trying in Europe London time zone. So time zone I'm passing as Europe London over here. You see this again it will change right? How about I try in Europe Berlin. So I here I'll pass the time zone as Euro Berlin. You see over here and of course it's in Germany. So I'm formatting the date time also in this particular format. You see in Europe, Berlin it's completely a different time. So these two things I want you to put some focus over here. This is just for formatting in a particular language. This is the date format. This is the time format. This is just for formatting. However, this part is for the actual time in that time zone. That's the difference guys. If you do a mistake here, you will never be able to fix a production issue that comes on date and time. You have to very clearly know the first part is just for formatting. Every geography on the earth has some way to understand how the date should be written, how the time should be written. This first part is just for that. Actual time would be represented by this time zone. If you don't pass anything, it's your browser's local time zone. If you explicitly pass something, yes, it is that time zone. Okay, I'll give you another example. Do you see over here how this formatting is done in 150326 in Europe Berlin? This is correct because this is the converted time for the UTC to the German time like in the Berlin and German guys understand this date time in this particular format. Now if you want to change this format but still want to keep the Germans time, you can just do enus. You'll see what happened. The last one now the formatting changed but the time is still same. It was 9:00 24 hours format. Now it become in 12-hour format. It was with dot it become slash. Time is not changing because you're changing the first one. It's just the format changes and you want to keep it in the format that certain people understand from that geography. That's why using the relevant format along with the time zone over here. I hope this is very clear guys. I actually going how much level possible for me to make you understand. I hope it is clear. All right. So let's take Japan in Asia Tokyo that's Japan. This is how it is the time right now with the right format. Awesome. You understood? Great. If you like it, please post a like, subscribe to the script so that you don't miss out any other videos. Okay. Let's talk about time zone names. You would have heard about IST. But there is a bigger form of that is called Indian standard time. Similarly, for many other go there will be a short form of a time zone, the longer form of a time zone. With the time how a time zone might appear, everything differs. So let's take some examples to understand different time zone names. It's very important. Okay. So I have taken a date again in UTC. The date is 15th of March but at 200 p.m. in UTC. Let's take a time zone of America/ LA Los Angeles. And here I have the same FMT function like before but I have hardcoded many things. Finally it returns a formatted date. That's what it does. But I have given some options hardcoded. For example, I want hour in the numeric format, minutes in the twodigit format. Time zone. This is the time zone that I want. and what kind of time zone name I want. What is the format of it? That's what I'll be passing to this particular function. If I pass a long format like log long while invoking this fmt with a date, date is already hardcoded over here. It will give me a long formatted date. If I do short, it will give me a short formatted date like that. So, let me do this fmt and I'll start with short sort and let me push this inside my log function. So I see over here 7 a.m. PDT. Okay. Why PDT? Because I have taken time zone as America/ Los Angeles. So it will show one of the time zone of America. By the way, America, you know, it has multiple time zones, right? Unlike India which is having only one time zone. So one of the time zone is PT. That's what is showing. Okay. Now if I want to elaborate that, I'll just copy this, paste it over here. And instead of short, I'm saying it's long. Now I see specific daylight time that's what PD stands for. So like that you can get various different other formatted name. For example, you want to get a short offset name, you want to get a long offset name, you want to get a short generic name, you want to get a long generic name, all these things will appear over here like so based on what kind of display you want to have for your application, you can choose different type of time zone names. Very very useful, very very powerful. You remember when we discussed about the JavaScript data object in the part one, I told guys JavaScript data object just support two things, two type of time zone. One is local time, another is UTC. But with the INTL API, now you are playing with any time zone of the world. Now I'm getting into one of the most exciting and one of my most favorite section of INL that is relative time formatting. Believe me, without this relative time formatting means you have to rely on data finesse. You have to rely on moment JS. You have to rely on some third party or you have to write lot of logic to get it done. But you will see it now how simple it is. So far we were dealing with only dead time format. Inl deadtime format. But now let's kill that boredom. Let's talk about intlative deadtime format. And again same thing you can pass a local enus. And then you can pass a bunch of options. Most used options are style and numeric. I'm going to explain them and let me take them in a variable called RTF. Style and numeric are two different options. There are few other options as well but these are the most used one. Numeric is auto means I'm telling inl whenever you're returning me a formatted relative time information you don't have to always give me the information in the numeric format. Sometime you can combine to make it as a alpha numeric but instead of auto if I give always this is where it is always going to give me in the numeric format style what are the other different things are there just like long there is a short there is a narrow we going to see all of them okay so for now let's go back to auto now here is the fun what if I do RTF dot format same like dead time format the relative time format is also having a format method to format things now here you're talking about relative what does it Mean if I'm standing today I want to know what it was a day before what it will be after 7 days what it was 36 months before what it will be after 2 years I want those kind of formatting representation so let's do RTF dot format minus one day and I'm going to put this inside my log function see what it prints it prints yesterday correct now if I'm standing today so it is always relative to today now if I'm invoking it now minus1 day from today is yesterday of course then what is minus 3 day it will be 3 days ago correct now imagine you have a date in the part one we have learned about date difference like and the day difference so you can write your function to identify there is how many days difference between today and yesterday you found the number as three now you do a string by concatenating that three plus space then days then space ago. This formatter function gives you this one directly. Why it is simpler? Because what if the difference is one day? You cannot say 1 days ago. It cannot be plural. It has to be day. Right? For singular one day ago. But for three, it has to be 3 days ago. You have to write this logic sitting now. If it is more than one, I'll be saying days. If it is less than one, I'll be saying day. All the things. Why this formatter function is giving you with all those control. Let's see some more. How about this two minus one week it says last week that's how it formats minus two months two months ago you see this so as and when this result you get with your own computation between how many months difference how many years difference how many days difference and you want to show that on your UI in a much more better formatted way this is how you'll be doing by the way if I want to show this not in English in German what will do you should tell D E D E do you see this what is happening I can't understand because I don't know German but it changed the language itself is changed beautiful isn't it let's take some more examples minus one year minus one year is nothing but the last year one day now no minus I'm going to positive next one day is tomorrowus one day was yesterday this one day is tomorrow 3 hour means in 3 hours minus 3 day was 3 days ago One week will be next week. Okay. Tell me what is zero day will be? If I put zero here, put day here, what it will be zero day? Of course, it will be today, right? Because the marker always starts from today, right? What will be 0 second? If I put instead of day here, if I put second, what do you think? What it will be output? It will be now. See, it is now. Got the point? So, this is how you can do the relative formatting. Now I'm going to teach you another real or something and especially with a library like react or angular or whatever it is where you build the applications and the method name is format two parts same on the formatter the relative time formatter not on the date time formatter you have a method called format two part it takes a similar kind of argument let's say minus 3 day okay I'll take this one and I will pass over here now if you call format it gives you the formatted data minus 3 day it gives it gave you 3 days ago. Okay. Now imagine in your React application you want to make these three bold days in blue color. Ago is in red color. Of course you won't use that kind of color but imagine. So now what you'll be doing after getting after formatting using format minus 3 day after getting this string you will be calling a method called split which is going to split this string based on this space and give you an array with all these three days and ego as element. Then you can do you know map on top of that and then you can do whatever you want to do. But instead of that if you call format to parts this is going to do the same thing but much more safer way. So if I do log of I will take this in a variable called parts and I will pass this parts over here. So you see it is giving me an array of two elements. The first element is an integer which is saying three and the second is the days ago. So this is the meaningful way in thinks that it has to be splitted. So that three you want to put into one style days ago you can put into another style. It is not splitting days and ago again because it doesn't make sense even from the UX perspective because days and ago has to be together. Okay. Why I'm saying it is much more safer? Because if you just now change this en us to de you see the enter things get changed. Now it is returning three things. Why? Because 3 days ago has changed to this particular stuff and now it thinks that it is meaningful to split them into three different sections because that's how the user from that geography can perceive the information. Three one thing days ago is another thing. English speaking people can understand that people understand English can understand this separation but for German the separation has to be different that's how it is giving you the output very very powerful thing guys please make use of it I hope this blow your mind and you will start using this very very effectively uh by the way I spoke about this style and this auto but before that let me change it to us so that all of us can understand clearly everything is in English now if I change the style to say short you don't see them in this long format. See everything becomes short, right? M O Y R things like that. Now if you make it always in instead of auto you see the representation change. If I make it auto you see the representation next week 2 day now. But if I do always it will honoring the numeric always like in 1 day in 3 days in 1 week in 0 days in 0 seconds. So whatever the ways it makes sense for you use that particular style and the numeric. The next thing you want to learn is how JavaScript picks a local. Okay, good. So, let's learn this. So, here is a question for you. I have a date, you know, in UTC. Again, I have two locals over here, BN and ID. BN is for Balanese local. ID is for the Indonesian local. I have passed this locals over here like both of them as an array. So, what is the local that picked up when JavaScript formatted this date? Can you answer that? Now, one thing you need to understand when you request a local, JavaScript doesn't always have exact match. It uses a negotiation algorithm to find the closest match for a supported local. Understanding this prevents lot of surprises in production. In this case, the BA is not a very well-known local than ID. Indonesian is a well-known local. So, JavaScript's negotiation algorithm automatically will picked up ID. It gives it a higher priority for a local. And we'll be formatting this date with Indonesian local. Okay. Let me teach you one more practical usage. Folks who works on NexJS especially NexJS i8N part of it they use the proxy or the middleware to identify what is the local they have to pick up dynamically. They use some of these things. Okay for your client what is the language that is set on the browser. How can you get it? You get it using navigator.language. Navigator JavaScript object widely available under the window object. If you do navigator dol languages, it is going to give you all the languages set on your browser or your or if your customer is using your application, your customer's browser, just take it inside a variable and then do a console dot log of user locals. Let's see what it prints for me. So, it's printing 10 locals because this is how I have set up my languages. The first one is English in India, then Bangla, then Spanish, German, you know, I have Chinese because I have to test some things. I just drag and drop going to my browser settings and languages. I have bunch of languages. I have some unknown language called Xhosa or HOSA for testing my localizations multiple times. Okay. Now let's say your applications having certain locals defined and you have defined them in this particular browser. These are your applications master local. Now how can you ensure that when you're doing formatting JavaScript picks up the best possible local match for you? Because your browser is giving this many local. You also have this applications local. What is the best match JavaScript can find among the user locals and the app locals. Okay, for that purpose INTL deadtime format gives you a function called supported locals of here you pass your user locals and then you pass an option that how you want this match to happen. There is a property called local matcher. You can pass it as a best fit and this gives you the negotiated local. So if I just take this into a variable called negotiated and I print this log of negotiated local. Yeah, now I get all the negotiated local. So if you see from here xh has gone because javascript thought xh is something that it cannot support after doing this negotiation. So these are the locals that are supported. I should not be using console.log, isn't it? It should be log always. All right, we know this, right? intel. dead time format and then call the format function on top of that and get a formatted date. Now one thing about performance creating an inl.etime format this object inl.etime time format is not cheap. Under the hood, the browser or the NodeJS environment, wherever you're using it, has to parse the local, negotiate the best match, load the relevant uni code data. You remember the CLDA database we spoke about, then compile the format pattern, set up an internal state for it. Lot of such things it has to do. Now if you doing the same thing like 50 60 70 times in your code you are going to make this calculation 50 60 70x slower. So how you should be handling this? You should never create a function like this where you are actually calling intel. Time format and call this function multiple times in your code. Rather you should use some amount of caching and refactoring. The correct way of doing would be create the formatter once. Once the formatter is created then create a function using this formatter you can format it. This format is not costly but creating this formatter for this particular local a particular time zone with a particular style is very very costly. So always create the formatter somewhere outside and then that formatter use it everywhere in your function or create a function for the format and use that formatter function everywhere. That's how you should be using. Please keep this in mind otherwise your application will be unnecessary slow. I'll show you a pattern that I use very often. I create a formatter cache using the map. You know what is map in the 40 days of JavaScript course. I have taught you map set hashmap hash set everything. So you can take a look into that. So this is a formatter cache that I will create and using this cache I create a function called get formatter. Okay, it takes you see local and options. Those are the two things your date formatter also need if you remember. Then I create a unique key. The unique key is for this particular local. What are the options? I combine them and I create a key. And what I do I first check whether for the same local same options somebody have already asked me a formatter or not. If no in that case what I do I put that into cache and then I return the formatter. Okay. If it is already found in the cache I directly return the formatter. So it's a cache thing right? If you found in the cache it's a cash hit. you directly send it from the cache. If it is not found in the cache, it's a miss. You first set in the cache and then you return from the cache. Right? So this is how you should be maintaining a formatter cache. And once you have the formatter cache, you can now use the formatter cache. Create a function for formatting the data. Take the date local and option. Here the formatter function get formatter function is going to return a formatter either from the cache or creating one putting into the cache and then returning. Getting the point? But you are not creating it again and again right the expensive operation. I hope this makes sense to you. Now the last thing with the inel API we are going to learn what it is. We are going to talk about the format range. Okay. So far we spoke about only single date single time. Let's talk about some to date from date scenario. Here I have created a formatter same intel. Time format for this particular local with these options. Month should be long day should be numeric. Year should be numeric. Let's take some dates. One start date and multiple end dates. On the first end date, it's a same month, same year of the start date. In the second end date, it is a different month than the start date. In the last end date, it's a different year altogether than the start date. Now, we are going to format the range. Range means from this to this. For that, I already have this formatter. I'll take this formatter and I'll do log of formatter dot there is a method called format range. And here first I'll pass start and end one. So look into this March 15 to 20, 2026. It is showing only in terms of date because the dates are both the dates are in the same month same year. So there is no use of repeating the month like in a different way. So March 15 to 20 that's how it is formatting. Now if I take the same thing twice and here I'll do for the number two and here I'll do for the number three just to show you the difference. Here it's automatically recognize it's a different month altogether. So it has to put the month March 15 to April 10th but again it's in the same year. So year was not repeated in both the cases. But in last case year has to be repeated because they fall under different years. Isn't it? So it is 2026 to 2027. This is how it is giving you the formatting. How best is this? So I hope you learned something new. You learned a lot about Iintel, isn't it? So how about bringing everything together and create something which is very very useful. So what I did I created an npm package last night only. I did that. Whatever we have learned in this session plus few other nitty-gritty things putting together I have created an npm package that is not dependent on any external libraries for date and time formatting purely uses the INTL package of JavaScript the INL API and it gives you a bunch of utility stuff like this you know how to format a date format a time format date and time formatted local date time ago you know 3 days back 2 days back get month name get day name format format the range is it today was it yesterday is it tomorrow start of the day bunch of methods bunch of utility methods what I want is you can take a look into this npm package install this npm package with all these commands that are given over here try it in your JavaScript or the typescript project give me a feedback this is an open source project you can always go ahead and click on this over here this repository will take you to the open source GitHub repository if you think that there is something to be added on top of it something to be modified ified you want to take part in this opensource project to make it even better you are most welcome and you can do that here there is a contributing guide that you can take a look and if you like this work go ahead and start this repository the link to this package in the description of this video please take a look and give it a try none of my course really end without giving you task and assignments actually that's how you learn better you know you have learned from me like how to utilize it use it I have told you about this npm package and everything but until unless you get down and try to tickle your brain to do something you don't learn. Well, so that's the reason under day 41 there is something called task.md file. Okay, I'm going to show you on GitHub where exactly it will be available. But look at this task. There are like close to 20 task already over there. So take your time and try to solve this. Everything that you have learned in part one and part two is going to help you to solve these problems. Please give it a try. Take your time. There is no hurry. All right. All the source code is in github.com/tapascript. Please feel free to follow this organization. Come to repositories. You will have 40 days of JavaScript. I have added this datetime initiative also under 40 days of JavaScript. In case you're not aware, this is a full-fledged course to teach JavaScript to learn JavaScript in much more depth with lot of task and assignments. So there is a day 41 where the part one video part one chapters are already there. Once I publish this part two video, you will be able to see the part two lesson related details, the source code and also the task.md for you to look into the task and do. You're always welcome to join Tapascript Discord. This is where we learn things together. If we do any kind of assignment, we post under task assignment stuff. You can see people are posting over here. developers help each other and whenever I have time I have bandwidth I definitely pitch in and try to help folks on JavaScript side of it react side of it you can see a lot of things kind of going on over here so feel free to join Tapascript discord as well next we'll get to part three part three something that I'm genuinely excited and interested about where we're going to talk about the temporal API what exactly it is it is something that was long overdue the problem that we faced with JavaScript date object those immutability problem etc. All are going to go away with the temporal API. With modern JavaScript, temporal API support is already there with the modern browsers. So you will be able to utilize and use many of those methods already. Let's see them together. Let's understand those things in part three. So that you will know handling date and time with legacy with the current state of things with Intel API and also with things that are upcoming in your JavaScript bucket. I hope you also equally excited for it. Stay tuned coming soon and don't forget to subscribe to Tapascript because that's where you get lot of awesome fundamental content. I was fixing a bug with my team and it was on date and time. The bug was a user's booking was showing up a day earlier for anyone in the time zone that is behind UTC. After some time me and my team traced the bug in a single line and the line was this. That's it. That is the one line was destroying the date for almost half of the planet because JavaScript's date object the new date with a date only string pares it as UTC midnight not as local time zone midnight. So if a user in New York open the app and what it should be 16th of June it renders at 15th of June. So we fixed it and we shipped it to production. Then 3 days later a new bug came in and that also was on date and time and it didn't stop there. It was keep coming. Then we slowly realize the problem is actually not in our code. It was rather in JavaScript's date object. In case you guys are not aware, JavaScript date object was written in just 10 days in 1995 and it was copied from Java's Java.util.class which was deprecated years later. But unfortunately in JavaScript we have carried the date object for last 30 years. But that ends now. In 2026, there is finally a proper replacement of it. And in the next 15 minutes, I'm going to show you exactly what it fixes, how it fixes, and why it is the number one replacement. That is a temporal API. Guys, let's go get started. Before you go anywhere, let me tell you clearly what is this video is and what it is not. This video is not the complete course on temporal. That's not possible in a single video and there is a separate series required which is coming. This video is more about an orientation. The orientation of mindset as a JavaScript developer we have used date and time in a different way with libraries without libraries. Now it's time that you fix your mindset properly so that hereafter whenever a situation comes to handle date and time you will always think about temporal. And I'll make sure that in next 14 minutes that happens. Let me now show you some of the actual problem with the date object. Here is a very innocent looking code, isn't it? You have a function called get next week which takes a date and then you increment the date by 7 days. It means if the date is 15th of November, after this the date is supposed to be 22nd of November. Okay. So now you are saying today is 15th of March 2026. A date that I have picked up. Then you pass this date today to get the next week. console.log log next week dot to date string I will get 22nd of March because today is 15th and we have incremented by 7 hence we got next week as 22nd. Fair enough. Now as a programmer my expectation is that today should not be changed because today is today and we are just incrementing it by 7 days to get the next week. But what happen if I do console.log of today string I see today is also changed to 22nd. today is gone. The function today this particular function today because in JavaScript date objects are mutable. Any method that changes them changes the original object in the memory. You always have to remember to clone the date object before you perform any operation to that. Now you know what nobody always remembers. People forget and that's where bugs get introduced. Every single JavaScript developer has been burned by this particular bug. JavaScript data object sell it as a feature but I say it's a bug. What month is this? When I create a date with a date object and I pass the month value as two. Any normal human being will think it is February because January is the first month and the two represent the February. But in JavaScript date object zero means January, one means February. This has been confusing developers for last 30 years. There is no good reason for it. It was a mistake in the language itself. So that's the second bug. Can you do this with JavaScript data object? No, you cannot do this because the JavaScript's data object only knows two time zones. One is UTC and other is your local missions time zone. That's it. It doesn't know any other time zone. So you cannot do a New York time passing a New York time zone. You cannot have a method called two time zone and then you pass another time zone. You cannot do all these things. you have to use the INTL API to display this or raw offset mathematical logic by yourself or reach out for another library which can do this. So this is third problematic area with the date object. And now is the worst part about all four of these bugs because they are silent. Date doesn't throw any errors whatsoever when all these things happen. It just quietly gives you the wrong data. And just imagine wrong date data in production can mean wrong invoices. It can mean wrong appointments. It can mean wrong compliance records. It can be devastating. Isn't it? Now this is another bug and this is related to DST. That is your daytime saving. So what happened when spring forward happened on March 8th say 2026 at 2:00 a.m. the clock jumps to 3:00 a.m. That's what we call as DST. So on that day clock jumps from 2:00 a.m. to 3:00 a.m. for DST. So literally 2:30 a.m. doesn't exist that day. Now this particular code is a valid JavaScript code. You can do 2:30 even for a time zone where DST is applied. JavaScript silently adjust it for you. You never know it happened. There is no error. There is no warning. Just wrong data in production. This is where the date object is absolutely absolutely wrong choice in a programming language like JavaScript. Now it's time to do the mind shift. The single biggest shift in temporal is this. Instead of one type that tries to do everything in case of date object, right? Temporal gives you seven specialized types. Each one designated for a specific kind of value. And that's amazing. To give an analogy, you can think it like this. The old data object is like a Swiss army knife, right? It technically has all the tools in it, but you can never get to the right one without cutting your finger. At the same time, when you think about the temporal, temporal is like a proper toolbox. Whatever you need, you can just pick up and get your work done. I hope this analogy makes sense. Let's go over each of these type one by one so that we can understand the differences. The first one is called temporal.plain date. You use that when you need a date on the calendar. No time only date the calendar date no time no time zone usually you use this for birthdays deadlines holidays right where you generally work with the dates you don't need any time you don't need any time zone then similar to plain date you have something called plain time a time of the day no date no time zone used for opening hours for a store alarms recurring times etc so I have given all this usage and this code is in the GitHub the link is in the description you can always go and read more about it later point in time. But now understand then you have temporal dot plain date time. Before we had date and time now we have plain date time. This is date and time combination but no time zone. Remember no time zone used for this kind of cases. Our standard meeting is every Monday at 10:00 a.m. Each office observes in their own time zone. Right? This particular meeting. So in this kind of use cases you will use temporal.plain date time. So far we have seen what is for date, what is for time, what is for date and time together but no time zone. Whenever you need date with time zone, you will use something called zoned temporal.zone date time. This is the most powerful one. You will use this when you need date time plus full time zone with DST awareness. Think about it how powerful it is in case of date object to deal with the date to deal with the time to deal with the time zone to deal with the DST doing all of them together is almost impossible is nightmare there will be bugs and you used to go to third party libraries multiple libraries to handle this now JavaScript provides you out of the box something called temporalzone date time to take care of this when you use that you use it for scheduling on the calendar that's what my 9:00 a.m. could be 8:00 p.m. somewhere else, right? Calendar app usercentric times. So, here is an example temporal.zone date time form. You pass the date and time and you also pass the time zone. This is something we could not do with the date object. We have seen it before. Then we have something called temporal.instant. This is an exact point on the timeline. No calendar, no time zone. When do you use it? Use it for logging. You're logging something into your logger, right? Or you need a time stamp. Then you have temporal.plain plain year month a month in a year used for usually in invoices for the billing cycles, credit card expiry and all those situation. Then the last one is temporal dot plain month day here month and day together but no year used for annual recurring dates like birthdays, anniversaries etc. So these are the seven different types that temporal provides so that we can use them based on the usage and the use cases. Now you notice something important over here the naming is also intentional plain wherever it comes as plain like here it means no time zone attached it means that you are working with a calendar representation no UTC math required no offset math required just the value that you need but whenever you see the zone that's the one that knows about the real world where you can pass the time zone name it handles your DST automatically and gives you the correct wall clock time anywhere where on the planet and the best part is all these dates are immutable. The first bug that we have seen in case of date object the date was mutable today was getting changed when we were computing the next week. In this case it's never ever going to happen. Here is an example temporal.plain date form this is a date that I picked up. This is the date you can call it as today also and then I'm doing on today or on this date just add seven more days. So this is the syntax that we need to master when you use temporal. So this will give me the next week's date. Now if I print my date two string is still remain 15th of March if you see here. But the next week string has got changed to 22nd. So it means this particular date is immutable. The dot add method on this date is not changing the date itself. It is giving me a new date value altogether. So here you don't have to remember to clone. No set date gotchas, no mutation bugs, just clean predictable immutable operations. Apart from this mutability and immutability, can we go through some dramatic differences? What if you want to get today's date? So you have new date and you want to get only today's date only the date part with the date object. You have to do this circus new date dot2 iso string. It will give you a big string of today's date and time. Then you will do a substring operation on that. Do a dot slice get the first few characters which will represent today's date which is 16th of June 2026 while I'm recording this video. But with temporal no such circus why do you want to do a string operation to get the date today's date. You just do temporal.now.plain date ISO. That's it. And then it gives you today's date. How simple is that? Have you done a comparison using old date object like this? I have 15th of March 2026 and again 15th of March 2026. Two dates and I want to know whether they are same date or not. I don't want to know whether they are same date object or not. What will happen over here? Of course, we know the object works with references. So, console.log D1 triple equals strict equality D2. If I do, it will give me false because these two are different objects. Of course, if I do a lose equality also, it is going to give me the false because two are different object. But how would I know if those two are pointing to the same date? For that workaround that date object gives me is that on D1 you have to do dot get time and on D2 also you have to do dot get time. This will give you the millisecond representation and then you equate them and then you figure it out whether they are the same time or not. This is a workaround. Why? Because on the date object you are calling another API to get certain representation of the date and then you are equating right. It is like holding your nose like this. But in temporal it is this simple. You have two dates again created with temporal.plain date because calendar date you just do t1 dot equals t2. That's it. And if I print this it will say it is true. How explicit it is. Have you compared two dates using javascript date object? How easy or difficult it was. If you have ever done you have written your own logic with temporal you just do this temporal.plain.compare pass t1 and t2 is going to return an integer. If it returns zero, it means those are equal. If it returns minus one, it means it's a before. If it returns one, it means after. So, how easy it is to compare two dates. Now, that's the last one that makes your user almost crazy that adding one day and getting wrong hour is a real production bug and this is related to DST. Temporal handles it by default using zone date time. It knows about DST. You don't have to think about it. You don't have to write any extra code. Here this code says on 7th of March at 12:00 a.m. in a time zone where DST is applied the DST starts. If I add one day temporal understand this is the DST date and it compensate the day accordingly. You don't have to do anything extra. Same thing if you have to do with the date object you'll be writing this big code to handle this. Now the question is can I use the temporal API today and write the code for production? My answer is yes. Now you will say if I go to can I use and search for temporal I see that it has a limited availability and the support for some of the browsers like Safari. Safari doesn't have any support itself. How can I use this? Don't worry about that. It has a great support on Chrome page Firefox and etc. You can start using it. Wherever there is a lack of support there is a polyfill for you. This is a polyfill. I have given the link in the description as well. And this polyfill is created and maintained by TC39 temporal champions. the same guys who build the spec for temporal. So it's not a workar around is a reference implementation. You can ship production code with this. Now my recommendation is start using temporal API from now on. Write your dead logic with temporal. Learn the types learn the mental model. With safari things won't work but you have the polyfill to work it out. Once safari start adding the support for temporal you can get rid of this polyfill. That's it. All the source code you have seen in this part or in the entire series is in my organization called github.com/tavascript. Please feel free to follow this organization because there are a lot of interesting projects and then you can go to this repositories. From there you can go to 40 days of JavaScript because date and time playlist is also part of 40 days of JavaScript. Over there if you scroll down you can go to day 41 and here you already have this INTL and the date API everything. Once I publish this video, you will be able to see temporal another folder over here where all temporal related code will be there. If you like my work, don't forget to start this repository. Thank you very much. So here is what we have done with this whole series. In the first part, we took care of understanding everything about date around date around time the basic concepts. Then in the second part, we also understand the intel APIs we understood the date objects etc. before that and in this part we took all insights the major insights about the temporal API now the natural progression is to create a different series on temporal I'll be doing that but for now I'm sure this will be enough for you to start writing correct production code where date and times come into picture if this was of any help to you please subscribe to tapascript I create content which are fundamentally right and which help you to get job faster thank you very much for watching. We'll be coming back again very soon.
Generated algorithmically for Search Engine
Indexing.