标签:title char var date ons function script date() nbsp
1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4 <meta charset="UTF-8"> 5 <title></title> 6 </head> 7 <body> 8 <script> 9 function format(date){ 10 //定义数组week:[日,一,二,三,四,五,六] 11 var week=["日","一","二","三","四","五","六"]; 12 //获得date中的年y 13 var y=date.getFullYear(); 14 //获得date中的月M,再+1 15 var M=date.getMonth()+1; 16 //如果M<10,就改为0+M; 17 M<10&&(M="0"+M); 18 //获得日d 19 var d=date.getDay(); 20 //如果d<10,就该位0+d 21 d<10&&(d="0"+d); 22 //获得星期day 23 var day=date.getDay(); 24 //获得week中的day位置的汉字保存回day中 25 day=week[day]; 26 //获得小时h 27 var h=date.getHours() 28 //定义变量am,赋值为: 29 var am=h<12?"上午":"下午"; 30 //如果h<12,就赋值为上午,否则赋值为下午 31 //将h-12 32 h-=12; 33 //如果h<10,就改为0+h 34 h<10&&(h="0"+h); 35 //获得分钟m 36 var m=date.getMinutes(); 37 //获得分钟m<10,就改为0+m 38 m<10&&(m="0"+m); 39 return y+"年"+M+"月"+d+"日 星期"+day+" "+am+h+":"+m; 40 //2016年09月01日星期四上午11:57 41 } 42 console.log(format(new Date())); 43 </script> 44 </body> 45 </html>
标签:title char var date ons function script date() nbsp
原文地址:http://www.cnblogs.com/longly/p/6139824.html