add toShortTime

This commit is contained in:
Timi
2026-04-12 10:40:20 +08:00
parent 6735a815c4
commit 1adf3eeb6d
2 changed files with 13 additions and 1 deletions

View File

@@ -2,6 +2,12 @@ import { describe, expect, it } from "vitest";
import Time from "./Time";
describe("Time", () => {
describe("toShortTime", () => {
it("test toShortTime", () => {
const date = new Date("2027-01-03T04:05:06.789Z");
expect(Time.toShortTime(date.getTime())).toBe("05:06");
});
});
describe("between", () => {
it("should return time segments between begin and end", () => {
const begin = new Date("2026-01-01T00:00:00.000Z");

View File

@@ -37,6 +37,12 @@ export default class Time {
return `${d.getHours().toString().padStart(2, "0")}:${d.getMinutes().toString().padStart(2, "0")}`;
}
public static toShortTime(unix?: number): string {
if (!unix) return "";
const d = new Date(unix);
return `${d.getMinutes().toString().padStart(2, "0")}:${d.getSeconds().toString().padStart(2, "0")}`;
}
/**
* Unix 时间戳转日期和时间
*
@@ -111,7 +117,7 @@ export default class Time {
const milliseconds = remain;
const parts: string[] = [];
Toolkit.doWhere(0 < years, () => parts.push(`${years}`));
Toolkit.doWhere(0 < days, () => parts.push(`${days} `));
Toolkit.doWhere(0 < days, () => parts.push(`${days} `));
Toolkit.doWhere(0 < hours, () => parts.push(`${hours} 小时`));
Toolkit.doWhere(0 < minutes, () => parts.push(`${minutes} 分钟`));
Toolkit.doWhere(0 < seconds, () => parts.push(`${seconds}`));