xxxxxxxxxx
const d = new Date();
let month = d.getMonth(); // 5
const month = ["January","February","March","April","May","June","July","August","September","October","November","December"];
const d = new Date();
let name = month[d.getMonth()]; // June
xxxxxxxxxx
Formating method if custom type is needed:
dateFormat(date: Date) {
var dateToFormat = new Date(date);
let day = dateToFormat.getDate();
let month = dateToFormat.getMonth() + 1;
let year = dateToFormat.getFullYear();
var dateFormatted = year + '/' + this.zeroPad(month) + '/' + this.zeroPad(day);
return dateFormatted;
}
//Adding a Zero to the single digit months and days
zeroPad(number: number) {
return ('0' + number).slice(-2);
}
let currentDate = new Date();
var dateString = this.dateFormat(currentDate) //2022/10/11
xxxxxxxxxx
// Create a new Date object
const date = new Date();
// Get the month (0 - January, 1 - February, etc.)
const month = date.getMonth();
// Output the month
console.log(month);