function getCurrentTime(format = "yyyy-MM-dd hh:mm:ss") {
const now = new Date();
const year = now.getFullYear();
const month = ("" + (now.getMonth() + 1)).padStart(2, "0");
const day = ("" + now.getDate()).padStart(2, "0");
const hours = ("" + now.getHours()).padStart(2, "0");
const minutes = ("" + now.getMinutes()).padStart(2, "0");
const seconds = ("" + now.getSeconds()).padStart(2, "0");
return format
.replace("yyyy", year + "")
.replace("MM", month)
.replace("dd", day)
.replace("hh", hours)
.replace("mm", minutes)
.replace("ss", seconds);
};
console.log("getCurrentTime() :>> ", getCurrentTime("yyyy-MM")); // 2023-08
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论