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 |
//方法一: function nowTime() { var time = new Date(); var year = time.getFullYear(); //年 var month = time.getMonth() + 1; //月 var date = time.getDate(); //日 var hours = time.getHours(); //时 var minute = time.getMinutes(); //分 var second = time.getSeconds(); //秒 //年月日的拼接 if (month < 10) month = '0' + month; if (date < 10) date = '0' + date; var text = year + '-' + month + '-' + date + ' '; //时分秒拼接 if (hours < 10) hours = '0' + hours; if (minute < 10) minute = '0' + minute; if (second < 10) second = '0' + second; text += hours + ':' + minute + ':' + second; //返回全部时间 return (text); } console.log(nowTime()); //调用函数,再控制台显示时间 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
//方法二: function nowTime() { var time = new Date(); var year = time.getFullYear(); //年 var month = buling(time.getMonth() + 1); //月 var date = buling(time.getDate()); //日 var hours = buling(time.getHours()); //时 var minute = buling(time.getMinutes()); //分 var second = buling(time.getSeconds()); //秒 // var arr = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']; // var day = arr[time.getDay()];//因为西方人的习惯是周日第一,周一第二这样的排列 var temp = year + '-' + month + '-' + date + ' ' + hours + ':' + minute + ':' + second; return (temp); } function buling(temp) { if (temp < 10) temp = "0" + temp; return temp; } console.log(nowTime()); //调用函数,再控制台显示时间 |