| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- import accounting from 'accounting'
- const formatRelative = function (value, ms = false) {
- let now = ms ? +new Date() : +new Date() / 1000
- let day, hour, minute, second, result
- let _value = value - now
- if (_value <= 0) {
- return '0天'
- }
- day = parseInt(_value / 86400)
- hour = parseInt(_value % 86400 / 3600)
- minute = parseInt(_value % 3600 / 60)
- second = parseInt(_value % 3600 % 60)
- result = day > 0 ? `${day}天` : hour > 0 ? `${hour}小时` : minute > 0 ? `${minute}分` : `${second}秒`
- return result
- }
- const formatTime = function (value) {
- let date = new Date(value * 1000)
- let year = date.getFullYear()
- let month = date.getMonth() + 1
- let day = date.getDate()
- let hour = date.getHours()
- let minute = date.getMinutes()
- return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute].map(formatNumber).join(':')
- }
- const formatChTime = function (value) {
- let date = new Date(value * 1000)
- let year = date.getFullYear()
- let month = date.getMonth() + 1
- let day = date.getDate()
- let hour = date.getHours()
- let minute = date.getMinutes()
- let second = date.getSeconds()
- return year + '年' + month + '月' + day + '日' + ' , ' + [hour, minute, second].map(formatNumber).join(':')
- }
- const formatHhTime = function (value) {
- let date = new Date(value * 1000)
- let year = date.getFullYear()
- let month = date.getMonth() + 1
- let day = date.getDate()
- return year + '年' + month + '月' + day + '日'
- }
- const formatSTime = function (value) {
- let date = new Date(value * 1000)
- let year = date.getFullYear()
- let month = date.getMonth() + 1
- let day = date.getDate()
- let hour = date.getHours()
- let minute = date.getMinutes()
- let second = date.getSeconds()
- return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
- }
- const formatDate = function (value) {
- let date = new Date(value * 1000)
- let year = date.getFullYear()
- let month = date.getMonth() + 1
- let day = date.getDate()
- return [year, month, day].map(formatNumber).join('-')
- }
- const formatDDate = function (value) {
- let date = new Date(value * 1000)
- let year = date.getFullYear()
- let month = date.getMonth() + 1
- let day = date.getDate()
- return [year, month, day].map(formatNumber).join('.')
- }
- const formatNumber = function (n) {
- n = n.toString()
- return n[1] ? n : '0' + n
- }
- const format = (time) => {
- let ymd = ''
- let mouth = (time.getMonth() + 1) >= 10 ? (time.getMonth() + 1) : ('0' + (time.getMonth() + 1))
- let day = time.getDate() >= 10 ? time.getDate() : ('0' + time.getDate())
- ymd += time.getFullYear() + '-' // 获取年份。
- ymd += mouth + '-' // 获取月份。
- ymd += day // 获取日。
- return ymd // 返回日期。
- }
- export const getAllDate = (start, end) => {
- let dateArr = []
- let startArr = start.split('-')
- let endArr = end.split('-')
- let db = new Date()
- db.setUTCFullYear(startArr[0], startArr[1] - 1, startArr[2])
- let de = new Date()
- de.setUTCFullYear(endArr[0], endArr[1] - 1, endArr[2])
- let unixDb = db.getTime()
- let unixDe = de.getTime()
- let stamp
- const oneDay = 24 * 60 * 60 * 1000
- for (stamp = unixDb; stamp <= unixDe;) {
- dateArr.push(format(new Date(parseInt(stamp))))
- stamp = stamp + oneDay
- }
- return dateArr
- }
- const getAcounting = function (val, text = '¥', short = true, sym = ',') {
- let max = 1000000
- if (val >= max && short) {
- return text + parseFloat((val / max).toFixed(3)) + '万'
- } else {
- return accounting.formatMoney(val / 100, text, val % 100 ? 2 : 0, sym)
- }
- }
- const numFormat = function (num) {
- if (num >= 10000) {
- num = Math.round(num / 1000) / 10 + '万'
- return num
- } else {
- return num
- }
- }
- const timeTalk = function (val) {
- let result
- let time = Math.round(new Date().getTime() / 1000)
- if (time - val < 86400 && time - val > 3600) {
- result = Math.floor((time - val) / 3600)
- return result + '小时前'
- } else if (time - val <= 3600 && time - val > 60) {
- result = Math.floor((time - val) / 60)
- return result + '分钟前'
- } else if (time - val <= 60 && time - val > 0) {
- return '刚刚'
- } else {
- result = formatTime(val)
- return result
- }
- }
- export default {
- formatRelative,
- formatTime,
- formatDate,
- // getAcounting,
- timeTalk,
- formatSTime,
- getAllDate,
- format,
- getAcounting,
- formatDDate,
- formatChTime,
- numFormat,
- formatHhTime
- }
|