其他分享
首页 > 其他分享> > 获取当前时间,不足补0

获取当前时间,不足补0

作者:互联网

项目需求

word压缩成zip时,存放的路径格式为:url+当前时间+.zip
eg:var outpath = ‘/repository/system/fileservice/management/send_client_template/’+currentTime+".zip";
在这里插入图片描述
一、方法一

		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
		};	
weixin_38673922 发布了1 篇原创文章 · 获赞 0 · 访问量 19 私信 关注

标签:&&,function,date,不足,month,获取,toString,当前,var
来源: https://blog.csdn.net/weixin_38673922/article/details/104009992