js获取当前时间(年月日时分秒)跟时间戳
程序员文章站
2022-05-02 15:13:55
...
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<div>当前日期:<span id="date"></span></div>
<div>当前日期的时间戳:<span id="timestamp"></span></div>
<script src="jquery-3.3.1.min.js"></script>
<script>
$('#date').text(getNowDate());
$('#timestamp').text(getTimestamp());
function getNowDate() {
var myDate = new Date;
var year = myDate.getFullYear(); //获取当前年
var mon = myDate.getMonth() + 1; //获取当前月
var date = myDate.getDate(); //获取当前日
var hours = myDate.getHours(); //获取当前小时
var minutes = myDate.getMinutes(); //获取当前分钟
var seconds = myDate.getSeconds(); //获取当前秒
var now = year + "-" + mon + "-" + date + " " + hours + ":" + minutes + ":" + seconds;
return now;
}
function getTimestamp() {
return new Date(getNowDate()).getTime();
}
</script>
</body>
</html>
运行效果: