当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 微信小程序实现日期格式化和倒计时

微信小程序实现日期格式化和倒计时

2019年07月18日  | 移动技术网IT编程  | 我要评论

本文实例为大家分享了微信小程序实现日期格式化和倒计时的具体代码,供大家参考,具体内容如下

首先看看日期怎么格式化

第一种:

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
 })
 }
},

时间戳转化为日期格式函数

//时间戳转化为日期格式
function timestamptotime(timestamp) {
    var date = new date(timestamp * 1000);//时间戳为10位需*1000,时间戳为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
 
//日期格式转化为时间戳
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
//以上三种获取方式的区别:第一、第二种:会精确到毫秒第三种:只能精确到秒,毫秒用000替代以上三个输出结果可观察其区别注意:获取到的时间戳除以1000就可获得unix时间戳,就可传值给后台得到。

分/秒转化为天时分

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 + '时:' + m + '分'
   } else {
    return result + '秒'
   }
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网