Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Moment.js moment().valueOf() Function

  • Last Updated : 29 Jul, 2020

The moment().valueOf() function is used to get the number of milliseconds since the Unix Epoch. Basically the Unix time is a system for describing a point in time.

Syntax:

moment().valueOf();

Parameters: This function has no parameter.

Return Value: This function returns the Unix Timestamp in milliseconds.

Installation of moment module:



  1. You can visit the link to Install moment module. You can install this package by using this command.
    npm install moment
  2. After installing the moment module, you can check your moment version in command prompt using the command.
    npm version moment
  3. After that, you can just create a folder and add a file for example, index.js as shown below.

Example 1: Filename: index.js




// Requiring the module
const moment = require('moment');
  
// Function call
var result = moment().valueOf();
  
console.log("Result:", result)

Steps to run the program:

  1. The project structure will look like this:
  2. Run index.js file using below command:
    node index.js

    Output:

    Result: 1595006948726
    

Example 2: Filename: index.js




// Requiring the module
const moment = require('moment');
   
function getUnixTimeStamp() {
   return moment().valueOf();
}
   
// Function call
var result = getUnixTimeStamp();
console.log("Unix Timestamp:", result)

Steps to run the program:

  1. The project structure will look like this:
  2. Run index.js file using below command:
    node index.js

    Output:

    Unix Timestamp: 1595007021164
    

Reference: https://momentjs.com/docs/#/displaying/unix-timestamp-milliseconds/

My Personal Notes arrow_drop_up
Recommended Articles
Page :