Skip to content

Commit

Permalink
feat: add formatDateDiff (需优化)
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Jan 31, 2019
1 parent 86a22ec commit 0a283d4
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 964 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"sinon": "^6.3.5",
"standard-version": "^4.4.0",
"ts-jest": "^23.10.4",
"ts-node": "^8.0.2",
"typed-we-app": "^1.7.0-update.6",
"typescript": "^3.1.6"
},
Expand Down
93 changes: 93 additions & 0 deletions src/formatDateDiff.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { FormatDateValue } from './formatDate'
import toDate from './toDate'

const MINUTE = 60
const HOUR = MINUTE * 60
const DAY = HOUR * 24
const MONTH = DAY * 30
const YEAR = DAY * 365

// function getDaysOfYear(year: number): number {
// return Math.floor((new Date(year, 11, 31).getTime() - new Date(year, 0, 0).getTime()) / (1000 * 60 * 60 * 24))
// }

/**
* 格式化日期间距。
*
* @param startDate 开始日期
* @param endDate 结束日期
* @param template 格式化模板
* @returns 格式化后的字符串
*/
export default function formatDateDiff(
startDate: FormatDateValue,
endDate: FormatDateValue,
template: string
): string {
template = `|${template}|`
const startTime = toDate(startDate).getTime()
const endTime = toDate(endDate).getTime()
const diffTime = endTime - startTime
const diffSeconds = Math.round(Math.abs(diffTime / 1000))
const years = Math.floor(diffSeconds / YEAR)
let months = Math.floor((diffSeconds - years * YEAR) / MONTH)
let days = Math.floor((diffSeconds - years * YEAR - months * MONTH) / DAY)
let hours = Math.floor((diffSeconds - years * YEAR - months * MONTH - days * DAY) / HOUR)
let minutes = Math.floor((diffSeconds - years * YEAR - months * MONTH - days * DAY - hours * HOUR) / MINUTE)
let seconds = diffSeconds - years * YEAR - months * MONTH - days * DAY - hours * HOUR - minutes * MINUTE
if (template.indexOf('y') >= 0) {
if (years > 0) {
template = template.replace('y', String(years))
} else {
template = template.replace(/\|[^|]*?y[^|]*?(?=\|)/, '')
}
} else {
months += years * 12
}
if (template.indexOf('m') >= 0) {
if (months > 0) {
template = template.replace('m', String(months))
} else {
template = template.replace(/\|[^|]*?m[^|]*?(?=\|)/, '')
}
} else {
days += months * 30
}
if (template.indexOf('d') >= 0) {
if (days > 0) {
template = template.replace('d', String(days))
} else {
template = template.replace(/\|[^|]*?d[^|]*?(?=\|)/, '')
}
} else {
hours += days * 24
}
if (template.indexOf('h') >= 0) {
if (hours > 0) {
template = template.replace('h', String(hours))
} else {
template = template.replace(/\|[^|]*?h[^|]*?(?=\|)/, '')
}
} else {
minutes += hours * 60
}
if (template.indexOf('i') >= 0) {
if (minutes > 0) {
template = template.replace('i', String(minutes))
} else {
template = template.replace(/\|[^|]*?i[^|]*?(?=\|)/, '')
}
} else {
seconds += minutes * 60
}
if (template.indexOf('s') >= 0) {
if (seconds > 0) {
template = template.replace('s', String(seconds))
} else {
template = template.replace(/\|[^|]*?s[^|]*?(?=\|)/, '')
}
}
return template.replace(/\|/g, '')
}

// console.log(formatDateDiff(new Date('2018-1-3'), new Date('2019-1-4'), 'd日'))
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export { default as flexible } from './flexible'
export { default as forOwn } from './forOwn'
export { default as formatCurrency } from './formatCurrency'
export { default as formatDate } from './formatDate'
export { default as formatDateDiff } from './formatDateDiff'
export { default as get } from './get'
export { default as getType } from './getType'
export { default as has } from './has'
Expand Down
12 changes: 12 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1570,3 +1570,15 @@ describe('toUnixTimestamp', () => {
})
})
})

describe('formatDateDiff', () => {
test('ok', () => {
[
['2019-1-3', '2019-1-4', 'y年|m月|d日|h时|i分|s秒', '1日'],
['2018-1-3', '2019-1-4', 'y年|m月|d日|h时|i分|s秒', '1年1日'],
// ['2018-1-3', '2019-1-4', 'd日', '366日'],
].map(([startDate, endDate, template, result]) => {
expect(vtils.formatDateDiff(startDate, endDate, template)).toBe(result)
})
})
})
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"strict": true,
"strictNullChecks": false,
"target": "es5",
"module": "ESNext",
"module": "esnext",
"moduleResolution": "node",
"declaration": true,
"removeComments": false,
Expand Down
Loading

0 comments on commit 0a283d4

Please sign in to comment.