dayjs now

Dayjs now

As someone who works with dates and times frequently, I often need to know the current date and time. This is where the now() function in Dayjs comes in handy.

The now() function returns the current date and time in the local time zone. It is similar to the Date.now() function in JavaScript, but with some additional features.

Syntax

The syntax for using the now() function is:


dayjs().now();

The () after dayjs is used to create a new instance of Dayjs with the current date and time. The now() function is then called on this instance to return the current date and time.

Example

Here is an example of how to use the now() function:


const now = dayjs().now();
console.log(now);

This will output the current date and time in the console in the following format:


2022-01-01T00:00:00+08:00

If you want to format the output differently, you can pass a format string to the format function:


const now = dayjs().now().format('YYYY-MM-DD HH:mm:ss');
console.log(now);

This will output the current date and time in the console in the following format:


2022-01-01 00:00:00

Another way to get the current date and time in Dayjs is to use the dayjs() function without any arguments:


const now = dayjs();
console.log(now);

This will also return the current date and time in the local time zone.

Conclusion

The now() function in Dayjs is a simple way to get the current date and time in the local time zone. It can be used in combination with other Dayjs functions to perform various date and time calculations and operations.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe