4

The day before yesterday is tomorrow - Date and time explained

 2 years ago
source link: https://devm.io/javascript/javascript-date-development
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Column: Hitchhiking through the JavaScript jungle

The Day Before Yesterday Is Tomorrow - Date and Time Explained

31. Aug 2022


JavaScript has its good and bad sides. But one of the darkest chapters of the language standard - and the majority of JavaScript developers will agree with me - is the Date object. When it comes to dealing with dates or times, you can’t escape it. But why is the Date object so bad? Here’s the best example: Aug 15, 2021 is July 15, 2021 in the JavaScript world. Why? Because months in JavaScript start at 0, but only the months. This is just one of many examples. Want to see more? Have you ever tried to calculate with native date values? Or tried to format a time value? Out of the box, nothing works. You either have to implement the desired functionality yourself or fall back on one of the many libraries. So let's take a look at how things are currently and take a peek into the future.

The JavaScript language standard’s Date class provides only a handful of classes to help you with your work. Math or Date are examples of this. These constructs are available on all platforms and work the same everywhere. So, it doesn't matter if you're working with Safari, Firefox, Chrome, or Node.js. You can create a Date object using the constructor in a few different ways:

  • Without arguments: A Date object is created at the current time.
  • A number as argument: Milliseconds since 1.1.1970
  • String: A representation of a date corresponding to either an IETF RFC 2822 timestamp or ISO8601.
  • Multiple numerical values: Here you must specify at least a year and the month index; Optionally, you can also specify a day, hour, minute, second, and millisecond.

As you can see, in principle, you can pass almost anything to Date object and it will come out with a date. If you pass a string, you have to make sure that you use the correct format, otherwise, you will get an invalid Date object. If you pass numbers, then it will look different yet again. For example, a new Date(2020, 5, 32) creates 01.07.2020. The Date class provides you with a number of methods to access individual aspects of the date. For example, with getHours you receive the hours part of the date. You can change this information with setHours. In addition to a numerical value for hours, you can also pass minutes, seconds, and milliseconds with this method. setSeconds works similarly, allowing you to pass milliseconds and seconds. The methods for setting the year, month, and day work analogously, except that you can't set time values. Admittedly, the API of the class feels a bit strange. Once you get used to the fact that the Date class works with index values rather than months, it's not so bad. However, the class is not very practical for everyday use, and is missing some crucial features: A typical task is formatting the date and time values. For this, the Date class provides nothing. It is also missing the possibility of calculating with date and time or comparing these values.

Alternatives to the Date Class

We have no choice but to either solve these problems ourselves or look for a library. But why implement things when a better solution already exists? A quick look with your search engine of choice shows us a library called Moment. Beware, the top dog among JavaScript date libraries is no longer being developed and there are better alternatives. Moment has some problems that would require redevelopment, so the team decided to take a radical...


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK