Skip to content
the poor coder

get date in milliseconds javascript

Poor Man
Published
Reading time
1 min read
On this page

Get Date in Milliseconds using JavaScript#

JavaScript provides a built-in Date object that allows you to work with dates and times. To get the current date in milliseconds, you can use the getTime() method attached to the Date object.

Method 1: Using getTime()#

Text

    // Get current date in milliseconds
    const currentDate = new Date().getTime();

    // Display date in console
    console.log(currentDate);

In the code snippet above, we create a new instance of the Date object and call the getTime() method to get the current date in milliseconds. We then store this value in the currentDate variable and display it in the console using console.log().

Method 2: Using Date.now()#

Text

    // Get current date in milliseconds
    const currentDate = Date.now();

    // Display date in console
    console.log(currentDate);

The second method to get the current date in milliseconds is by calling the static method now() on the Date object. This method returns the number of milliseconds since January 1, 1970, 00:00:00 UTC.

Both methods work similarly and return the same result. You can choose any method that suits your coding style and preference.

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