共计 2595 个字符,预计需要花费 7 分钟才能阅读完成。
1、将毫秒,转换成工夫字符串。例如说,xx 分钟
export function getDate(ms) {const day = Math.floor(ms / (24 * 60 * 60 * 1000));
const hour = Math.floor((ms / (60 * 60 * 1000) - day * 24));
const minute = Math.floor(((ms / (60 * 1000)) - day * 24 * 60 - hour * 60));
const second = Math.floor((ms / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - minute * 60));
if (day > 0) {return day + "天" + hour + "小时" + minute + "分钟";}
if (hour > 0) {return hour + "小时" + minute + "分钟";}
if (minute > 0) {return minute + "分钟";}
if (second > 0) {return second + "秒";} else {return 0 + "秒";}
}
2、获取以后工夫 timeStr 时分秒 字符串 格局为 xx:xx:xx(23:59:59)
export function getNowDateTime(timeStr) {let now = new Date();
let year = now.getFullYear(); // 失去年份
let month = (now.getMonth() + 1).toString().padStart(2, "0"); // 失去月份
let day = now.getDate().toString().padStart(2, "0"); // 失去日期
if (timeStr != null) {return `${year}-${month}-${day} ${timeStr}`;
}
let hours = now.getHours().toString().padStart(2, "0") // 失去小时;
let minutes = now.getMinutes().toString().padStart(2, "0") // 失去分钟;
let seconds = now.getSeconds().toString().padStart(2, "0") // 失去秒;
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
3、工夫戳转年月日时分秒(例:Thu Sep 01 2022 08:00:00 GMT+0800 (中国规范工夫))
export function timestampToTime(time) {if (time) {var date = new Date(time) // 工夫戳为 10 位需 *1000,工夫戳为 13 位的话不需乘 1000
let Y = date.getFullYear() + '-';
let M = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) + '-' : date.getMonth() + 1 + '-';
let D = date.getDate() < 10 ? '0' + date.getDate() + '' : date.getDate() +' ';
let h = date.getHours() < 10 ? '0' + date.getHours() + ':' : date.getHours() + ':';
let m = date.getMinutes() < 10 ? '0' + date.getMinutes() + ':' : date.getMinutes() + ':';
let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
return Y + M + D + h + m + s;
} else {return ''}
}
4、工夫戳转年月日星期(例:Thu Sep 01 2022 08:00:00 GMT+0800 (中国规范工夫))
export function timestampToDay(time) {if (time) {var date = new Date(time) // 工夫戳为 10 位需 *1000,工夫戳为 13 位的话不需乘 1000
let Y = date.getFullYear() + '年';
let M = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) + '月' : date.getMonth() + 1 + '月';
let D = date.getDate() < 10 ? '0' + date.getDate() + '日' : date.getDate() + '日';
const w = date.getDay();
const weekObj = {
1: '星期一',
2: '星期二',
3: '星期三',
4: '星期四',
5: '星期五',
6: '星期六',
0: '星期日',
}
return Y + M + D + weekObj[w];
} else {return ''}
}
5、工夫戳转年月(例:Thu Sep 01 2022 08:00:00 GMT+0800 (中国规范工夫))
export function timestampToMonth(time) {if (time) {var date = new Date(time) // 工夫戳为 10 位需 *1000,工夫戳为 13 位的话不需乘 1000
let Y = date.getFullYear() + '年';
let M = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) + '月' : date.getMonth() + 1 + '月';
return Y + M;
} else {return ''}
}
6、工夫戳转年月日(例:Thu Sep 01 2022 08:00:00 GMT+0800 (中国规范工夫))
export function timestamp(time) {if (time) {var date = new Date(time) // 工夫戳为 10 位需 *1000,工夫戳为 13 位的话不需乘 1000
let Y = date.getFullYear() + '-';
let M = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) + '-' : date.getMonth() + 1 + '-';
let D = date.getDate() < 10 ? '0' + date.getDate() + ' ' : date.getDate();
return Y + M + D;
} else {return ''}
}
正文完
发表至: javascript
2022-10-13