var date_ob = new Date();
var day = ("0" + date_ob.getDate()).slice(-2);
var month = ("0" + (date_ob.getMonth() + 1)).slice(-2);
var year = date_ob.getFullYear();
var date = year + "-" + month + "-" + day;
console.log(date);
var hours = date_ob.getHours();
var minutes = date_ob.getMinutes();
var seconds = date_ob.getSeconds();
var dateTime = year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
console.log(dateTime);
Get Current Date and time in node js
var dateTime = require('node-datetime');
var dt = dateTime.create();
var formatted = dt.format('Y-m-d H:M:S');
console.log(formatted);
Timezone in Node.js
The toLocaleString() method of the Date() object can be used to get date and time for a given timezone name in Nodejs
File Name :
// Date object initialized as per New Zealand timezone. Returns a datetime string
let nz_date_string = new Date().toLocaleString("en-US", { timeZone: "Asia/Calcutta" });
// Date object initialized from the above datetime string
let date_nz = new Date(nz_date_string);
// year as (YYYY) format
let year = date_nz.getFullYear();
// month as (MM) format
let month = ("0" + (date_nz.getMonth() + 1)).slice(-2);
// date as (DD) format
let date = ("0" + date_nz.getDate()).slice(-2);
// hours as (HH) format
let hours = ("0" + date_nz.getHours()).slice(-2);
// minutes as (mm) format
let minutes = ("0" + date_nz.getMinutes()).slice(-2);
// seconds as (ss) format
let seconds = ("0" + date_nz.getSeconds()).slice(-2);
// date as YYYY-MM-DD format
let date_yyyy_mm_dd = year + "-" + month + "-" + date;
console.log("Date in YYYY-MM-DD format: " + date_yyyy_mm_dd);
// time as hh:mm:ss format
let time_hh_mm_ss = hours + ":" + minutes + ":" + seconds;
console.log("Time in hh:mm:ss format: " + time_hh_mm_ss);
// date and time as YYYY-MM-DD hh:mm:ss format
let date_time = year + "-" + month + "-" + date + " " + hours + ":" + minutes + ":" + seconds;
console.log("Date and Time in YYYY-MM-DD hh:mm:ss format: " + date_time);
File Name :
let date_ob = new Date();
File Name :
getDate : returns the day of the month (1-31)
getMonth : returns the month as an integer (0-11). Note that January is represented as 0 and December as 11.
getFullYear : returns the year in 4-digit format
getHours: returns the hour of the day in 24-hour format (0-23)
getMinutes: returns the minute (0-59)
getSeconds: returns the seconds (0-59)
File Name :
let date_ob = new Date();
// current date
// adjust 0 before single digit date
let date = ("0" + date_ob.getDate()).slice(-2);
// current month
let month = ("0" + (date_ob.getMonth() + 1)).slice(-2);
// current year
let year = date_ob.getFullYear();
// current hours
let hours = date_ob.getHours();
// current minutes
let minutes = date_ob.getMinutes();
// current seconds
let seconds = date_ob.getSeconds();
// prints date in YYYY-MM-DD format
console.log(year + "-" + month + "-" + date);
// prints date & time in YYYY-MM-DD HH:MM:SS format
console.log(year + "-" + month + "-" + date + " " + hours + ":" + minutes + ":" + seconds);
// prints time in HH:MM format
console.log(hours + ":" + minutes);
Getting Current Timestamp
File Name :
let ts = Date.now();
// timestamp in milliseconds
console.log(ts);
// timestamp in seconds
console.log(Math.floor(ts/1000));