mirror of
https://github.com/hcengineering/platform.git
synced 2026-09-13 13:17:45 +02:00
Add date difference calculation functionality (#10673)
* Add date difference calculation functionality Signed-off-by: Denis Bykhov <bykhov.denis@gmail.com> * Fix Signed-off-by: Denis Bykhov <bykhov.denis@gmail.com> * Fix formatting Signed-off-by: Denis Bykhov <bykhov.denis@gmail.com> --------- Signed-off-by: Denis Bykhov <bykhov.denis@gmail.com>
This commit is contained in:
@@ -246,6 +246,34 @@ export function Offset (val: Timestamp, props: Record<string, any>): Timestamp {
|
||||
return val
|
||||
}
|
||||
|
||||
export async function DateDifference (
|
||||
value: Timestamp,
|
||||
props: Record<string, any>,
|
||||
control: ProcessControl,
|
||||
execution: Execution
|
||||
): Promise<number> {
|
||||
if (typeof value !== 'number') return 0
|
||||
const otherDate = await getContextValue(props.otherDate, control, execution)
|
||||
if (typeof otherDate !== 'number') return 0
|
||||
|
||||
const diff = value - otherDate
|
||||
const v = new Date(value)
|
||||
const o = new Date(otherDate)
|
||||
switch (props.unit) {
|
||||
case 'hours':
|
||||
return Math.floor(diff / (1000 * 60 * 60))
|
||||
case 'weeks':
|
||||
return Math.floor(diff / (1000 * 60 * 60 * 24 * 7))
|
||||
case 'months':
|
||||
return (v.getUTCFullYear() - o.getUTCFullYear()) * 12 + (v.getUTCMonth() - o.getUTCMonth())
|
||||
case 'years':
|
||||
return v.getUTCFullYear() - o.getUTCFullYear()
|
||||
case 'days':
|
||||
default:
|
||||
return Math.floor(diff / (1000 * 60 * 60 * 24))
|
||||
}
|
||||
}
|
||||
|
||||
// #endregion
|
||||
|
||||
// #region Numbers
|
||||
@@ -508,4 +536,3 @@ export function DayFromDate (value: Date): number {
|
||||
}
|
||||
|
||||
// #endregion
|
||||
|
||||
|
||||
Reference in New Issue
Block a user