new Date() constructor. This will give you the current date and time.
// Create a new Date object
let currentDate = new Date();
// Log the current date and time
currentDate object will contain the current date and time in your local time zone.
// Get the current year
let currentYear = currentDate.getFullYear();
// Log the current year
getFullYear() method will return the year as a four-digit number.
Similar to the
getFullYear() method, you can use the
getMonth() method returns the month as a zero-based index, where 0 represents January and 11 represents December.
// Get the current month
let currentMonth = currentDate.getMonth();
// Log the current month
To get the month as a string, you can create an array of month names and use the
currentMonth as an index to retrieve the month name.
// Array of month names
const monthNames = [
"January", "February", "March",
"April", "May", "June", "July",
"August", "September", "October",
// Get the current month name
let currentMonthName = monthNames[currentDate.getMonth()];
// Log the current month name
currentMonthName variable will contain the name of the current month.
getDate() method. The
getDate() method returns the day of the month as a number between 1 and 31.
// Get the current day
let currentDay = currentDate.getDate();
// Log the current day
currentDay variable will contain the current day of the month as a number.
- 2023-07-28js 去除最后一个字符(JS去除最后一位字符。)