js时间比较的几个常用方法
js时间比较常用方法 备忘
·
/**
* 判断当前时间是否在指定日期范围内
* @Date: 2022-11-24 11:45
* @param date [beginDateStr] [开始日期] yyyy-MM-dd
* @param date [endDateStr] [结束日期] yyyy-MM-dd
* @return Boolean
*/
const isDuringDate = (beginDateStr, endDateStr) => {
const curDate = new Date();
const beginDate = new Date(beginDateStr.replace(/-/g, "/"));
const endDate = new Date(endDateStr.replace(/-/g, "/"));
console.log(beginDate,endDate);
if (curDate >= beginDate && curDate <= endDate) {
return true;
}
return false;
};
/**
* 判断指定时间是否在指定日期范围内
* @Date: date [curDateStr] [指定时间] yyyy-MM-dd
* @param date [beginDateStr] [开始日期] yyyy-MM-dd
* @param date [endDateStr] [结束日期] yyyy-MM-dd
* @return Boolean
*/
const isCurDuringDate = (curDateStr,beginDateStr, endDateStr) => {
const curDate = new Date(curDateStr.replace(/-/g, "/"));
const beginDate = new Date(beginDateStr.replace(/-/g, "/"));
const endDate = new Date(endDateStr.replace(/-/g, "/"));
console.log(beginDate,endDate);
if (curDate >= beginDate && curDate <= endDate) {
return true;
}
return false;
};
/**
* 判断开始日期是否大于结束日期
* @param date [beginDateStr] [开始日期] yyyy-MM-dd
* @param date [endDateStr] [结束日期] yyyy-MM-dd
* @return Boolean
*/
const isDiffDate = (beginDateStr, endDateStr) => {
const beginDate = new Date(beginDateStr.replace(/-/g, "/"));
const endDate = new Date(endDateStr.replace(/-/g, "/"));
console.log(beginDate,endDate);
if ( beginDate > endDate) {
return true;
}
return false;
};
/**
* 判断当前时间是否大于参数时间
* @Date: 2022-11-24 11:45
* @param date [DateStr] [开始日期] yyyy-MM-dd
* @return Boolean
*/
const isDefDate = (DateStr) => {
const curDate = new Date();
const paramDate = new Date(DateStr.replace(/-/g, "/"));
console.log(paramDate);
if (curDate >= paramDate) {
return true;
}
return false;
};
/**
* 时间处理(显示几分钟前,,几小时前,,几天前...)
*/
const commentTimeHandle = function (dateStr) {
var publishTime = dateStr / 1000, //获取dataStr的秒数 打印结果--1536230820000
date = new Date(publishTime * 1000), //获取dateStr的标准格式 console.log(date) 打印结果 Thu Sep 06 2018 18:47:00 GMT+0800 (中国标准时间)
// 获取date 中的 年 月 日 时 分 秒
Y = date.getFullYear(),
M = date.getMonth() + 1,
D = date.getDate(),
H = date.getHours(),
m = date.getMinutes(),
s = date.getSeconds();
// 对 月 日 时 分 秒 小于10时, 加0显示 例如: 09-09 09:01
if (M < 10) {
M = '0' + M;
}
if (D < 10) {
D = '0' + D;
}
if (H < 10) {
H = '0' + H;
}
if (m < 10) {
m = '0' + m;
}
if (s < 10) {
s = '0' + s;
}
var nowTime = new Date().getTime() / 1000, //获取此时此刻日期的秒数
diffValue = nowTime - publishTime, // 获取此时 秒数 与 要处理的日期秒数 之间的差值
diff_days = parseInt(diffValue / 86400), // 一天86400秒 获取相差的天数 取整
diff_hours = parseInt(diffValue / 3600), // 一时3600秒
diff_minutes = parseInt(diffValue / 60),
diff_secodes = parseInt(diffValue);
if (diff_days > 0 && diff_days < 3) { //相差天数 0 < diff_days < 3 时, 直接返出
return diff_days + "天前";
} else if (diff_days <= 0 && diff_hours > 0) {
return diff_hours + "小时前";
} else if (diff_hours <= 0 && diff_minutes > 0) {
return diff_minutes + "分钟前";
} else if (diff_secodes < 60) {
if (diff_secodes <= 0) {
return "刚刚";
} else {
return diff_secodes + "秒前";
}
} else if (diff_days >= 3 && diff_days < 30) {
return M + '-' + D + ' ' + H + ':' + m;
} else if (diff_days >= 30) {
return Y + '-' + M + '-' + D + ' ' + H + ':' + m;
}
};
/**
* 把指定格式的字符串转换为日期对象yyyy-MM-dd HH:mm:ss
*
*/
const strFormatToDate = function (formatStr, dateStr) {
var year = 0;
var start = -1;
var len = dateStr.length;
if ((start = formatStr.indexOf('yyyy')) > -1 && start < len) {
year = dateStr.substr(start, 4);
}
var month = 0;
if ((start = formatStr.indexOf('MM')) > -1 && start < len) {
month = parseInt(dateStr.substr(start, 2)) - 1;
}
var day = 0;
if ((start = formatStr.indexOf('dd')) > -1 && start < len) {
day = parseInt(dateStr.substr(start, 2));
}
var hour = 0;
if (((start = formatStr.indexOf('HH')) > -1 || (start = formatStr.indexOf('hh')) > 1) && start < len) {
hour = parseInt(dateStr.substr(start, 2));
}
var minute = 0;
if ((start = formatStr.indexOf('mm')) > -1 && start < len) {
minute = dateStr.substr(start, 2);
}
var second = 0;
if ((start = formatStr.indexOf('ss')) > -1 && start < len) {
second = dateStr.substr(start, 2);
}
return new Date(year, month, day, hour, minute, second);
}
/**
* 日期对象转换为指定格式的字符串
* @param f 日期格式,格式定义如下 yyyy-MM-dd HH:mm:ss
* @param date Date日期对象, 如果缺省,则为当前时间
*
* YYYY/yyyy/YY/yy 表示年份
* MM/M 月份
* W/w 星期
* dd/DD/d/D 日期
* hh/HH/h/H 时间
* mm/m 分钟
* ss/SS/s/S 秒
* @return string 指定格式的时间字符串
*/
const formatDate = function (formatStr, date) {
formatStr = arguments[0] || "yyyy-MM-dd HH:mm:ss";
date = arguments[1] || new Date();
var str = formatStr;
var Week = ['日', '一', '二', '三', '四', '五', '六'];
str = str.replace(/yyyy|YYYY/, date.getFullYear());
str = str.replace(/yy|YY/, (date.getYear() % 100) > 9 ? (date.getYear() % 100).toString() : '0' + (date.getYear() % 100));
str = str.replace(/MM/, date.getMonth() > 9 ? (date.getMonth() + 1) : '0' + (date.getMonth() + 1));
str = str.replace(/M/g, date.getMonth());
str = str.replace(/w|W/g, Week[date.getDay()]);
str = str.replace(/dd|DD/, date.getDate() > 9 ? date.getDate().toString() : '0' + date.getDate());
str = str.replace(/d|D/g, date.getDate());
str = str.replace(/hh|HH/, date.getHours() > 9 ? date.getHours().toString() : '0' + date.getHours());
str = str.replace(/h|H/g, date.getHours());
str = str.replace(/mm/, date.getMinutes() > 9 ? date.getMinutes().toString() : '0' + date.getMinutes());
str = str.replace(/m/g, date.getMinutes());
str = str.replace(/ss|SS/, date.getSeconds() > 9 ? date.getSeconds().toString() : '0' + date.getSeconds());
str = str.replace(/s|S/g, date.getSeconds());
return str;
}
更多推荐
已为社区贡献1条内容
所有评论(0)