function getDays(time1, time2) {
if(typeof time1 === 'string') {
time1 = parse(time1);
}
if(typeof time2 === 'string') {
time2 = parse(time2);
}
return parseInt(Math.abs(time1.getTime() - time2.getTime()) / (1000 * 60 * 60 * 24));
}
function lpadNum(num, len) {
var l = num.toString().length;
while(l < len) {
num = "0" + num;
l++;
}
return num;
}
function parse(str) {
return new Date(str.replace(/-/g, "/"));
}
function formatDateTime(time) {
if(typeof time === 'string') {
time = parse(time);
}
return yyyymmdd(time) + " " + hhmmss(time)
}
function yyyymmdd(time, joinStr = '-') {
if(typeof time === 'string') {
time = parse(time);
}
let year = time.getFullYear();
let month = time.getMonth() + 1;
let date = time.getDate();
return year + joinStr + lpadNum(month,2) + joinStr + lpadNum(date,2);
}
function hhmmss(time) {
if(typeof time === 'string') {
time = parse(time);
}
let hours = time.getHours();
let minutes = time.getMinutes();
let seconds = time.getSeconds();
return lpadNum(hours,2) + ":" + lpadNum(minutes,2) + ":" + lpadNum(seconds,2);
}
function hhmm(time) {
if(typeof time === 'string') {
time = parse(time);
}
let hours = time.getHours();
let minutes = time.getMinutes();
return lpadNum(hours,2) + ":" + lpadNum(minutes,2);
}
function showSimpleTime(time) {
if(typeof time === 'string') {
time = parse(time);
}
let nowDate = new Date();
let fullYear = time.getFullYear();
let month = time.getMonth() + 1;
let date = time.getDate();
let hours = time.getHours();
let minutes = time.getMinutes();
let dayCount = getDays(parse(yyyymmdd(time)), parse(yyyymmdd(nowDate)));
if(dayCount === 0) {
return lpadNum(hours,2) + ":" + lpadNum(minutes, 2);
}
if (dayCount === 1) {
return "昨天 " + lpadNum(hours,2) + ":" + lpadNum(minutes, 2);
}
if (dayCount === 2) {
return "前天 " + lpadNum(hours,2) + ":" + lpadNum(minutes, 2);
}
if(fullYear === nowDate.getFullYear()) {
return month + "/" + date + " " + lpadNum(hours,2) + ":" + lpadNum(minutes, 2);
}
return fullYear + "/" + month + "/" + date;
}