Skip to content

Commit

Permalink
Refactor serializeDateForFileName
Browse files Browse the repository at this point in the history
  • Loading branch information
JiriLojda committed Jan 7, 2025
1 parent d3bfb97 commit 368365a
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/utils/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ export enum DateLevel {
}

export const serializeDateForFileName = (date: Date, level: DateLevel) =>
[
date.getUTCFullYear(),
...level >= DateLevel.Month ? [("0" + (date.getUTCMonth() + 1)).slice(-2)] : [],
...level >= DateLevel.Day ? [("0" + date.getUTCDate()).slice(-2)] : [],
...level >= DateLevel.Hour ? [("0" + date.getUTCHours()).slice(-2)] : [],
...level >= DateLevel.Minute ? [("0" + date.getUTCMinutes()).slice(-2)] : [],
...level >= DateLevel.Second ? [("0" + date.getUTCSeconds()).slice(-2)] : [],
].join("-");
createDateParts(date)
.slice(0, level + 1)
.join("-");

const createDateParts = (date: Date) => Object.values({
[DateLevel.Year]: date.getUTCFullYear().toString(),
[DateLevel.Month]: ("0" + (date.getUTCMonth() + 1)).slice(-2),
[DateLevel.Day]: ("0" + date.getUTCDate()).slice(-2),
[DateLevel.Hour]: ("0" + date.getUTCHours()).slice(-2),
[DateLevel.Minute]: ("0" + date.getUTCMinutes()).slice(-2),
[DateLevel.Second]: ("0" + date.getUTCSeconds()).slice(-2),
} as const satisfies Record<DateLevel, string>);

0 comments on commit 368365a

Please sign in to comment.