js时间格式化

时间格式化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>

</head>
<body>

<script>
function time(){


var date=new Date();
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 h=date.getHours();
if(h<10)h="0"+h;
var i=date.getMinutes();
if(i<10)i="0"+i;
var s=date.getSeconds();
if(s<10)s="0"+s;
var mmm = y+"-"+m+"-"+d+" "+h+":"+i+":"+s;
console.log(mmm);
};
time();
</script>


</body>
</html>