@@ -4,12 +4,6 @@ import { Timestamp } from "google-protobuf/google/protobuf/timestamp_pb";
44import daysjs , { Dayjs } from "./dayjs" ;
55import { dayMillis } from "./timeAgo" ;
66
7- const monthFormatter = ( locale : string ) =>
8- new Intl . DateTimeFormat ( locale , {
9- month : "short" ,
10- year : "numeric" ,
11- } ) ;
12-
137const numNights = ( date1 : string , date2 : string ) => {
148 const diffTime = Date . parse ( date1 ) - Date . parse ( date2 ) ;
159 const diffDays = Math . ceil ( diffTime / dayMillis ) ;
@@ -27,6 +21,8 @@ interface LocalizeDateTimeParams {
2721 timezone ?: string | typeof BROWSER_TIMEZONE ;
2822 /// Whether to include the date (defaults to true).
2923 includeDate ?: boolean ;
24+ /// If including the date, whether to include the day (defaults to true).
25+ includeDay ?: boolean ;
3026 /// If including the date, whether to include the day of week (defaults to false).
3127 includeDayOfWeek ?: boolean ;
3228 /// Whether to include the time (defaults to true).
@@ -49,6 +45,24 @@ export function localizeDateTime(
4945 return format . format ( date ) ;
5046}
5147
48+ /// Localizes only the year and month of a date.
49+ export function localizeYearMonth (
50+ date : Date | Dayjs ,
51+ args : {
52+ timezone ?: string | typeof BROWSER_TIMEZONE ;
53+ locale : string ;
54+ abbreviate ?: boolean ;
55+ } ,
56+ ) : string {
57+ return localizeDateTime ( date , {
58+ timezone : args . timezone ,
59+ locale : args . locale ,
60+ abbreviate : args . abbreviate ,
61+ includeDay : false ,
62+ includeTime : false ,
63+ } ) ;
64+ }
65+
5266/// Localizes a range of date and times as a string.
5367export function localizeDateTimeRange (
5468 start : Date | Dayjs ,
@@ -93,7 +107,9 @@ function createIntlDateTimeFormat(
93107 if ( args . includeDate !== false ) {
94108 options . year = "numeric" ;
95109 options . month = args . abbreviate ? "short" : "long" ;
96- options . day = "numeric" ;
110+ if ( args . includeDay !== false ) {
111+ options . day = "numeric" ;
112+ }
97113 if ( args . includeDayOfWeek ) {
98114 options . weekday = args . abbreviate ? "short" : "long" ;
99115 }
@@ -128,4 +144,4 @@ function isSameOrFutureDate(date1: Dayjs, date2: Dayjs): boolean {
128144 return isSameDate ( date1 , date2 ) || date1 . isAfter ( date2 ) ;
129145}
130146
131- export { isSameOrFutureDate , monthFormatter , numNights , timestamp2Date } ;
147+ export { isSameOrFutureDate , numNights , timestamp2Date } ;
0 commit comments