|
@@ -0,0 +1,181 @@
|
|
|
|
+export function parseTime(time, cFormat) {
|
|
|
|
+ if (!time) {
|
|
|
|
+ return null
|
|
|
|
+ }
|
|
|
|
+ const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
|
|
|
|
+ let date
|
|
|
|
+ if (typeof time === 'object') {
|
|
|
|
+ date = time
|
|
|
|
+ } else {
|
|
|
|
+ if ((typeof time === 'string')) {
|
|
|
|
+ if ((/^[0-9]+$/.test(time))) {
|
|
|
|
+ // support "1548221490638"
|
|
|
|
+ time = parseInt(time)
|
|
|
|
+ } else {
|
|
|
|
+ // support safari
|
|
|
|
+ // https://stackoverflow.com/questions/4310953/invalid-date-in-safari
|
|
|
|
+ time = time.replace(new RegExp(/-/gm), '/')
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if ((typeof time === 'number') && (time.toString().length === 10)) {
|
|
|
|
+ time = time * 1000
|
|
|
|
+ }
|
|
|
|
+ date = new Date(time)
|
|
|
|
+ }
|
|
|
|
+ const formatObj = {
|
|
|
|
+ y: date.getFullYear(),
|
|
|
|
+ m: date.getMonth() + 1,
|
|
|
|
+ d: date.getDate(),
|
|
|
|
+ h: date.getHours(),
|
|
|
|
+ i: date.getMinutes(),
|
|
|
|
+ s: date.getSeconds(),
|
|
|
|
+ a: date.getDay(),
|
|
|
|
+ p: date.getHours(),
|
|
|
|
+ }
|
|
|
|
+ const time_str = format.replace(/{([ymdhisap])+}/g, (result, key) => {
|
|
|
|
+ const value = formatObj[key]
|
|
|
|
+ // Note: getDay() returns 0 on Sunday
|
|
|
|
+ if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
|
|
|
|
+ if (key === 'p') {
|
|
|
|
+ if (value < 12) {
|
|
|
|
+ return '上午'
|
|
|
|
+ }
|
|
|
|
+ return '下午'
|
|
|
|
+ }
|
|
|
|
+ return value.toString().padStart(2, '0')
|
|
|
|
+ })
|
|
|
|
+ return time_str
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 时间日期转换
|
|
|
|
+ * @param date 当前时间,new Date() 格式
|
|
|
|
+ * @param format 需要转换的时间格式字符串
|
|
|
|
+ * @returns 返回拼接后的时间字符串
|
|
|
|
+ */
|
|
|
|
+export function formatDate(date : Date | string, format ?: string) : string {
|
|
|
|
+ // 日期不存在,则返回空
|
|
|
|
+ if (!date) {
|
|
|
|
+ return ''
|
|
|
|
+ }
|
|
|
|
+ const _date = typeof date === 'string' || 'object' ? new Date(date) : date
|
|
|
|
+ // 日期存在,则进行格式化
|
|
|
|
+ if (format === undefined) {
|
|
|
|
+ format = '{y}-{m}-{d} {h}:{i}:{s}'
|
|
|
|
+ }
|
|
|
|
+ return parseTime(_date, format)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 将时间转换为 `几秒前`、`几分钟前`、`几小时前`、`几天前`
|
|
|
|
+ * @param param 当前时间,new Date() 格式或者字符串时间格式
|
|
|
|
+ * @param format 需要转换的时间格式字符串
|
|
|
|
+ * @returns 返回拼接后的时间字符串
|
|
|
|
+ */
|
|
|
|
+export function formatPast(param : string | Date, format = '{y}-{m}-{d} {h}:{i}:{s}') : string {
|
|
|
|
+ // 传入格式处理、存储转换值
|
|
|
|
+ let t : any, s : number
|
|
|
|
+ // 获取js 时间戳
|
|
|
|
+ let time : number = new Date().getTime()
|
|
|
|
+ // 是否是对象
|
|
|
|
+ typeof param === 'string' || 'object' ? (t = new Date(param).getTime()) : (t = param)
|
|
|
|
+ // 当前时间戳 - 传入时间戳
|
|
|
|
+ time = Number.parseInt(`${time - t}`)
|
|
|
|
+ if (time < 10000) {
|
|
|
|
+ // 10秒内
|
|
|
|
+ return '刚刚'
|
|
|
|
+ } else if (time < 60000 && time >= 10000) {
|
|
|
|
+ // 超过10秒少于1分钟内
|
|
|
|
+ s = Math.floor(time / 1000)
|
|
|
|
+ return `${s}秒前`
|
|
|
|
+ } else if (time < 3600000 && time >= 60000) {
|
|
|
|
+ // 超过1分钟少于1小时
|
|
|
|
+ s = Math.floor(time / 60000)
|
|
|
|
+ return `${s}分钟前`
|
|
|
|
+ } else if (time < 86400000 && time >= 3600000) {
|
|
|
|
+ // 超过1小时少于24小时
|
|
|
|
+ s = Math.floor(time / 3600000)
|
|
|
|
+ return `${s}小时前`
|
|
|
|
+ } else if (time < 259200000 && time >= 86400000) {
|
|
|
|
+ // 超过1天少于3天内
|
|
|
|
+ s = Math.floor(time / 86400000)
|
|
|
|
+ return `${s}天前`
|
|
|
|
+ } else {
|
|
|
|
+ // 超过3天
|
|
|
|
+ const date = typeof param === 'string' || 'object' ? new Date(param) : param
|
|
|
|
+ return formatDate(date, format)
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+//根据出生日期计算年龄
|
|
|
|
+export const calculateAge = (birthDate) => {
|
|
|
|
+ const today = new Date(); // 获取当前日期
|
|
|
|
+ // 将传入的出生日期字符串转换为Date对象
|
|
|
|
+ const birth = new Date(birthDate);
|
|
|
|
+ let age = today.getFullYear() - birth.getFullYear(); // 计算年份差值
|
|
|
|
+ // 检查是否已经过了生日
|
|
|
|
+ const hasPassedBirthday = today.getMonth() > birth.getMonth() ||
|
|
|
|
+ (today.getMonth() === birth.getMonth() && today.getDate() >= birth.getDate());
|
|
|
|
+ if (!hasPassedBirthday) {
|
|
|
|
+ age--; // 如果还没过生日,则减去1年
|
|
|
|
+ }
|
|
|
|
+ return age;
|
|
|
|
+}
|
|
|
|
+/**
|
|
|
|
+ * 身份证号获得1:男/2:女
|
|
|
|
+ */
|
|
|
|
+export const getSexByCardNo = (cardNo: string): string => {
|
|
|
|
+ let letter = '';
|
|
|
|
+ if (cardNo.length == 15) {
|
|
|
|
+ letter= cardNo.substring(14)
|
|
|
|
+ } else if (cardNo.length == 18) {
|
|
|
|
+ letter= cardNo.substring(16,17)
|
|
|
|
+ }
|
|
|
|
+ if (letter != '') {
|
|
|
|
+ return Number(letter)%2?'1':'2'
|
|
|
|
+ }
|
|
|
|
+ return ''
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 身份证号获得出身年月
|
|
|
|
+ */
|
|
|
|
+export const getBirthByCardNo = (cardNo: string,split:string='-'): string => {
|
|
|
|
+ let year: string = '';
|
|
|
|
+ let month: string = '';
|
|
|
|
+ let day: string = '';
|
|
|
|
+
|
|
|
|
+ if (cardNo.length == 15) {
|
|
|
|
+ var org_birthday = cardNo.substring(6, 12);
|
|
|
|
+ //获取出生年月日
|
|
|
|
+ year = "19" + org_birthday.substring(0, 2);
|
|
|
|
+ month = org_birthday.substring(2, 4);
|
|
|
|
+ day = org_birthday.substring(4, 6);
|
|
|
|
+
|
|
|
|
+ } else if (cardNo.length == 18) {
|
|
|
|
+ year = cardNo.substring(6,10);
|
|
|
|
+ month = cardNo.substring(10,12);
|
|
|
|
+ day = cardNo.substring(12,14);
|
|
|
|
+ }
|
|
|
|
+ return year+split+month+split+day
|
|
|
|
+}
|
|
|
|
+/**
|
|
|
|
+ * 身份证号获得年龄
|
|
|
|
+ */
|
|
|
|
+export const getAgeByCardNo = (cardNo: string): number => {
|
|
|
|
+ let birth = getBirthByCardNo(cardNo)
|
|
|
|
+ if (birth == '') {
|
|
|
|
+ return 0
|
|
|
|
+ }
|
|
|
|
+ let birthArr = birth.split('-');
|
|
|
|
+ var newDate = new Date();
|
|
|
|
+ var month = newDate.getMonth() + 1;
|
|
|
|
+ var day = newDate.getDate();
|
|
|
|
+ var age = newDate.getFullYear() - Number(birthArr[0]) - 1;
|
|
|
|
+ if (Number(birthArr[1])< month || Number(birthArr[1]) == month && Number(birthArr[2]) <= day) {
|
|
|
|
+ age++;
|
|
|
|
+ }
|
|
|
|
+ return age;
|
|
|
|
+}
|