1 Star 0 Fork 0

沉冰浮水/水水的旧代码合集

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

JavaScript 时间对象

时间相关的笔记及函数

getDay() 方法

getDay() 方法返回指定日期是星期几(从 0 到 6)。

注释:星期日为 0,星期一为 1,依此类推。

const oDate = new Date();
console.log(oDate.getDay());

获取 2022-07-18 格式的日期

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 对象

// "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
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wdssmq/StaleCode.git
git@gitee.com:wdssmq/StaleCode.git
wdssmq
StaleCode
水水的旧代码合集
master

搜索帮助