JavaScript 日期格式

javascriptfront end technologyobject oriented programming

要在 JavaScript 中实现日期格式,代码如下 −

示例

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
body {
   font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}
.sample {
   font-size: 18px;
   font-weight: 500;
}
</style>
</head>
<body>
<h1>Date Format</h1>
<div class="sample"></div>
<button class="btn">点击此处</button>
<h3>点击上面的按钮可以查看不同的日期格式</h3>
<script>
let fillEle = document.querySelector(".sample");
let date = [];
date.push(new Date('2020-01-20'));
date.push(new Date('2020-02'));
date.push(new Date('2020'));
date.push(new Date("2015-03-25T12:00:00Z"));
fillEle.innerHTML = date + '<br>';
document.querySelector(".Btn").addEventListener("click", () => {
   date.forEach(item=>{
      fillEle.innerHTML += item + '<br>'
    })
});
</script>
</body>
</html>

输出

点击“CLICK HERE”按钮 −


相关文章