Moment.js moment().unix() Function
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.
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:
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