Skip to content
the poor coder

dayjs now

Poor Man
Published
Reading time
1 min read
On this page

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:

JavaScript

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:

JavaScript

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

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

JavaScript

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:

JavaScript

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:

JavaScript

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:

JavaScript

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.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC