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

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 2017 15: 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{
// 秒数
let second = Math.floor(time_canshu / 1000);
// 小时位
let hr = Math.floor(second / 3600);
// 分钟位
let min = Math.floor((second - hr * 3600) / 60);
// 秒位
let sec = 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;
let time = hr + ":" + min + ":" + sec + " ";
this.setData({
clock: time
})
}
},


猜你喜欢

转载自blog.csdn.net/wangle_style/article/details/80049651