国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 熱點 > 微信 > 正文

微信小程序實現日期格式化和倒計時

2024-07-22 01:18:11
字體:
來源:轉載
供稿:網友

本文實例為大家分享了微信小程序實現日期格式化和倒計時的具體代碼,供大家參考,具體內容如下

首先看看日期怎么格式化

第一種:

Date.prototype.Format = function (fmt) { //author: meizz   var o = {    "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 (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; }

然后是調用this.value1=new Date().Format("yyyy-MM-dd HH: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 } var time= _year + "-" + _month + "-" + _date + " " + _hour + ":" + _minute + ":" +  _second;// var time = new Date();// var formatTime = formatDateTime(time);// 返回結果:// Tue Jun 06 2017 15:31:09 GMT+ 0800(中國標準時間)// 2017 - 06 - 06 15:31:09//clock為在data中定義的空變量,存放轉化好的日期 this.setData({  clock: time })},

2、把格式化時間轉換為毫秒數

var formatTimeS = new Date('2017-06-06 15:31:09').getTime();

返回結果:1496734269900

3、把毫秒數轉換為標準時間

var formatTimeS = new Date(1496734269900);

返回結果:Tue Jun 06 201715:31:09 GMT+0800(中國標準時間)

二、實現倒計時

//倒計時:其中time_canshu為傳入的毫秒數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 { // 秒數 letsecond = Math.floor(time_canshu / 1000); // 小時位 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 }) }},            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 渝北区| 姚安县| 河北区| 且末县| 灌云县| 门头沟区| 石屏县| 自贡市| 定结县| 同江市| 延长县| 南宫市| 沈阳市| 平湖市| 呼伦贝尔市| 寻乌县| 青田县| 乌恰县| 宁化县| 宽甸| 枝江市| 新竹市| 比如县| 安新县| 义马市| 禄丰县| 贡嘎县| 新龙县| 柯坪县| 黄大仙区| 武强县| 繁峙县| 辽宁省| 岑巩县| 沐川县| 石狮市| 平舆县| 山阳县| 宜州市| 景宁| 益阳市|