获取当前时间,不足补0

项目需求

word压缩成zip时,存放的路径格式为:url+当前时间+.zip
eg:var outpath = ‘/repository/system/fileservice/management/send_client_template/’+currentTime+".zip";
获取当前时间,不足补0_第1张图片
一、方法一

		function getNowFormatDate1() {
		     var date = new Date();
		     var year = date.getFullYear();
		     var month = date.getMonth() + 1;				//月
		     var dateDate = date.getDate();					//日
		     var dateHours = date.getHours();               //时
		     var dateMinutes = date.getMinutes();           //分
		     var dateSeconds = date.getSeconds();           //秒
		     if (month >= 1 && month <= 9)  month = "0" + month;
		     if (dateDate >= 0 && dateDate <= 9)  dateDate = "0" + dateDate;
		     if(dateHours >= 0 && dateHours <= 9) dateHours="0" + dateHours;
		     if(dateMinutes >= 0 && dateMinutes <= 9) dateMinutes="0" + dateMinutes;
		     if(dateSeconds >= 0 && dateSeconds <= 9) dateSeconds="0" + dateSeconds;
		     var currentdate = year + month + dateDate + dateHours + dateMinutes + dateSeconds;
		     return currentdate;
		};

二、方法二

		function getNowFormatDate2() {
		     var date = new Date();
		     var year = date.getFullYear().toString();
		     var month = (date.getMonth() + 1).toString();
		     var dateDate = date.getDate().toString();
		     var dateHours = date.getHours().toString();               //时
		     var dateMinutes = date.getMinutes().toString();           //分
		     var dateSeconds = date.getSeconds().toString();           //秒
			 function timeAdd0(m) {
			    if(m.length<=1){
			        m='0'+m;
			    }
			    return m
			 } 
			year=timeAdd0(year)
			month=timeAdd0(month)
			dateDate=timeAdd0(dateDate)
			dateHours=timeAdd0(dateHours)
			dateMinutes=timeAdd0(dateMinutes)
			dateSeconds=timeAdd0(dateSeconds)			
			console.log(year+"-"+month +"-"+dateDate +" "+dateHours+":"+dateMinutes+":"+dateSeconds)	//2020-01-16 18:58:21
		};	

你可能感兴趣的:(获取当前时间,不足补0)