78 lines
2.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

export const monthsDifferenceEndDay = (str: string, num: number) => {
// 获取当前日期
const currentDate = new Date(str)
// 创建一个新的日期对象用于存储加3个月后的日期
const futureDate = new Date(currentDate)
// 加3个月
futureDate.setMonth(futureDate.getMonth() + num)
// 减去1天
futureDate.setDate(futureDate.getDate() - 1)
// 格式化为 YYYY-MM-DD
const formattedDate = futureDate.toISOString().split('T')[0]
return formattedDate
}
export const getNextYearDate = (date: string) => {
//获取一年后日期
if (date) {
// 设置目标日期为2023-07-11
const targetDate = new Date(date)
targetDate.setDate(targetDate.getDate() - 1) //方法一
// 获取一年后的日期
targetDate.setFullYear(targetDate.getFullYear() + 1)
return targetDate.toISOString().split('T')[0]
} else {
return undefined
}
}
export const compareDates = (date1: string, date2: string): number => {
// 创建日期对象
const dateObj1 = new Date(date1)
const dateObj2 = new Date(date2)
// 比较日期
if (dateObj1 > dateObj2) {
return 1 // date1 大于 date2
} else if (dateObj1 < dateObj2) {
return -1 // date1 小于 date2
} else {
return 0 // date1 等于 date2
}
}
export const calculateDaysDifference = (selectedDate: string) => {
//获取选择的日期与当前的日期相差多少天
// 创建当前日期对象
const currentDate: any = new Date()
// 创建选择的日期对象
const selectedDateObj: any = new Date(selectedDate)
// 计算两个日期的毫秒差
const timeDifference = currentDate - selectedDateObj
// 将毫秒差转换为天数差
const daysDifference = Math.floor(timeDifference / (1000 * 60 * 60 * 24))
return daysDifference
}
export const DaysDifference = (selectedDate: string) => {
//获取选择的日期与当前的日期相差多少天
// 创建当前日期对象
const currentDate: any = new Date()
// 创建选择的日期对象
const selectedDateObj: any = new Date(selectedDate)
// 计算两个日期的毫秒差
const timeDifference = selectedDateObj - currentDate
// 将毫秒差转换为天数差
const daysDifference = Math.floor(timeDifference / (1000 * 60 * 60 * 24))
return daysDifference
}
export const onSplitArray = (val: string, type: string) => {
let resultArray = val.split(type)
return resultArray
}
export function formatPhoneNumber(phone: string) {
return phone ? phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2') : ''
}