封装日期函数
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> </style> </head> <body> </body> <script> // 封装函数返回当前的时分秒:00:00:00 function getTime(){ var time=new Date(); var h=time.getHours(); //小时 h=h<10?'0'+h:h; //如果返回的时间小于10,则在前面加个0,否则原样输出 var m=time.getMinutes(); //分钟 m=m<10?'0'+m:m; var s=time.getSeconds(); //秒数 s=s<10?'0'+s:s; return h+':'+m+':'+s; } console.log(getTime()); </script> </html>