xxxxxxxxxx
const today = new Date();
const year = today.getFullYear();
const month = String(today.getMonth() + 1).padStart(2, '0');
const day = String(today.getDate()).padStart(2, '0');
const currentDate = `${year}-${month}-${day}`;
console.log(currentDate);
xxxxxxxxxx
var today = new Date();
var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
//var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
var dateTime = date+' '+time;
The dateTime variable contains result as:
2018-8-3 //11:12:40
xxxxxxxxxx
var today = new Date();
var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
xxxxxxxxxx
let today = new Date().toISOString().slice(0, 10)
console.log(today)
xxxxxxxxxx
let today = new Date();
let month = today.getMonth() + 1;
month = month < 10 ? "0" + month : month;
let days = today.getDate() < 10 ? "0" + today.getDate() : today.getDate();
today = today.getFullYear() + "-" + month + "-" + days;
console.log(today);
xxxxxxxxxx
var d= new Date();
d.getFullYear();//Get the year as a four digit number (yyyy)
d.getMonth();//Get the month as a number (0-11)
d.getDate();//Get the day as a number (1-31)
d.getHours();//Get the hour (0-23)
d.getMinutes();//Get the minute (0-59)
d.getSeconds();//Get the second (0-59)
d.getMilliseconds();//Get the millisecond (0-999)
d.getTime();//Get the time (milliseconds since January 1, 1970)
xxxxxxxxxx
// Get current date
const currentDate = new Date();
// Format the date as per requirement
const options = { year: 'numeric', month: 'long', day: 'numeric' };
const formattedDate = currentDate.toLocaleDateString(undefined, options);
console.log(formattedDate);
xxxxxxxxxx
// Creating a new Date object
let today = new Date();
// Extracting the year, month, and day
let year = today.getFullYear();
let month = today.getMonth() + 1; // Month starts from 0
let day = today.getDate();
// Formatting the date in desired format (e.g., DD-MM-YYYY)
let formattedDate = `${day}-${month}-${year}`;
// Printing the formatted date
console.log(formattedDate);