// 获取当前时间
function nowDate() {
var now = new Date();
var yy = now.getFullYear(); //年
var mm = now.getMonth() + 1; //月
var dd = now.getDate(); //日
var hh = now.getHours(); //时
var ii = now.getMinutes(); //分
var ss = now.getSeconds(); //秒
var clock = yy + "-";
if (mm < 10) clock += "0";
clock += mm + "-";
if (dd < 10) clock += "0";
clock += dd + " ";
if (hh < 10) clock += "0";
clock += hh + ":";
if (ii < 10) clock += '0';
clock += ii + ":";
if (ss < 10) clock += '0';
clock += ss;
return clock;
}
// 获取当前时间
getNowTime: function () {
let dateTime
let yy = new Date().getFullYear()
let mm = new Date().getMonth() + 1
let dd = new Date().getDate()
let hh = new Date().getHours()
let mf = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes()
:
new Date().getMinutes()
let ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds()
:
new Date().getSeconds()
dateTime = yy + '-' + mm + '-' + dd + ' ' + hh + ':' + mf + ':' + ss
console.log(dateTime)
return dateTime
},
//日期字符串转成时间戳
//例如var date = '2015-03-05 17:59:00';
function dateStrChangeTimeTamp(dateStr) {
dateStr = dateStr.substring(0, 18);
dateStr = dateStr.replace(/-/g, '/');
var timesTamp = new Date(dateStr).getTime();
//console.log("timeTamp",timesTamp)
return timesTamp
}
//把时间戳转成日期格式
//例如 timeTamp = '1425553097';
function formatTimeTamp(timeTamp) {
var time = new Date(timeTamp * 1000);
var date = ((time.getFullYear()) + "-" +
(time.getMonth() + 1) + "-" +
(time.getDate()) + " " +
(time.getHours()) + ":" +
(time.getMinutes()) + ":" +
(time.getSeconds()))
return date
}
// 计算年龄或工作年限
function ages(str) {
if(str.length <= 7){
str += '-00'
}
var r = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
if (r == null) return false;
var d = new Date(r[1], r[3] - 1, r[4]);
if (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] && d.getDate() == r[4]) {
var Y = new Date().getFullYear();
// return ("年龄 = " + (Y - r[1]) + " 周岁");
// console.log(Y - r[1],'nianling');
return (Y - r[1]);
}
return ("");
}
```javascript
//javascript 精准计算两个时间间相差多少年月日时分秒
//时间格式 2001-10-01 05:12:10 2021-10-01 05:12:10
var time = time_jisuan('2001-09-01 03:10:10', '2021-10-31 05:12:11');
console.log('===================')
console.log(time)
//相差1年,2个月,3日
function time_jisuan(start_time, end_time) {
var common_year = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
var leap_year = [0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
//验证时间格式
var reg = /^[1-9]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])\s+(20|21|22|23|[0-1]\d):[0-5]\d:[0-5]\d$/;
var regExp = new RegExp(reg);
if (!regExp.test(end_time) || !regExp.test(start_time) ) {
return false;
}
//判断前后时间大小
var timestamp_start_time = Date.parse(new Date(start_time));
var timestamp_end_time = Date.parse(new Date(end_time));
if (timestamp_end_time / 1000 - timestamp_start_time / 1000 < 0) {
return false;
}
//获取前四位
var time_info_start = start_time.split(' ');
var time_info_end = end_time.split(' ');
//获取年月日
var year_month_day_start = time_info_start[0].split('-')
var year_month_day_end = time_info_end[0].split('-')
//获取时间
var hour_minute_second_start = time_info_start[1].split(':')
var hour_minute_second_end = time_info_end[1].split(':')
var beapart_year = parseInt(year_month_day_end[0]) - parseInt(year_month_day_start[0])
//进行判断时间
var time_info_1 = parseInt(hour_minute_second_start[2]) + parseInt(hour_minute_second_start[1])*60 + parseInt(hour_minute_second_start[0])*3600;
var time_info_2 = parseInt(hour_minute_second_end[2]) + parseInt(hour_minute_second_end[1])*60 + parseInt(hour_minute_second_end[0])*3600;
var hour = 0
var minute = 0
var second = 0
var beapart_time = time_info_2 - time_info_1
var beapart_day_last = 0
if(timestamp_end_time/1000 -timestamp_start_time/1000 <=86400){
var tim = timestamp_end_time/1000 -timestamp_start_time/1000
hour = parseInt(tim / 3600)
minute = parseInt((tim - hour * 60 * 60) / 60)
second = tim - hour * 60 * 60 - minute * 60
return [0, 0, 0 , hour, minute, second];
}
if (beapart_time < 0) {
beapart_day = beapart_day- 1
beapart_time = time_info_2 + 86400 - time_info_1;
hour = parseInt(beapart_time / 3600)
minute = parseInt((beapart_time - hour * 60 * 60) / 60)
second = beapart_time - hour * 60 * 60 - minute * 60
beapart_day_last = 1
} else {
hour = parseInt(beapart_time / 3600)
minute = parseInt((beapart_time - hour * 60 * 60) / 60)
second = beapart_time - hour * 60 * 60 - minute * 60
}
//同一年
if (beapart_year === 0) {
var beapart_day = parseInt(year_month_day_end[2]) - parseInt(year_month_day_start[2]);
var beapart_month = parseInt(year_month_day_end[1]) - parseInt(year_month_day_start[1]);
if (beapart_day > 0) {
return [0, beapart_month, beapart_day - beapart_day_last, hour, minute, second];
} else {
if (parseInt(year_month_day_end[0]) % 4 === 0) {
beapart_day = leap_year[year_month_day_end[1]] - parseInt(year_month_day_start[2]) + parseInt(year_month_day_end[2])
} else {
beapart_day = common_year[year_month_day_end[1]] - parseInt(year_month_day_start[2]) + parseInt(year_month_day_end[2])
}
if (time_info_2 - time_info_1 < 0) {
beapart_day -= 1
}
return [0, beapart_month - 1, beapart_day - beapart_day_last, hour, minute, second];
}
//非同一年
} else {
//判断如果相差一年
var beapart_day = parseInt(year_month_day_end[2]) - parseInt(year_month_day_start[2]);
var beapart_month = parseInt(year_month_day_end[1]) - parseInt(year_month_day_start[1]);
var beapart_year = parseInt(year_month_day_end[0]) - parseInt(year_month_day_start[0]);
if (beapart_month < 0) {
beapart_year = beapart_year - 1;
beapart_month = 12 - parseInt(year_month_day_end[1]) + parseInt(year_month_day_start[1])
}
if (beapart_day > 0) {
return [beapart_year, beapart_month, beapart_day - beapart_day_last, hour, minute, second];
}else {
if (parseInt(year_month_day_end[0]) % 4 === 0) {
beapart_day = leap_year[year_month_day_end[1]] - parseInt(year_month_day_start[2]) + parseInt(year_month_day_end[2])
} else {
beapart_day = common_year[year_month_day_end[1]] - parseInt(year_month_day_start[2]) + parseInt(year_month_day_end[2])
}
return [beapart_year, beapart_month - 1, beapart_day - beapart_day_last, hour, minute, second];
}
}
}
// 过滤时间转成 - 4-5-
function filterTime0(time) {
var arr = time.split(’-’);
arr.shift();
return arr.join(’-’);
}
// 过滤时间转成 - 4月5日
function filterTime1(time) {
var arr = time.split(’-’);
var month = arr[1] + ‘月’;
var day = arr[2] + ‘日’;
return month + day;
}
// 过滤时间不要分秒
function filterTime2(time) {
var arr = time.split(’:’);
arr.pop();
// //console.log(arr);
return arr.join(’:’);
}
// 过滤时间转成 - 2019年4月5日
function filterTime3(time) {
var arr = time.split(’-’);
var year = arr[0] + ‘年’;
var month = arr[1] + ‘月’;
var day = arr[2] + ‘日’;
return year + month + day;
}
根据当前时间判断过去多久
function getDateDiff(dateTimeStamp) {
var minute = 1000 * 60;
var hour = minute * 60;
var day = hour * 24;
var halfamonth = day * 15;
var month = day * 30;
var now = new Date().getTime();
var diffValue = now - dateTimeStamp;
if (diffValue < 0) {
return;
}
var monthC = diffValue / month;
var weekC = diffValue / (7 * day);
var dayC = diffValue / day;
var hourC = diffValue / hour;
var minC = diffValue / minute;
if (monthC >= 1) {
result = “” + parseInt(monthC) + “月前”;
} else if (weekC >= 1) {
result = “” + parseInt(weekC) + “周前”;
} else if (dayC >= 1) {
result = “” + parseInt(dayC) + “天前”;
} else if (hourC >= 1) {
result = “” + parseInt(hourC) + “小时前”;
} else if (minC >= 1) {
result = “” + parseInt(minC) + “分钟前”;
} else
result = “刚刚”;
return result;
}