JS 格式化时间(获取两个日期之间的每一天、每一月、每半小时、每一秒)

时间戳转换为时间

// 时间戳转换为时间
function timestampToTime(timestamp, isMs = true) {
const date = new Date(timestamp * (isMs ? 1 : 1000))
return `${date.getFullYear()}-${date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1}-${date.getDate()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`
}

获取当天过去每半小时的时间

function formatTodayHalfHours(nowDate = +new Date()) {
var timeStamp = new Date(new Date().setHours(0, 0, 0, 0));
var dateList = []; while (nowDate > timeStamp) {
var hour = new Date(nowDate).getHours() < 10 ? '0' + new Date(nowDate).getHours() : new Date(nowDate).getHours();
var minute = new Date(nowDate).getMinutes() < 31 ? '00' : '30';
dateList.unshift(hour + ':' + minute + ':' + '00');
nowDate -= 30 * 60 * 1000
}
return dateList
}

例如,当天0点 - 12点20分之间没半个小时的时间:

formatTodayHalfHours(+new Date('2019-04-28 12:20:00'))

输出:

["00:00:00", "00:30:00", "01:00:00", "01:30:00", "02:00:00", "02:30:00", "03:00:00", "03:30:00", "04:00:00", "04:30:00", "05:00:00", "05:30:00", "06:00:00", "06:30:00", "07:00:00", "07:30:00", "08:00:00", "08:30:00", "09:00:00", "09:30:00", "10:00:00", "10:30:00", "11:00:00", "11:30:00", "12:00:00"]

获取两个日期之间的每一天

function formatEveryDay(start, end) {
let dateList = [];
var startTime = getDate(start);
var endTime = getDate(end); while ((endTime.getTime() - startTime.getTime()) >= 0) {
var year = startTime.getFullYear();
var month = startTime.getMonth() + 1 < 10 ? '0' + (startTime.getMonth() + 1) : startTime.getMonth() + 1;
var day = startTime.getDate().toString().length == 1 ? "0" + startTime.getDate() : startTime.getDate();
dateList.push(year + "-" + month + "-" + day);
startTime.setDate(startTime.getDate() + 1);
}
return dateList;
} function getDate(datestr) {
var temp = datestr.split("-");
var date = new Date(temp[0], temp[1] - 1, temp[2]);
return date;
}

例如:

formatEveryDay('2019-03-05','2019-03-16');

输出:

["2019-03-05", "2019-03-06", "2019-03-07", "2019-03-08", "2019-03-09", "2019-03-10", "2019-03-11", "2019-03-12", "2019-03-13", "2019-03-14", "2019-03-15", "2019-03-16"]

获取两个日期之间的所有月份

// 格式化时间,获取两个日期之间所有月份
function getMonthBetween(start, end) { //传入的格式YYYY-MM
let dateList = []; // 时间格式 ‘2019-04-01’
let dateList2 = []; // 时间格式 ‘2019-04’
var s = start.split("-");
var e = end.split("-");
var min = new Date();
var max = new Date();
min.setFullYear(s[0], s[1] * 1 - 1, 1); // 开始日期
max.setFullYear(e[0], e[1] * 1 - 1, 1); // 结束日期
var current = min;
while (current <= max) {
var year = current.getFullYear();
var month = current.getMonth() + 1 < 10 ? '0' + (current.getMonth() + 1) : current.getMonth() + 1;
dateList.push(year + "-" + month + "-" + '01');
dateList2.push(year + "-" + month);
current.setMonth(month);
}
return [dateList, dateList2];
}

例如:

getMonthBetween('2018-12-05','2019-03-16');

输出:

[["2018-12-01","2019-01-01","2019-02-01","2019-03-01"], ["2018-12","2019-01","2019-02","2019-03"]]

获取最近n秒的时间

function formatDateToArray(steps = 40) {
var nowDate = +new Date() - 10000;
var xAxisDate = [],
postDate = []; for (var i = 0; i < steps; i++) {
var year = new Date(nowDate).getFullYear();
var month = new Date(nowDate).getMonth() + 1 < 10 ? '0' + (new Date(nowDate).getMonth() + 1) : new Date(nowDate).getMonth() + 1;
var day = new Date(nowDate).getDate() < 10 ? '0' + new Date(nowDate).getDate() : new Date(nowDate).getDate();
var hour = new Date(nowDate).getHours() < 10 ? '0' + new Date(nowDate).getHours() : new Date(nowDate).getHours();
var minute = new Date(nowDate).getMinutes() < 10 ? '0' + new Date(nowDate).getMinutes() : new Date(nowDate).getMinutes();
var second = new Date(nowDate).getSeconds() < 10 ? '0' + new Date(nowDate).getSeconds() : new Date(nowDate).getSeconds(); xAxisDate.unshift(hour + ':' + minute + ':' + second);
postDate.unshift(year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second);
nowDate = nowDate - 1000;
} return [xAxisDate, postDate];
}

例如:获取最近10秒:

formatDateToArray(10);

获取最近 n 天

function getRecentDays(num) {
var today = new Date();
var steps = today.getTime() + 1000 * 60 * 60 * 24 * num;
today.setTime(steps);
var yy = today.getFullYear();
var mm = today.getMonth();
var dd = today.getDate();
mm = bindMonth(mm + 1);
dd = doHandleMonth(dd);
return yy + "-" + mm + "-" + dd;
} function bindMonth(month) {
var m = month;
if (month.toString().length == 1) {
m = "0" + month;
}
return m;
} function doHandleMonth(month) {
var m = month;
if (month.toString().length == 1) {
m = "0" + month;
}
return m;
}

例如,9天前:

getRecentDays(-9);

获取两个日期之间的天数

function getDateDiff(startDate, endDate) {
var startTime = new Date(Date.parse(startDate.replace(/-/g, "/"))).getTime();
var endTime = new Date(Date.parse(endDate.replace(/-/g, "/"))).getTime();
var dates = Math.abs((startTime - endTime)) / (1000 * 60 * 60 * 24);
return dates;
}

例如:

getDateDiff('2019-05-30','2019-05-20')

输出:

10

上一篇:4、构造方法、this、super


下一篇:ajax异步加载回跳定位