javascript 格式化 Date api
1.1 小数点后位数
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>summerlex@126.com</title>
</head>
<body>
<h1>008.00</h1>
<script>
let i = 8
//输出小数点前后总共5位
document.write(i.toPrecision(5))
//换行
document.write('<br/>')
//保留小数点后两位
document.write(i.toFixed(2))
document.write('<br/>')
document.write(i.toFixed(2).toString().padStart(6, '0'))
</script>
</body>
</html>
1.2 Date api
getDay() #返回星期几(0-6,0代表星期天)
getFullYear() #返回年份
getMonth() #返回月份(0-11)
getDate() #返回月中的第几天(从1到31)
getHours() #返回小时(0-23)
getMinutes() #返回分钟(0-59)
getSeconds() #返回秒数(0-59)
getMilliseconds() #返回毫秒数(0-999)
getTime() #返回自 1970 年 1 月 1 日午夜以来与指定日期的毫秒数。
详细见:
https://www.w3school.com.cn/jsref/jsref_obj_date.asp
1.3 格式化
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>summerlex@126.com</title>
</head>
<body>
<h1>Hello, world!</h1>
<script>
let d = new Date()
document.write(d.toLocaleDateString())
document.write('<br/>')
document.write(d.toLocaleTimeString())
document.write('<br/>')
document.write(`${d}<br>`)
document.write(d.toLocaleString())
let yy = d.getFullYear()
let mm = d.getMonth()+1
let da = d.getDate()
let hh = d.getHours()
let mi = d.getMinutes()
let ss = d.getSeconds()
let xq = d.getDay()
document.write(`${yy}年${mm}月${da}日 ${hh}:${mi}:${ss} 星期${'日一二三四五六'.charAt(d.getDay())}`)
</script>
</body>
</html>