Node Js : Accepting Command Line Argument

Time zone Example :

node init
touch index.js
node install moment-timezone
node index.js Asia/Kolkata

index.js

const moment = require('moment-timezone');

moment.tz.setDefault('America/Los_Angeles');

let targetTimezone;

if(process.argv.length != 3){
    console.log("Usage: node <script-file> <timezone>")
    process.exit(1);
}else{
    targetTimezone=process.argv[2];
}

console.log(`Time zone at ${targetTimezone} timezone is ${moment().tz(targetTimezone).format()}`);

Leave a Comment