Moment.js moment().unix() Function
Last Updated :
18 Jan, 2024
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
const moment = require( 'moment' );
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
const moment = require( 'moment' );
function getUnixTimeStamp() {
return moment().unix();
}
let result = getUnixTimeStamp();
console.log( "Unix Timestamp in seconds:" , result)
|
Steps to run the program:
node index.js
Output:
Unix Timestamp in seconds: 1595007321
Share your thoughts in the comments
Please Login to comment...