Files
ghatreh_user/src/utils/useDateTime.ts
T

55 lines
1.4 KiB
TypeScript
Raw Normal View History

2025-10-05 09:44:14 +03:30
//@ts-ignore
import moment from 'moment-jalaali'
//@ts-ignore
import fa from 'moment/src/locale/fa'
moment.locale('fa', fa)
moment.loadPersian()
function momentTimeAgo(dataTime: string) {
return moment.utc(dataTime).fromNow()
}
function toJalali(dateTime: string, format = 'jYYYY/jMM/jDD-HH:mm') {
return moment.utc(dateTime).format(format)
}
function toJalaliOutOfUTC(dateTime: string, format = 'jYYYY/jMM/jDD-HH:mm') {
const date = new Date()
date.setHours(3, 30)
return moment.utc(dateTime).format(format)
}
function toJalaliNotime(dateTime: string, format = 'jYYYY/jMM/jDD') {
return moment.utc(dateTime).format(format)
}
function toTime(dateTime: string, format = 'HH:mm:ss') {
return moment.utc(dateTime).format(format)
}
function diffDateByYearMontDay(date: string) {
const a = moment(date)
const b = moment()
const years = a.diff(b, 'year')
b.add(years, 'years')
const months = a.diff(b, 'months')
b.add(months, 'months')
const days = a.diff(b, 'days')
let tempStr = ''
if (years != 0) {
tempStr += years + ' سال'
}
if (months != 0) {
if (tempStr.length > 0) tempStr += ' و '
tempStr += months + ' ماه'
}
if (days != 0) {
if (tempStr.length > 0) tempStr += ' و '
tempStr += days + ' روز'
}
return tempStr
}
export {momentTimeAgo, toJalali, diffDateByYearMontDay, toTime, toJalaliNotime, toJalaliOutOfUTC}