Trivial Facts About JavaScript Date


Hello and welcome to another episode of Continuous Improvement, the podcast where we dive into all things programming and web development. I’m your host, Victor, and today we have some interesting facts about dates in JavaScript.

Fact number one: Did you know that in JavaScript, the integer value representing the month actually begins with 0 for January and goes up to 11 for December? It’s a common mistake to think that January is represented by 1 and December by 12, but in reality, it starts from 0. So, if you’re working with dates in JavaScript, keep that in mind to avoid any confusion.

Let’s look at an example. If today’s date is April 29, 2016, instead of writing var date = new Date(2016, 4, 29, 11, 10, 30), you should actually write var date = new Date(2016, 3, 29, 11, 10, 30). The month is zero-based, so April is represented by 3 instead of 4. It’s a small detail, but an important one to remember.

Now, let’s move on to fact number two. In JavaScript, the date is based on a time value expressed in milliseconds, while the UNIX timestamp is expressed in seconds since 00:00:00 UTC on January 1, 1970. This means that if you want to convert a UNIX timestamp into a JavaScript date, you need to multiply the timestamp by 1000.

For instance, if you have a UNIX timestamp and want to represent it as a JavaScript date, you can use the following syntax: var date = new Date(UNIX_Timestamp * 1000). This multiplication by 1000 converts the seconds into milliseconds, which is the required format for JavaScript dates.

And there you have it, two fascinating facts about dates in JavaScript. Remember to always keep the month’s zero-based indexing in mind and don’t forget to multiply UNIX timestamps by 1000 when converting them to JavaScript dates.

Thank you for listening to this episode of Continuous Improvement. If you enjoyed today’s content, make sure to subscribe to our podcast for more programming tips and tricks. Stay curious and keep improving!