lyb-js 1.6.29 → 1.6.30

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.
@@ -1,4 +1,5 @@
1
- export declare const libJsCountdown: (endTime: number) => {
1
+ /** @description 倒计时,keepUnit 为保留单位,不再使用其上的进制 */
2
+ export declare const libJsCountdown: (endTime: number, keepUnit: "year" | "month" | "day" | "hour" | "minute" | "second") => {
2
3
  years: string;
3
4
  months: string;
4
5
  days: string;
@@ -1,18 +1,49 @@
1
1
  import dayjs from "dayjs";
2
2
  import duration from "dayjs/plugin/duration";
3
3
  dayjs.extend(duration);
4
- export const libJsCountdown = (endTime) => {
4
+ /** @description 倒计时,keepUnit 为保留单位,不再使用其上的进制 */
5
+ export const libJsCountdown = (endTime, keepUnit) => {
5
6
  const startTime = dayjs();
6
7
  const diff = dayjs(endTime).diff(startTime);
7
8
  const time = dayjs.duration(diff);
8
9
  const pad = (n) => n.toString().padStart(2, "0");
10
+ if (diff <= 0) {
11
+ return {
12
+ years: "00",
13
+ months: "00",
14
+ days: "00",
15
+ hours: "00",
16
+ minutes: "00",
17
+ seconds: "00",
18
+ ended: true,
19
+ };
20
+ }
21
+ // 先获取原始值
22
+ const years = time.years();
23
+ const months = time.months();
24
+ const days = time.days();
25
+ let hours = time.hours();
26
+ let minutes = time.minutes();
27
+ let seconds = time.seconds();
28
+ // 将整个时长打平成秒
29
+ const totalSec = time.asSeconds();
30
+ // 根据保留单位展开
31
+ if (keepUnit === "hour") {
32
+ hours = Math.floor(totalSec / 3600); // total hours
33
+ minutes = Math.floor((totalSec % 3600) / 60);
34
+ seconds = Math.floor(totalSec % 60);
35
+ }
36
+ else if (keepUnit === "minute") {
37
+ minutes = Math.floor(totalSec / 60); // total minutes
38
+ seconds = Math.floor(totalSec % 60);
39
+ }
9
40
  return {
10
- years: diff <= 0 ? "00" : pad(time.years()),
11
- months: diff <= 0 ? "00" : pad(time.months()),
12
- days: diff <= 0 ? "00" : pad(time.days()),
13
- hours: diff <= 0 ? "00" : pad(time.hours()),
14
- minutes: diff <= 0 ? "00" : pad(time.minutes()),
15
- seconds: diff <= 0 ? "00" : pad(time.seconds()),
16
- ended: diff <= 0,
41
+ years: pad(years),
42
+ months: pad(months),
43
+ days: pad(days),
44
+ hours: pad(hours),
45
+ minutes: pad(minutes),
46
+ seconds: pad(seconds),
47
+ ended: false,
17
48
  };
18
49
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lyb-js",
3
- "version": "1.6.29",
3
+ "version": "1.6.30",
4
4
  "description": "自用JS方法库",
5
5
  "license": "ISC",
6
6
  "type": "module",