【js】获取当前时间
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script> /** * type是中间分隔符,默认type为“-” * 时间精确到分钟 * getTime(“-”);得到的结果是2020-05-13 00:00; * getTime(“/”);得到的结果是2020/05/13 00:00 */ function getTime(type){ if(type ==null || type=="" || type==undefined){ type="-"; } var date = new Date(); var time=""; time =date.getFullYear()+type+(date.getMonth()+1).toString().padStart(2, "0")+type+date.getDate().toString().padStart(2, "0")+" "+date.getHours().toString().padStart(2, "0")+":"+date.getMinutes().toString().padStart(2, "0"); alert(time); return time } </script> </head> <body> <button type="button" onclick="getTime()">显示日期</button> </body> </html>
附带测试地址及截图
https://www.runoob.com/try/try.php?filename=tryjs_events