时间的各种转换+倒计时

js去除时分秒

1
2
3
var time_str= '2014-9-19 13:19:21';
var t = time_str.substr(0,10);
alert(t);

获取当前时间,格式YYYY-MM-DD

1
2
3
4
5
6
7
8
9
10
11
12
13
14
getNowFormatDate(date) {
var seperator1 = "-";
var year = date.getFullYear();
var month = date.getMonth() + 1;
var strDate = date.getDate();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
var currentdate = year + seperator1 + month + seperator1 + strDate;
return currentdate;
},

时间戳转换成”yyyy–mm–dd”格式

1
2
3
4
5
6
7
fmtDate(timestamp) {
var date = new Date(timestamp);
var y = 1900 + date.getYear();
var m = "0" + (date.getMonth() + 1);
var d = "0" + date.getDate();
return y + "-" + m.substring(m.length - 2, m.length) + "-" + d.substring(d.length - 2, d.length);
},

毫秒转 X分X秒

1
2
3
4
5
6
counttime(rowtime) {
var second = Math.floor(rowtime / 1000);
var min = Math.floor(parseInt(second) / 60);
var second = parseInt(second) % 60;
return min + '分' + second + '秒'
},

倒计时

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
countDown(){
let maxtime = 30 * 60; //30分钟
let minutes = '';
let seconds = '';
--maxtime;
this.timer2 = setInterval(() => {
if (maxtime >= 0) {
minutes = Math.floor(maxtime / 60);
seconds = Math.floor(maxtime % 60);
--maxtime;
if(seconds < 10){
seconds = '0' + seconds;
}
this.timeRemaining = minutes + "分" + seconds + "秒";
} else{
clearInterval(this.timer2);
}
}, 1000);
},
海盗船长 wechat
扫码关注我的公众号哟
0%