微信小程序?qū)崿F(xiàn)日期格式化和倒計(jì)時(shí)
本文實(shí)例為大家分享了微信小程序?qū)崿F(xiàn)日期格式化和倒計(jì)時(shí)的具體代碼,供大家參考,具體內(nèi)容如下
首先看看日期怎么格式化
第一種:
Date.prototype.Format = function (fmt) { //author: meizz
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小時(shí)
"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 (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
然后是調(diào)用this.value1=new Date().Format("yyyy-MM-dd HH:MM:SS")
第二種
1.中國(guó)標(biāo)準(zhǔn)時(shí)間格式化:
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 }
var time= _year + "-" + _month + "-" + _date + " " + _hour + ":" + _minute + ":" +
_second;
// var time = new Date();
// var formatTime = formatDateTime(time);
// 返回結(jié)果:
// Tue Jun 06 2017 15:31:09 GMT+ 0800(中國(guó)標(biāo)準(zhǔn)時(shí)間)
// 2017 - 06 - 06 15:31:09
//clock為在data中定義的空變量,存放轉(zhuǎn)化好的日期
this.setData({
clock: time
})
},
2、把格式化時(shí)間轉(zhuǎn)換為毫秒數(shù)
var formatTimeS = new Date('2017-06-06 15:31:09').getTime();
返回結(jié)果:1496734269900
3、把毫秒數(shù)轉(zhuǎn)換為標(biāo)準(zhǔn)時(shí)間
var formatTimeS = new Date(1496734269900);
返回結(jié)果:Tue Jun 06 201715:31:09 GMT+0800(中國(guó)標(biāo)準(zhǔn)時(shí)間)
二、實(shí)現(xiàn)倒計(jì)時(shí)
//倒計(jì)時(shí):其中time_canshu為傳入的毫秒數(shù)
date_format: function (time_canshu) {
// let formatTime1 = new Date().getTime();
// let formatTime2 = new Date('2018-04-24 15:31:09').getTime();
// let formatTimeS = new Date(formatTime2 - formatTime1);
var none = '00:00:00';
if (formatTimeS<=0){
this.setData({
clock: none
})} else {
// 秒數(shù)
letsecond = Math.floor(time_canshu / 1000);
// 小時(shí)位
lethr = Math.floor(second / 3600);
// 分鐘位
letmin = Math.floor((second - hr * 3600) /60);
// 秒位
letsec = second % 60;// equal to => var sec = 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
})
}
},
時(shí)間戳轉(zhuǎn)化為日期格式函數(shù)
//時(shí)間戳轉(zhuǎn)化為日期格式
function timestampToTime(timestamp) {
var date = new Date(timestamp * 1000);//時(shí)間戳為10位需*1000,時(shí)間戳為13位的話不需乘1000
var Y = date.getFullYear() + '-';
var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
var D = date.getDate() + ' ';
var h = date.getHours() + ':';
var m = date.getMinutes() + ':';
var s = date.getSeconds();
return Y+M+D+h+m+s;
}
timestampToTime(1403058804);
console.log(timestampToTime(1403058804));//2014-06-18 10:33:24
//日期格式轉(zhuǎn)化為時(shí)間戳
var date = new Date('2014-04-23 18:55:49:123');
// 有三種方式獲取
var time1 = date.getTime();
var time2 = date.valueOf();
var time3 = Date.parse(date);
console.log(time1);//1398250549123
console.log(time2);//1398250549123
console.log(time3);//1398250549000
//以上三種獲取方式的區(qū)別:第一、第二種:會(huì)精確到毫秒第三種:只能精確到秒,毫秒用000替代以上三個(gè)輸出結(jié)果可觀察其區(qū)別注意:獲取到的時(shí)間戳除以1000就可獲得Unix時(shí)間戳,就可傳值給后臺(tái)得到。
分/秒轉(zhuǎn)化為天時(shí)分
secondToDate (result) {
if (result > 60) {
let d = parseInt(Math.floor(result / 86400))
let h = d > 0? Math.floor((result - d * 86400) / 3600): Math.floor(result / 3600);
let m = h > 0? Math.floor((result - d * 86400 - h * 3600) / 60): Math.floor(result / 60);
return d + '天:' + h + '時(shí):' + m + '分'
} else {
return result + '秒'
}
}
為大家推薦現(xiàn)在關(guān)注度比較高的微信小程序教程一篇:《微信小程序開發(fā)教程》小編為大家精心整理的,希望喜歡。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript中async與await實(shí)現(xiàn)原理與細(xì)節(jié)
這篇文章主要介紹了JavaScript中async與await實(shí)現(xiàn)原理與細(xì)節(jié),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09
JavaScript實(shí)現(xiàn)獲取select下拉框中第一個(gè)值的方法
這篇文章主要介紹了JavaScript實(shí)現(xiàn)獲取select下拉框中第一個(gè)值的方法,涉及javascript針對(duì)頁(yè)面元素屬性的相關(guān)獲取操作技巧,需要的朋友可以參考下2018-02-02
js中異步函數(shù)async function變同步函數(shù)的簡(jiǎn)單入門
這篇文章主要介紹了js中異步函數(shù)async function變同步函數(shù)的簡(jiǎn)單入門,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
JavaScript常用工具函數(shù)匯總(瀏覽器環(huán)境)
這篇文章主要匯總了JavaScript常用的工具函數(shù),幫助大家更好的理解和使用JavaScript,感興趣的朋友可以了解下2020-09-09

