source: frontend/src/utils.ts

main
Last change on this file was 700e2f9, checked in by 186079 <matej.milevski@…>, 5 days ago

Init

  • Property mode set to 100644
File size: 1.7 KB
Line 
1export const formatDate = (dateString: string | Date) =>
2 new Date(dateString).toLocaleDateString("en-US", {
3 year: "numeric",
4 month: "long",
5 day: "numeric",
6 });
7
8export const formatDateWithWeekday = (dateString: string | Date) =>
9 new Date(dateString).toLocaleDateString("en-US", {
10 weekday: "short",
11 year: "numeric",
12 month: "short",
13 day: "numeric",
14 });
15
16export const formatDateWithLongWeekday = (dateString: string | Date) =>
17 new Date(dateString).toLocaleDateString("en-US", {
18 weekday: "long",
19 year: "numeric",
20 month: "long",
21 day: "numeric",
22 });
23
24export const formatDateTime = (dateString: string | Date) =>
25 new Date(dateString).toLocaleString("en-US", {
26 year: "numeric",
27 month: "short",
28 day: "numeric",
29 hour: "2-digit",
30 minute: "2-digit",
31 });
32
33export const getMonthName = (year: number, month: number) =>
34 new Date(year, month - 1, 1).toLocaleDateString("en-US", {
35 month: "long",
36 year: "numeric",
37 });
38
39export const dateToString = (date: Date): string => {
40 const year = date.getFullYear();
41 const month = String(date.getMonth() + 1).padStart(2, "0");
42 const day = String(date.getDate()).padStart(2, "0");
43
44 return `${year}-${month}-${day}`;
45};
46
47export const getTodayString = (): string => dateToString(new Date());
48
49export const isFutureDate = (date: Date | null): boolean => {
50 if (!date) return false;
51 const dateStr = dateToString(date);
52 const todayStr = getTodayString();
53 return dateStr > todayStr;
54};
55
56export const isTodayDate = (date: Date | null): boolean => {
57 if (!date) return false;
58 const dateStr = dateToString(date);
59 const todayStr = getTodayString();
60 return dateStr === todayStr;
61};
Note: See TracBrowser for help on using the repository browser.