silverbullet/lib/dates.ts

23 lines
501 B
TypeScript
Raw Normal View History

2022-04-12 02:34:09 +08:00
export function niceDate(d: Date): string {
2022-10-07 06:04:12 +08:00
function pad(n: number) {
let s = String(n);
if (s.length === 1) {
2022-10-12 17:47:13 +08:00
s = "0" + s;
2022-10-07 06:04:12 +08:00
}
return s;
}
2022-10-12 17:47:13 +08:00
return d.getFullYear() + "-" + pad(d.getMonth() + 1) + "-" + pad(d.getDate());
2022-04-12 02:34:09 +08:00
}
export function niceTime(d: Date): string {
const isoDate = d.toISOString();
2024-07-27 17:03:42 +08:00
const [_, time] = isoDate.split("T");
// hh:mm:ss
return time.split(".")[0];
}
2024-07-27 17:03:42 +08:00
export function safeTime(d: Date): string {
return niceTime(d).replace(/:/g, "-");
}