微信小程序实现日期格式化和倒计时
本文实例为大家分享了微信小程序实现日期格式化和倒计时的具体代码,供大家参考,具体内容如下
首先看看日期怎么格式化
第一种:
Date.prototype.Format=function(fmt){//author:meizz
varo={
"M+":this.getMonth()+1,//月份
"d+":this.getDate(),//日
"h+":this.getHours(),//小时
"m+":this.getMinutes(),//分
"s+":this.getSeconds(),//秒
"q+":Math.floor((this.getMonth()+3)/3),//季度
"S":this.getMilliseconds()//毫秒
};
if(/(y+)/.test(fmt))fmt=fmt.replace(RegExp.$1,(this.getFullYear()+"").substr(4-RegExp.$1.length));
for(varkino)
if(newRegExp("("+k+")").test(fmt))fmt=fmt.replace(RegExp.$1,(RegExp.$1.length==1)?(o[k]):(("00"+o[k]).substr((""+o[k]).length)));
returnfmt;
}
然后是调用this.value1=newDate().Format("yyyy-MM-ddHH:MM:SS")
第二种
1.中国标准时间格式化:
formatDateTime:function(theDate){
var_hour=theDate.getHours();
var_minute=theDate.getMinutes();
var_second=theDate.getSeconds();
var_year=theDate.getFullYear()
var_month=theDate.getMonth();
var_date=theDate.getDate();
if(_hour<10){_hour="0"+_hour}
if(_minute<10){_minute="0"+_minute}
if(_second<10){_second="0"+_second}
_month=_month+1
if(_month<10){_month="0"+_month;}
if(_date<10){_date="0"+_date}
vartime=_year+"-"+_month+"-"+_date+""+_hour+":"+_minute+":"+
_second;
//vartime=newDate();
//varformatTime=formatDateTime(time);
//返回结果:
//TueJun06201715:31:09GMT+0800(中国标准时间)
//2017-06-0615:31:09
//clock为在data中定义的空变量,存放转化好的日期
this.setData({
clock:time
})
},
2、把格式化时间转换为毫秒数
varformatTimeS=newDate('2017-06-0615:31:09').getTime();
返回结果:1496734269900
3、把毫秒数转换为标准时间
varformatTimeS=newDate(1496734269900);
返回结果:TueJun06201715:31:09GMT+0800(中国标准时间)
二、实现倒计时
//倒计时:其中time_canshu为传入的毫秒数
date_format:function(time_canshu){
//letformatTime1=newDate().getTime();
//letformatTime2=newDate('2018-04-2415:31:09').getTime();
//letformatTimeS=newDate(formatTime2-formatTime1);
varnone='00:00:00';
if(formatTimeS<=0){
this.setData({
clock:none
})}else{
//秒数
letsecond=Math.floor(time_canshu/1000);
//小时位
lethr=Math.floor(second/3600);
//分钟位
letmin=Math.floor((second-hr*3600)/60);
//秒位
letsec=second%60;//equalto=>varsec=second%60;
if(hr<=9)hr='0'+hr;
if(min<=9)min='0'+min;
if(sec<=9)sec='0'+sec;
lettime=hr+":"+min+":"+sec+"";
this.setData({
clock:time
})
}
},
时间戳转化为日期格式函数
//时间戳转化为日期格式
functiontimestampToTime(timestamp){
vardate=newDate(timestamp*1000);//时间戳为10位需*1000,时间戳为13位的话不需乘1000
varY=date.getFullYear()+'-';
varM=(date.getMonth()+1<10?'0'+(date.getMonth()+1):date.getMonth()+1)+'-';
varD=date.getDate()+'';
varh=date.getHours()+':';
varm=date.getMinutes()+':';
vars=date.getSeconds();
returnY+M+D+h+m+s;
}
timestampToTime(1403058804);
console.log(timestampToTime(1403058804));//2014-06-1810:33:24
//日期格式转化为时间戳
vardate=newDate('2014-04-2318:55:49:123');
//有三种方式获取
vartime1=date.getTime();
vartime2=date.valueOf();
vartime3=Date.parse(date);
console.log(time1);//1398250549123
console.log(time2);//1398250549123
console.log(time3);//1398250549000
//以上三种获取方式的区别:第一、第二种:会精确到毫秒第三种:只能精确到秒,毫秒用000替代以上三个输出结果可观察其区别注意:获取到的时间戳除以1000就可获得Unix时间戳,就可传值给后台得到。
分/秒转化为天时分
secondToDate(result){
if(result>60){
letd=parseInt(Math.floor(result/86400))
leth=d>0?Math.floor((result-d*86400)/3600):Math.floor(result/3600);
letm=h>0?Math.floor((result-d*86400-h*3600)/60):Math.floor(result/60);
returnd+'天:'+h+'时:'+m+'分'
}else{
returnresult+'秒'
}
}
为大家推荐现在关注度比较高的微信小程序教程一篇:《微信小程序开发教程》小编为大家精心整理的,希望喜欢。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。