@purpurds/countdown 8.12.1 → 8.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/utils.ts CHANGED
@@ -1,24 +1,17 @@
1
1
  export function getDiffs(endtime: number) {
2
2
  const now = new Date().getTime();
3
- const diffInMs = Math.abs(endtime - now);
4
3
 
5
- let seconds = 0,
6
- minutes = 0,
7
- hours = 0,
8
- days = 0;
4
+ const totalSeconds = Math.max(0, endtime - now);
9
5
 
10
- if (diffInMs >= inMs.day) {
11
- days = Math.floor(diffInMs / inMs.day);
12
- }
13
- if (diffInMs >= inMs.hour) {
14
- hours = Math.floor((diffInMs % inMs.day) / inMs.hour);
15
- }
16
- if (diffInMs >= inMs.minute) {
17
- minutes = Math.floor((diffInMs % inMs.hour) / inMs.minute);
18
- }
19
- if (diffInMs >= inMs.second) {
20
- seconds = Math.floor((diffInMs % inMs.minute) / inMs.second);
21
- }
6
+ const _days = Math.floor(totalSeconds / inMs.day);
7
+ const _hours = Math.floor((totalSeconds % inMs.day) / inMs.hour);
8
+ const _minutes = Math.floor((totalSeconds % inMs.hour) / inMs.minute);
9
+ const _seconds = Math.floor((totalSeconds % inMs.minute) / inMs.second);
10
+
11
+ const days = splitDigits(_days);
12
+ const hours = splitDigits(_hours);
13
+ const minutes = splitDigits(_minutes);
14
+ const seconds = splitDigits(_seconds);
22
15
 
23
16
  return {
24
17
  seconds,
@@ -28,18 +21,18 @@ export function getDiffs(endtime: number) {
28
21
  };
29
22
  }
30
23
 
31
- export function getNext(time: number, tag: string) {
32
- let r = 0;
33
- if (tag === "seconds") r = time - 1 >= 0 ? time - 1 : 59;
34
- if (tag === "minutes") r = time - 1 >= 0 ? time - 1 : 59;
35
- if (tag === "hours") r = time - 1 >= 0 ? time - 1 : 23;
36
- if (tag === "days") r = time - 1 >= 0 ? time - 1 : 0;
37
- return r;
38
- }
39
-
40
24
  export const inMs = {
41
- day: 24 * 60 * 60 * 1000,
42
- hour: 60 * 60 * 1000,
43
- minute: 60 * 1000,
44
25
  second: 1000,
26
+ minute: 60 * 1000,
27
+ hour: 60 * 60 * 1000,
28
+ day: 24 * 60 * 60 * 1000,
45
29
  };
30
+
31
+ export function toDate(t: string | number | Date) {
32
+ return t instanceof Date ? t : new Date(t);
33
+ }
34
+
35
+ export function splitDigits(value: number) {
36
+ const s = String(value).padStart(2, "0");
37
+ return s.split("");
38
+ }