JavaScript获取默认显示当日日期
2021-04-12 本文已影响0人
祈澈菇凉
1:时间格式为
2021/4/2
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
<title></title>
</head>
<body>
<input type="text" name="alertDateQuery" id="aDate">
</body>
<script type="text/javascript">
var mydateInput = document.getElementById("aDate");
var date = new Date();
var dateString = date.getFullYear() + "/" + (date.getMonth() + 1) + "/" + date.getDate();
mydateInput.value = dateString;
</script>
</html>
2:时间格式为
yyyy-MM-dd
2021-04-01
var mydateInput = document.getElementById("beginDate1");
var date = new Date();
// var dateString = date.getFullYear() + "/" + (date.getMonth() + 1) + "/" + date.getDate();
//下面语句判断,当月份小于10月时,前面位0
var Y = date.getFullYear();
var M = date.getMonth() + 1;
if( M < 10){
M = '0' + M;
}
var D = date.getDate();
if( D < 10){
D = '0' + D;
}
var dateString = Y + "-" + M + "-" + D;
mydateInput.value = dateString;