时间常用方法

集合了一些时间上的处理方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* [setNdayDate 获取n天后日期(转格式后的)]
* @param {[type]} num [天数] _time [传入时间]
*/
function setNdayDate(num, _time) {
let _this = this;
let d = new Date();
if (_time) {
d = new Date(_time.replace(/-/g, "/"));
}
let year = d.getFullYear();
let month = _this.addZero(d.getMonth() + 1);
let day = _this.addZero(d.getDate());
let hour = _this.addZero(d.getHours());
let min = _this.addZero(d.getMinutes());
let sec = _this.addZero(d.getSeconds());

//今天
let new_day = year + '-' + month + '-' + day + ' ' + hour + ':' + min + ':' + sec;
let old_day = new_day;
if (num != 0) {
//num天后
d.setDate(parseFloat(day) + parseFloat(num));
let old_year = d.getFullYear();
let old_month = _this.addZero(d.getMonth() + 1);
old_day = _this.addZero(d.getDate());
let old_hour = _this.addZero(d.getHours());
let old_min = _this.addZero(d.getMinutes());
let old_sec = _this.addZero(d.getSeconds());

old_day = old_year + '-' + old_month + '-' + old_day + ' ' + old_hour + ':' + old_min + ':' + old_sec;
}
return {
new_day: new_day,
new_day_str: Date.parse(new_day.replace(/-/g, "/")),
old_day: old_day,
old_day_str: Date.parse(old_day.replace(/-/g, "/")),
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* [计算出时间差]
* @param {[type]} str1 [2010-10-20 15:00:00]
* @param {[type]} str2 [2011-10-20 15:00:00]
*/
function timeDifference(str1, str2) {
let date1 = Date.parse(str1.replace(/-/g, "/")); //开始时间
let date2 = Date.parse(str2.replace(/-/g, "/")); //结束时间
let date3 = date2 - date1; //时间差的毫秒数

//计算出相差天数
let days = Math.floor(date3 / (24 * 3600 * 1000));
//计算天数后剩余的毫秒数
let leave1 = date3 % (24 * 3600 * 1000);
//计算出小时数
let hours = Math.floor(leave1 / (3600 * 1000));
//计算小时数后剩余的毫秒数
let leave2 = leave1 % (3600 * 1000);
//计算相差分钟数
let minutes = Math.floor(leave2 / (60 * 1000));
//计算分钟数后剩余的毫秒数
let leave3 = leave2 % (60 * 1000);
//计算相差秒数
let seconds = Math.round(leave3 / 1000);

return {
_date3: date3,
_days: days,
_hours: hours,
_minutes: minutes,
_seconds: seconds,
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* [获取n分钟后的日期(转格式后的)]
* @param {[type]} minutes [分钟数]
*/
function setNMinuteDate(num) {
let date = new Date();
let seperator1 = "-";
let seperator2 = ":";

let interTimes = num * 60 * 1000;

date = new Date(Date.parse(date) + interTimes);

let month = date.getMonth() + 1;
let strDate = date.getDate();
let hour = date.getHours();
let minutes = date.getMinutes();
let seconds = date.getSeconds()
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
if (hour >= 0 && hour <= 9) {
hour = "0" + hour;
}
if (minutes >= 0 && minutes <= 9) {
minutes = "0" + minutes;
}
if (seconds <= 9) {
seconds = '0' + seconds;
}

let resdate = date.getFullYear() + seperator1 + month + seperator1 + strDate +
" " + hour + seperator2 + minutes +
seperator2 + seconds;
return resdate;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* [获取n毫秒后的日期(转格式后的)]
* @param {[string } time [ 时间字符串]
* @param {[number]} seconds [ 距离这个时间多少毫秒]
* @return {string} 返回这个格式的时间字符串 2017-02-20 17:49:01
*/
function formatTimeStr(time, seconds) {
let _t1 = new Date(Date.parse(time.replace(/-/g, "/"))).getTime() + seconds;
time = new Date(_t1);
let y = time.getFullYear(); //年
let m = time.getMonth() + 1; //月
let d = time.getDate(); //日
let h = time.getHours(); //时
let mm = time.getMinutes(); //分
let s = time.getSeconds(); //秒
return y + "-" + this.addZero(m) + "-" + this.addZero(d) + " " + this.addZero(h) + ":" + this.addZero(mm) + ":" + this.addZero(s);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* [格式化时间将时间戳转为时间(转格式后的)]
* @param {[string } time [ 时间字符串]
* @return {string} 返回这个格式的时间字符串 2017-02-20 17:49:01
*/
function formatTimestamp(time) {
console.log(time, '????????????????')
time = new Date(time);
console.log(time, '22222222222222222')
let y = time.getFullYear(); //年
let m = time.getMonth() + 1; //月
let d = time.getDate(); //日
let h = time.getHours(); //时
let mm = time.getMinutes(); //分
let s = time.getSeconds(); //秒
return y + "-" + this.addZero(m) + "-" + this.addZero(d) + " " + this.addZero(h) + ":" + this.addZero(mm) + ":" + this.addZero(s);
}