What date is it?
No, really! Have you ever asked yourself, whether JavaScript handles time, dates and time zones?
It turns out that it does. And it does really well!
Check out some of the scripts that I coded today to play around with the Dates in JavaScript!
For example, JavaScript can easily tell you the correct day of the week for every date in the past or in the future.
Try this function at JSFiddle and pass it any day you like to test it:
Or JavaScript can count time for you. Let’s say you’d like to know the exact amount of time that has passed since the start of the current day.
Check out this function at JSFiddle, or below:
JavaScript even has an advanced logic to auto-replace the Dates in case you made a mistake. We can use this mechanism too.
For example, you can always call the first day in any month simply by assigning ‘1’ to the Date. Now, the question is, how can you call the last day of a month, bearing in mind that different months have different numbers of days?!
Here’s a function that can find out the last day in any month of any year. Here, I intentionally ‘make a mistake’ and assigned the Date to the 0th day of the next month. However, smart JavaScript knows that there is no 0th day in any month, so it corrects it and assigns the Date to the previous calendar day, which is the last day of the current month!
Check it out at JSFiddle, or by clicking ‘Result’ below: