Open In App

Moment.js moment().unix() Function

Last Updated : 08 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The function moment().unix() retrieves the count of seconds since the Unix Epoch, which is a reference point for denoting a specific moment in time. Unix time serves as a system for timestamping.

Syntax:

moment().unix();

Parameters: This function has no parameters. 

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

The moment().unix() function converts dates into Unix timestamps in Moment.js.

Steps to create the Express App and Installing the Modules:

Step 1: Initializing the Nodejs app using the below command:

npm init -y

Step 2: Installing the express module:

npm install moment

Project Structure:

NodeProj

The updated dependencies in package.json file will look like:

"dependencies": {
"moment": "^2.30.1",

Example 1: Below is the code for the moment().unix():

JavaScript
// Requiring the module
const moment = require('moment');

// Function call
let result = moment().unix();

console.log("Result:", result)

Steps to run the program:

node index.js

Output:

Result: 1595007379

Example 2: Below is the code for the moment().unix():

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

Steps to run the program:

node index.js

Output:

Unix Timestamp in seconds: 1595007321



Next Article

Similar Reads