Moment.js moment().unix() Function

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

Syntax:

moment().unix();

Parameters: This function has no parameter.

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

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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: 1595007379
    

Example 2: Filename: index.js

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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 in seconds: 1595007321
    

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

My Personal Notes arrow_drop_up

To obtain a position of responsibility that utilizes my skills and keen to work in an environment where I can enrich my knowledge

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.