MCPcopy Index your code
hub / github.com/github/relative-time-element / elapsedTime

Function elapsedTime

src/duration.ts:107–129  ·  view source on GitHub ↗
(date: Date, precision: Unit = 'second', now = Date.now())

Source from the content-addressed store, hash-verified

105}
106
107export function elapsedTime(date: Date, precision: Unit = 'second', now = Date.now()): Duration {
108 const delta = date.getTime() - now
109 if (delta === 0) return new Duration()
110 const sign = Math.sign(delta)
111 const ms = Math.abs(delta)
112 const sec = Math.floor(ms / 1000)
113 const min = Math.floor(sec / 60)
114 const hr = Math.floor(min / 60)
115 const day = Math.floor(hr / 24)
116 const month = Math.floor(day / 30)
117 const year = Math.floor(month / 12)
118 const i = unitNames.indexOf(precision) || unitNames.length
119 return new Duration(
120 i >= 0 ? year * sign : 0,
121 i >= 1 ? (month - year * 12) * sign : 0,
122 0,
123 i >= 3 ? (day - month * 30) * sign : 0,
124 i >= 4 ? (hr - day * 24) * sign : 0,
125 i >= 5 ? (min - hr * 60) * sign : 0,
126 i >= 6 ? (sec - min * 60) * sign : 0,
127 i >= 7 ? (ms - sec * 1000) * sign : 0,
128 )
129}
130
131interface RoundingOpts {
132 relativeTo: Date | number

Callers 2

duration.tsFile · 0.90
updateMethod · 0.85

Calls 1

absMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…