代码拉取完成,页面将自动刷新
时间相关的笔记及函数
getDay()
方法返回指定日期是星期几(从 0 到 6)。
注释:星期日为 0,星期一为 1,依此类推。
const oDate = new Date();
console.log(oDate.getDay());
const curDate = new Date();
const fnDateToStr = (date = curDate) => {
const options = { year: 'numeric', month: '2-digit', day: '2-digit' };
return date.toLocaleDateString("zh-CN", options).replace(/\//g, "-");
}
console.log(fnDateToStr());
// "2021-12-06" 转 Date 对象
function fnStrToDate(str) {
const arr = str.split("-");
const date = new Date(arr[0], arr[1] - 1, arr[2]);
return date;
}
setMonth()
const testDate = fnStrToDate("2022-10-31");
// 设置为 9 月
testDate.setMonth(9 - 1);
// 9 月并没有 31 号,所以会自动变成 10 月 1 号
console.log(fnDateToStr(testDate));
// 2022-10-01
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。