日期 Date
时间戳转换为时间
默认为当前时间转换结果
isMs为时间戳是否为毫秒
function timestampToTime(timestamp = Date.parse(new Date()), 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()}`
}
从Date对象里获取当前时间
const getColonTimeFromDate = date => date.toTimeString().slice(0, 8);
getColonTimeFromDate(new Date()); // "08:38:00"
返回两个日期之间相差多少天
const getDaysDiffBetweenDates = (dateInitial, dateFinal) =>
(new Date(dateFinal) - new Date(dateInitial)) / (1000 * 3600 * 24);
getDaysDiffBetweenDates('2019-01-13','2019-01-15'); // 2