js获取时间
这里我就不进行封装了,有个js库可以快速使用,moment.js,如下:
html页面使用引入如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> </body> </html> <script src="https://cdn.bootcss.com/moment.js/2.9.0/moment.min.js"></script> <script> console.log(moment().format('YYYY-MM-DD')); // 获取年月日 console.log(moment().format('HH:mm:ss')); // 获取时分秒 console.log(moment().format('DDDD')); // 获取今天是一年的第多少天 console.log(moment().hour()); // 获取小时 console.log(moment().date()); // 获取今天是当前月份的几号 console.log(moment().day()); // 获取今天是周几 console.log(moment().week()); // 获取今天是当前年份的第多少周 console.log(moment().month()+1); // 获取当前月份 console.log(moment().quarter()); // 获取当前季度 console.log(moment().year()); // 获取年份 console.log(moment().get('year')); console.log(moment().get('month')+1); console.log(moment().get('date')); console.log(moment().get('hour')); console.log(moment().get('minute')); console.log(moment().get('second')); console.log(moment().get('millisecond')); </script>
nodejs项目使用如下:
// npm install moment -S const moment = require('moment'); console.log(moment().format('YYYY-MM-DD')); // 获取年月日

浙公网安备 33010602011771号