JS寫一個函數輸出今天的日期

練習:輸出今天的日期,以YYYY-MM-DD的方式,比如今天是2015年8月11日,則輸出2015-08-11

var o = {
		
			/*
			*輸出今天的日期,以YYYY-MM-DD的方式,比如今天是2015年8月11日,則輸出2015-08-11
			*/
				outputDate:function(){
					var arrDate = [];
					var nowDate = new Date();
					var year = nowDate.getFullYear();//獲取年,返回2015
					var month = nowDate.getMonth() + 1;//返回月,0是1月
						month = month<10 ? '0'+month : month;
					var day = nowDate.getDate();//返回日,
						day = day<10 ? '0'+day : day;
					console.log(year+'-'+month + '-' +day);//或者利用下面的數組方法
					arrDate.push(year);
					arrDate.push(month);
					arrDate.push(day);
					arrDate = arrDate.join('-');
					console.log(arrDate);
				}
			};

			o.outputDate();


測試結果:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章