一、date
如何使用、如何获取年月日时分秒、时间戳、如何获取指定日期的时间戳或周几
1..Date 对象用于处理日期和时间。
创建 Date 对象的语法:
var myDate=new Date()
获取年月日时分秒:
// 格式化日对象
const getNowDate = () => {let date = new Date();let sign2 = ":";let year = date.getFullYear() // 年let month = date.getMonth() + 1; // 月let day = date.getDate(); // 日let hour = date.getHours(); // 时let minutes = date.getMinutes(); // 分let seconds = date.getSeconds() //秒let weekArr = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期天'];let week = weekArr[date.getDay()];// 给一位数的数据前面加 “0”if (month >= 1 && month <= 9) {month = "0" + month;}if (day >= 0 && day <= 9) {day = "0" + day;}if (hour >= 0 && hour <= 9) {hour = "0" + hour;}if (minutes >= 0 && minutes <= 9) {minutes = "0" + minutes;}if (seconds >= 0 && seconds <= 9) {seconds = "0" + seconds;}return year + "-" + month + "-" + day + " " + hour + sign2 + minutes + sign2 + seconds;
}
console.log("当前日期"+getNowDate());
获取时间戳:
new date.getTime()
获取指定日期的星期:
// 获取指定日期的星期
let nowDate = new Date("2024-4-13")
let today = nowDate.getDay()
let weekDays = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
console.log(weekDays[today]);//星期六
获取指定日期的时间戳:
let dates = new Date
console.log(dates.getTime());