|
| 1 | +/*** |
| 2 | +Bindings to JavaScript's `Intl.DateTimeFormat`. |
| 3 | +*/ |
| 4 | + |
1 | 5 | @notUndefined |
2 | 6 | type t |
3 | 7 |
|
@@ -106,22 +110,110 @@ type dateTimeRangePart = { |
106 | 110 | source: dateTimeRangeSource, |
107 | 111 | } |
108 | 112 |
|
| 113 | +/** |
| 114 | +Creates a new `Intl.DateTimeFormat` instance for formatting date values. |
| 115 | +
|
| 116 | +See [`Intl.DateTimeFormat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat) on MDN. |
| 117 | +
|
| 118 | +## Examples |
| 119 | +
|
| 120 | +```rescript |
| 121 | +let formatter = Intl.DateTimeFormat.make(~locales=["en-US"], ~options={timeStyle: #short}) |
| 122 | +let sampleDate = Js.Date.makeWithYMD(~year=2024, ~month=0, ~date=1) |
| 123 | +formatter->Intl.DateTimeFormat.format(sampleDate)->String.length > 0 |
| 124 | +``` |
| 125 | +*/ |
109 | 126 | @new external make: (~locales: array<string>=?, ~options: options=?) => t = "Intl.DateTimeFormat" |
110 | 127 |
|
| 128 | +/** |
| 129 | +`supportedLocalesOf(locales, ~options)` filters `locales` to those supported by the runtime for date/time formatting. |
| 130 | +
|
| 131 | +See [`Intl.DateTimeFormat.supportedLocalesOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/supportedLocalesOf) on MDN. |
| 132 | +
|
| 133 | +## Examples |
| 134 | +
|
| 135 | +```rescript |
| 136 | +Intl.DateTimeFormat.supportedLocalesOf(["en-US", "klingon"]) == ["en-US"] |
| 137 | +``` |
| 138 | +*/ |
111 | 139 | @val |
112 | | -external supportedLocalesOf: (array<string>, ~options: supportedLocalesOptions=?) => t = |
| 140 | +external supportedLocalesOf: (array<string>, ~options: supportedLocalesOptions=?) => array<string> = |
113 | 141 | "Intl.DateTimeFormat.supportedLocalesOf" |
114 | 142 |
|
| 143 | +/** |
| 144 | +`resolvedOptions(formatter)` returns the actual locale and formatting options in use. |
| 145 | +
|
| 146 | +See [`Intl.DateTimeFormat.prototype.resolvedOptions`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/resolvedOptions) on MDN. |
| 147 | +
|
| 148 | +## Examples |
| 149 | +
|
| 150 | +```rescript |
| 151 | +let formatter = Intl.DateTimeFormat.make(~locales=["en-US"]) |
| 152 | +Intl.DateTimeFormat.resolvedOptions(formatter).locale == "en-US" |
| 153 | +``` |
| 154 | +*/ |
115 | 155 | @send external resolvedOptions: t => resolvedOptions = "resolvedOptions" |
116 | 156 |
|
| 157 | +/** |
| 158 | +`format(formatter, date)` returns the formatted string for `date`. |
| 159 | +
|
| 160 | +## Examples |
| 161 | +
|
| 162 | +```rescript |
| 163 | +let formatter = Intl.DateTimeFormat.make(~locales=["en"]) |
| 164 | +let date = Js.Date.makeWithYMD(~year=2024, ~month=0, ~date=1) |
| 165 | +formatter->Intl.DateTimeFormat.format(date)->String.length > 0 |
| 166 | +``` |
| 167 | +*/ |
117 | 168 | @send external format: (t, Stdlib_Date.t) => string = "format" |
118 | | -@send |
119 | | -external formatToParts: (t, Stdlib_Date.t) => array<dateTimePart> = "formatToParts" |
120 | 169 |
|
| 170 | +/** |
| 171 | +`formatToParts(formatter, date)` breaks the formatted output into an array of parts. |
| 172 | +
|
| 173 | +See [`Intl.DateTimeFormat.prototype.formatToParts`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/formatToParts) on MDN. |
| 174 | +
|
| 175 | +## Examples |
| 176 | +
|
| 177 | +```rescript |
| 178 | +let formatter = Intl.DateTimeFormat.make(~locales=["en"]) |
| 179 | +let date = Js.Date.makeWithYMD(~year=2024, ~month=0, ~date=1) |
| 180 | +formatter->Intl.DateTimeFormat.formatToParts(date)->Array.length > 0 |
| 181 | +``` |
| 182 | +*/ |
| 183 | +@send external formatToParts: (t, Stdlib_Date.t) => array<dateTimePart> = "formatToParts" |
| 184 | + |
| 185 | +/** |
| 186 | +`formatRange(formatter, ~startDate, ~endDate)` formats the range between `startDate` and `endDate`. |
| 187 | +
|
| 188 | +See [`Intl.DateTimeFormat.prototype.formatRange`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/formatRange) on MDN. |
| 189 | +
|
| 190 | +## Examples |
| 191 | +
|
| 192 | +```rescript |
| 193 | +let formatter = Intl.DateTimeFormat.make(~locales=["en-US"], ~options={dateStyle: #short}) |
| 194 | +let startDate = Js.Date.makeWithYMD(~year=2024, ~month=0, ~date=1) |
| 195 | +let endDate = Js.Date.makeWithYMD(~year=2024, ~month=1, ~date=1) |
| 196 | +formatter->Intl.DateTimeFormat.formatRange(~startDate=startDate, ~endDate=endDate)->String.length > 0 |
| 197 | +``` |
| 198 | +*/ |
121 | 199 | @send |
122 | 200 | external formatRange: (t, ~startDate: Stdlib_Date.t, ~endDate: Stdlib_Date.t) => string = |
123 | 201 | "formatRange" |
124 | 202 |
|
| 203 | +/** |
| 204 | +`formatRangeToParts(formatter, ~startDate, ~endDate)` returns an array describing how the range would be rendered. |
| 205 | +
|
| 206 | +See [`Intl.DateTimeFormat.prototype.formatRangeToParts`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/formatRangeToParts) on MDN. |
| 207 | +
|
| 208 | +## Examples |
| 209 | +
|
| 210 | +```rescript |
| 211 | +let formatter = Intl.DateTimeFormat.make(~locales=["en-US"], ~options={dateStyle: #short}) |
| 212 | +let startDate = Js.Date.makeWithYMD(~year=2024, ~month=0, ~date=1) |
| 213 | +let endDate = Js.Date.makeWithYMD(~year=2024, ~month=1, ~date=1) |
| 214 | +formatter->Intl.DateTimeFormat.formatRangeToParts(~startDate=startDate, ~endDate=endDate)->Array.length > 0 |
| 215 | +``` |
| 216 | +*/ |
125 | 217 | @send |
126 | 218 | external formatRangeToParts: ( |
127 | 219 | t, |
|
0 commit comments