Skip to content

Commit 31cd1fd

Browse files
authored
Add relative date display (#57)
* Refactor into date-utils
1 parent 266820a commit 31cd1fd

5 files changed

Lines changed: 62 additions & 43 deletions

File tree

src/app/feed/feed.page.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
}
8989
<ion-card-header>
9090
<ion-card-subtitle>
91-
{{entry.source}} | {{formatDate(entry.isoDate, settingsService.getSettings().locale)}}
91+
{{entry.source}} | {{formatDateRelative(entry.isoDate, settingsService.getSettings().locale)}}
9292
</ion-card-subtitle>
9393
<ion-card-title>
9494
<!---ToDo: We override the background as the colour is slightly different on iOS-->

src/app/feed/feed.page.ts

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { PlatformService } from '../services/platform.service';
3030
import { SettingsService } from '../services/settings.service';
3131
import { SourcesService } from '../services/sources.service';
3232
import { SettingsComponent } from '../settings/settings.component';
33+
import { formatDateAsDay, formatDateAsLong, formatDateRelative } from '../lib/date-utils';
3334

3435
@Component({
3536
selector: 'app-feed',
@@ -51,6 +52,10 @@ export class FeedPage {
5152
public filter: string = '';
5253
public currentScrollOffset: number = 0;
5354

55+
public formatDateAsDay = formatDateAsDay;
56+
public formatDateAsLong = formatDateAsLong;
57+
public formatDateRelative = formatDateRelative;
58+
5459
constructor(public sourcesService: SourcesService, public platformService: PlatformService,
5560
public bookmarkService: BookmarkService, public feedService: FeedService,
5661
private modalController: ModalController, public elementRef: ElementRef,
@@ -59,36 +64,6 @@ export class FeedPage {
5964
chevronUpOutline });
6065
}
6166

62-
public formatDate(dateStr: string | number, locale: string) {
63-
const date = new Date(dateStr);
64-
const dateOptions: Intl.DateTimeFormatOptions = {
65-
weekday: 'short',
66-
month: 'short',
67-
day: 'numeric'
68-
};
69-
return date.toLocaleDateString(locale, dateOptions);
70-
}
71-
72-
public formatDateAsDay(dateStr: string | number, locale: string) {
73-
const date = new Date(dateStr);
74-
const dateOptions: Intl.DateTimeFormatOptions = {
75-
weekday: 'short'
76-
};
77-
return date.toLocaleDateString(locale, dateOptions);
78-
}
79-
80-
public formatDateAsLong(dateStr: string | number, locale: string) {
81-
const date = new Date(dateStr);
82-
const dateOptions: Intl.DateTimeFormatOptions = {
83-
hour: 'numeric',
84-
minute: 'numeric',
85-
weekday: 'short',
86-
month: 'short',
87-
day: 'numeric'
88-
};
89-
return date.toLocaleDateString(locale, dateOptions);
90-
}
91-
9267
public scrollToTop() {
9368
this.mainFeed.scrollToTop(400);
9469
}

src/app/lib/date-utils.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// src/app/lib/date-utils.ts
2+
// Utility functions for date formatting and display
3+
4+
export function formatDate(dateStr: string | number, locale: string): string {
5+
const date = new Date(dateStr);
6+
const dateOptions: Intl.DateTimeFormatOptions = {
7+
year: 'numeric',
8+
month: 'short',
9+
day: 'numeric'
10+
};
11+
return date.toLocaleDateString(locale, dateOptions);
12+
}
13+
14+
export function formatDateAsDay(dateStr: string | number, locale: string): string {
15+
const date = new Date(dateStr);
16+
const dateOptions: Intl.DateTimeFormatOptions = {
17+
weekday: 'short'
18+
};
19+
return date.toLocaleDateString(locale, dateOptions);
20+
}
21+
22+
export function formatDateAsLong(dateStr: string | number, locale: string): string {
23+
const date = new Date(dateStr);
24+
const dateOptions: Intl.DateTimeFormatOptions = {
25+
hour: 'numeric',
26+
minute: 'numeric',
27+
weekday: 'short',
28+
month: 'short',
29+
day: 'numeric'
30+
};
31+
return date.toLocaleDateString(locale, dateOptions);
32+
}
33+
34+
export function formatDateRelative(dateStr: string | number, locale: string): string {
35+
const date = new Date(dateStr);
36+
const today = new Date();
37+
const yesterday = new Date();
38+
yesterday.setDate(today.getDate() - 1);
39+
40+
// Remove time for comparison
41+
const dateOnly = new Date(date.getFullYear(), date.getMonth(), date.getDate());
42+
const todayOnly = new Date(today.getFullYear(), today.getMonth(), today.getDate());
43+
const yesterdayOnly = new Date(yesterday.getFullYear(), yesterday.getMonth(), yesterday.getDate());
44+
45+
if (dateOnly.getTime() === todayOnly.getTime()) {
46+
return 'Today';
47+
} else if (dateOnly.getTime() === yesterdayOnly.getTime()) {
48+
return 'Yesterday';
49+
} else {
50+
return formatDate(dateStr, locale);
51+
}
52+
}

src/app/saved/saved.page.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<ion-card [button]="true" (click)="platformService.openUrlInPlatformBrowser(entry.link)">
2121
<ion-card-header>
2222
<ion-card-subtitle>
23-
{{entry.source}} | {{formatDate(entry.pubDate, "en-AU")}}
23+
{{entry.source}} | {{formatDateRelative(entry.pubDate, "en-AU")}}
2424
</ion-card-subtitle>
2525
<ion-card-title>
2626
<ion-item class="ion-no-padding">

src/app/saved/saved.page.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { addIcons } from 'ionicons';
77
import { bookmark } from 'ionicons/icons';
88
import { BookmarkService } from '../services/bookmark.service';
99
import { PlatformService } from '../services/platform.service';
10+
import { formatDateRelative } from '../lib/date-utils';
1011

1112
@Component({
1213
selector: 'app-saved',
@@ -17,21 +18,12 @@ import { PlatformService } from '../services/platform.service';
1718
IonCardSubtitle, IonCardTitle, IonButton, IonLabel, IonList, IonIcon, IonItem],
1819
})
1920
export class SavedPage {
21+
public formatDateRelative = formatDateRelative;
22+
2023
constructor(public bookmarks: BookmarkService, public platformService: PlatformService) {
2124
addIcons({ bookmark });
2225
}
2326

24-
public formatDate(dateStr: string, locale: string)
25-
{
26-
const date = new Date(dateStr);
27-
const dateOptions: Intl.DateTimeFormatOptions = {
28-
weekday: 'short',
29-
month: 'short',
30-
day: 'numeric'
31-
};
32-
return date.toLocaleDateString(locale, dateOptions);
33-
}
34-
3527
// eslint-disable-next-line @typescript-eslint/no-explicit-any
3628
public removeBookmark(event: any, entry: any) {
3729
this.bookmarks.removeEntry(entry);

0 commit comments

Comments
 (0)