25

Node.js date-fns: Good date handling in JavaScript

 5 years ago
source link: https://www.tuicool.com/articles/3yamE3I
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
FFZ7Jvj.jpg!webmmMVbiY.jpg!web
Image from pexels

Working with dates is a problem of the first hour of the language. Although JavaScript basically has a Date object that you can theoretically use to perform date calculations, there are not many weaknesses in the API. Fortunately, there are helpful libraries like Node.js date-fns that can save us a lot of work.

For example, one problem is how to deal with different time zones in the date objects, so JavaScript uses the current time zone of the system as a basis. Especially with cross-time applications, this can lead to difficulties. Another peculiarity of the Date object in JavaScript is the representation of the month. For example, January is given the value 0. However, if the daily and year numbers are JavaScript again, then the 5th standard of a month is represented by the number 5.

When implementing an application that uses dates, you often encounter the task of creating, modifying, and outputting them. The creation and output is possible without any problems with the on-board tools of JavaScript. If, however, you want to modify a date, for example, if you want to subtract two days from a date, this is no longer possible without further ado. Of course, you can get the timestamp of the date and then subtract the appropriate number of milliseconds to get to the target date. However, this approach is not easy to read and easy to maintain or even very elegant. For this reason, and for a number of other reasons, many libraries have evolved in the past to help you use date values ​​in JavaScript. One of the most widely used solutions in the market is Moment.js. However, the top dog got a serious competitor some time ago: the project date-fns.

How is Node.js date-fns different from Moment.js?

The first and one of the most important differences is already in the name of the project, because fns stands for Functions. Node.js date-fns is a collection of functions that you can use to work with dates. In contrast, Moment.js takes an object-oriented approach. Here you create a moment instance and work with the methods of this object. This of course affects the package size. By default, Moment.js includes the entire interface. Although you can optimize the package, additional steps are required. With date-fns you only load the functions that you really need. In a backend application with Node.js this consideration does not play too much, as the package size is rather minor. You can also use date-fns as well as Moment.js in the frontend in your browser. Here the package size is crucial.

The developers of Node.js date-fns have not only paid attention to subdividing the project into many small and largely independent functions, but also that the functions are pure functions. For example, you pass the addHours function a date object and the number of hours to add. The result is a new date object, with the specified number of hours later than when typing. So there are no side effects, such as the direct modification of the input on.

How is Node.js date-fns installed?

date-fns, like most other JavaScript libraries, is available as an npm package and can be installed as such through npm. Use the command npm install date-fns in your project. The package is automatically entered as a dependency in your package.json file. Similarly, you can also use yarn with the yarn add date-fns command .

How is it used?

You can use the date-fns package with both the CommonJS module system and ES modules. In the following example, use the format function to print the current date. Listing 1 shows you how to work with the CommonJS module system.

<strong>const { format } = require('date-fns');</strong>
<strong>const date = new Date();</strong>
<strong>console.log(`Today is: ${format(date, 'DD.MM.YYYY')}`);</strong>

Newer versions of Node.js now also support the import and export keywords to import or export modules. At this point you can either import the entire date-fns package and access the required functions, or you can take advantage of the fact that each function is available in a separate file, so you can import the format function individually. How this works can be seen in example 2.

<strong>import { format } from 'date-fns/format';</strong>
<strong>const date = new Date();</strong>
<strong>console.log(`Today is: ${format(date, 'DD.MM.YYYY')}`);</strong>

For more information about formatting of dates, comparison and other operations you can read the full post here . Thanks for reading!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK