Skip to content
This repository has been archived by the owner on Sep 1, 2022. It is now read-only.

Commit

Permalink
Use Date.toLocaleString instead of an array of month names
Browse files Browse the repository at this point in the history
  • Loading branch information
sndrs committed Aug 23, 2022
1 parent 1e9c9fb commit 1a504be
Showing 1 changed file with 3 additions and 47 deletions.
50 changes: 3 additions & 47 deletions src/datetime/timeAgo.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,5 @@
type Unit = 's' | 'm' | 'h' | 'd';

const shortMonth = (monthIndex: number): string => {
const month = [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec',
][monthIndex];

if (month) {
return month;
}

throw new Error(`Invalid month index: ${monthIndex}`);
};

const longMonth = (monthIndex: number): string => {
const month = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
][monthIndex];

if (month) {
return month;
}

throw new Error(`Invalid month index: ${monthIndex}`);
};

const pad = (n: number): number | string => n.toString().padStart(2, '0');

const isWithin24Hours = (date: Date): boolean => {
Expand Down Expand Up @@ -151,7 +105,9 @@ export const timeAgo = (
// Simple date - "9 Nov 2019"
return [
then.getDate(),
verbose ? longMonth(then.getMonth()) : shortMonth(then.getMonth()),
verbose
? then.toLocaleString('default', { month: 'long' })
: then.toLocaleString('default', { month: 'short' }),
then.getFullYear(),
].join(' ');
}
Expand Down

0 comments on commit 1a504be

Please sign in to comment.