|
| 1 | +{/* Copyright 2020 Adobe. All rights reserved. |
| 2 | +This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 3 | +you may not use this file except in compliance with the License. You may obtain a copy |
| 4 | +of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 5 | +Unless required by applicable law or agreed to in writing, software distributed under |
| 6 | +the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 7 | +OF ANY KIND, either express or implied. See the License for the specific language |
| 8 | +governing permissions and limitations under the License. */} |
| 9 | + |
| 10 | +import {Layout} from '@react-spectrum/docs'; |
| 11 | +export default Layout; |
| 12 | + |
| 13 | +import docs from 'docs:@internationalized/date'; |
| 14 | +import {HeaderInfo, FunctionAPI, ClassAPI, TypeContext, InterfaceType, TypeLink, PageDescription, renderHTMLfromMarkdown} from '@react-spectrum/docs'; |
| 15 | +import packageData from '@internationalized/date/package.json'; |
| 16 | +import tableStyles from '@adobe/spectrum-css-temp/components/table/vars.css'; |
| 17 | +import styles from '@react-spectrum/docs/src/docs.css'; |
| 18 | +import typographyStyles from '@adobe/spectrum-css-temp/components/typography/vars.css'; |
| 19 | + |
| 20 | +--- |
| 21 | +category: Date and Time |
| 22 | +keywords: [date, calendar, internationalization] |
| 23 | +after_version: 3.0.0 |
| 24 | +--- |
| 25 | + |
| 26 | +# Calendar |
| 27 | + |
| 28 | +<PageDescription>{docs.exports.Calendar.description}</PageDescription> |
| 29 | + |
| 30 | +<HeaderInfo |
| 31 | + packageData={packageData} |
| 32 | + componentNames={['Calendar']} |
| 33 | + sourceData={[]} /> |
| 34 | + |
| 35 | +## Introduction |
| 36 | + |
| 37 | +While the Gregorian calendar is the most commonly used, many other calendar systems are used throughout the world. The `Calendar` interface is used to represent calendar systems in the `@internationalized/date` library. It encapsulates information such as the number of days in a month, the number of months in a year, and the list of eras in a calendar system, as well as methods that handle correct arithmetic of dates in that calendar system, as well as converting dates between calendar systems. Many implementations of this interface are provided in `@internationalized/date` to handle the most commonly used calendar systems. |
| 38 | + |
| 39 | +As described in the docs for [CalendarDate](CalendarDate.html#calendar-systems) and other date objects, you can pass a `Calendar` instance to a date to represent a date in that calendar. Date manipulation follows the rules defined by that calendar system. You can also convert between calendar systems using the <TypeLink links={docs.links} type={docs.exports.toCalendar} /> function. |
| 40 | + |
| 41 | +```tsx |
| 42 | +import {HebrewCalendar, GregorianCalendar, toCalendar} from '@internationalized/date'; |
| 43 | + |
| 44 | +let hebrewDate = new CalendarDate(new HebrewCalendar(), 5781, 1, 1); |
| 45 | +toCalendar(hebrewDate, new GregorianCalendar()); |
| 46 | +// => new CalendarDate(new GregorianCalendar(), 2020, 9, 19); |
| 47 | +``` |
| 48 | + |
| 49 | +### Calendar identifiers |
| 50 | + |
| 51 | +While it is possible to construct `Calendar` objects manually, a common usecase is to get a calendar object for a certain locale. Each calendar has an associated string identifier that can be used to retrieve an instance of that calendar using the <TypeLink links={docs.links} type={docs.exports.createCalendar} /> function. A list of supported calendar identifiers is available [below](#implementations). |
| 52 | + |
| 53 | +```tsx |
| 54 | +import {createCalendar} from '@internationalized/date'; |
| 55 | + |
| 56 | +createCalendar('gregory'); |
| 57 | +createCalendar('hebrew'); |
| 58 | +createCalendar('japanese'); |
| 59 | +``` |
| 60 | + |
| 61 | +Locales are typically represented as strings such as `en-US`, and represent information about a user's preferences, such as language, script, number format, and calendar. Most of this is automatically determined based on data, but it can also be provided in the locale string itself via a [locale extension](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/calendar#adding_a_calendar_in_the_locale_string). For example, the locale `"hi-IN-u-ca-indian"` represents the Hindi language, in the country of India, using the `indian` calendar. |
| 62 | + |
| 63 | +The [Intl.DateTimeFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat) object can be used to get the calendar identifier from a locale string, either provided explicitly or implicitly. This can then be passed to the `createCalendar` function to retrieve a `Calendar` instance. |
| 64 | + |
| 65 | +```tsx |
| 66 | +// Get the calendar identifier for the current user. |
| 67 | +let calendarIdentifier = new Intl.DateTimeFormat().resolvedOptions().calendar; // e.g. 'gregory' |
| 68 | +createCalendar(calendarIdentifier); // new GregorianCalendar() |
| 69 | + |
| 70 | +// Language and region provided, calendar inferred. |
| 71 | +let calendarIdentifier = new Intl.DateTimeFormat('th-TH').resolvedOptions().calendar; // 'buddhist' |
| 72 | +createCalendar(calendarIdentifier); // new BuddhistCalendar() |
| 73 | + |
| 74 | +// Calendar system set explicitly. |
| 75 | +let calendarIdentifier = new Intl.DateTimeFormat('hi-IN-u-ca-indian').resolvedOptions().calendar; // 'indian' |
| 76 | +createCalendar(calendarIdentifier); // new IndianCalendar() |
| 77 | +``` |
| 78 | + |
| 79 | +**Note**: importing `createCalendar` into your project will result in all available calendars being included in your bundle. If you wish to limit the supported calendars to reduce bundle sizes, you can create your own implementation that only imports the desired classes. This way, your bundler can tree-shake the unused calendar implementations. |
| 80 | + |
| 81 | +```tsx |
| 82 | +import {GregorianCalendar, JapaneseCalendar} from '@internationalized/date'; |
| 83 | + |
| 84 | +function createCalendar(identifier) { |
| 85 | + switch (identifier) { |
| 86 | + case 'gregory': |
| 87 | + return new GregorianCalendar(); |
| 88 | + case 'japanese': |
| 89 | + return new JapaneseCalendar(); |
| 90 | + default: |
| 91 | + throw new Error(`Unsupported calendar ${identifier}`); |
| 92 | + } |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +## Implementations |
| 97 | + |
| 98 | +<table className={`${tableStyles['spectrum-Table']} ${tableStyles['spectrum-Table--quiet']} ${styles.propTable}`}> |
| 99 | + <thead> |
| 100 | + <tr> |
| 101 | + <th className={tableStyles['spectrum-Table-headCell']}>Class</th> |
| 102 | + <th className={tableStyles['spectrum-Table-headCell']}>Identifier</th> |
| 103 | + <th className={tableStyles['spectrum-Table-headCell']}>Description</th> |
| 104 | + </tr> |
| 105 | + </thead> |
| 106 | + <tbody className={tableStyles['spectrum-Table-body']}> |
| 107 | + <tr className={tableStyles['spectrum-Table-row']}> |
| 108 | + <td className={tableStyles['spectrum-Table-cell']}><TypeLink links={docs.links} type={docs.exports.GregorianCalendar} /></td> |
| 109 | + <td className={tableStyles['spectrum-Table-cell']}> |
| 110 | + <code className={typographyStyles['spectrum-Code4']}> |
| 111 | + <span className="token hljs-string">'gregory'</span> |
| 112 | + </code> |
| 113 | + </td> |
| 114 | + <td className={tableStyles['spectrum-Table-cell']}>{docs.exports.GregorianCalendar.description}</td> |
| 115 | + </tr> |
| 116 | + <tr className={tableStyles['spectrum-Table-row']}> |
| 117 | + <td className={tableStyles['spectrum-Table-cell']}><TypeLink links={docs.links} type={docs.exports.BuddhistCalendar} /></td> |
| 118 | + <td className={tableStyles['spectrum-Table-cell']}> |
| 119 | + <code className={typographyStyles['spectrum-Code4']}> |
| 120 | + <span className="token hljs-string">'buddhist'</span> |
| 121 | + </code> |
| 122 | + </td> |
| 123 | + <td className={tableStyles['spectrum-Table-cell']}>{docs.exports.BuddhistCalendar.description}</td> |
| 124 | + </tr> |
| 125 | + <tr className={tableStyles['spectrum-Table-row']}> |
| 126 | + <td className={tableStyles['spectrum-Table-cell']}><TypeLink links={docs.links} type={docs.exports.EthiopicCalendar} /></td> |
| 127 | + <td className={tableStyles['spectrum-Table-cell']}> |
| 128 | + <code className={typographyStyles['spectrum-Code4']}> |
| 129 | + <span className="token hljs-string">'ethiopic'</span> |
| 130 | + </code> |
| 131 | + </td> |
| 132 | + <td className={tableStyles['spectrum-Table-cell']}>{docs.exports.EthiopicCalendar.description}</td> |
| 133 | + </tr> |
| 134 | + <tr className={tableStyles['spectrum-Table-row']}> |
| 135 | + <td className={tableStyles['spectrum-Table-cell']}><TypeLink links={docs.links} type={docs.exports.EthiopicAmeteAlemCalendar} /></td> |
| 136 | + <td className={tableStyles['spectrum-Table-cell']}> |
| 137 | + <code className={typographyStyles['spectrum-Code4']}> |
| 138 | + <span className="token hljs-string">'ethioaa'</span> |
| 139 | + </code> |
| 140 | + </td> |
| 141 | + <td className={tableStyles['spectrum-Table-cell']}>{docs.exports.EthiopicAmeteAlemCalendar.description}</td> |
| 142 | + </tr> |
| 143 | + <tr className={tableStyles['spectrum-Table-row']}> |
| 144 | + <td className={tableStyles['spectrum-Table-cell']}><TypeLink links={docs.links} type={docs.exports.CopticCalendar} /></td> |
| 145 | + <td className={tableStyles['spectrum-Table-cell']}> |
| 146 | + <code className={typographyStyles['spectrum-Code4']}> |
| 147 | + <span className="token hljs-string">'coptic'</span> |
| 148 | + </code> |
| 149 | + </td> |
| 150 | + <td className={tableStyles['spectrum-Table-cell']}>{docs.exports.CopticCalendar.description}</td> |
| 151 | + </tr> |
| 152 | + <tr className={tableStyles['spectrum-Table-row']}> |
| 153 | + <td className={tableStyles['spectrum-Table-cell']}><TypeLink links={docs.links} type={docs.exports.HebrewCalendar} /></td> |
| 154 | + <td className={tableStyles['spectrum-Table-cell']}> |
| 155 | + <code className={typographyStyles['spectrum-Code4']}> |
| 156 | + <span className="token hljs-string">'hebrew'</span> |
| 157 | + </code> |
| 158 | + </td> |
| 159 | + <td className={tableStyles['spectrum-Table-cell']}>{docs.exports.HebrewCalendar.description}</td> |
| 160 | + </tr> |
| 161 | + <tr className={tableStyles['spectrum-Table-row']}> |
| 162 | + <td className={tableStyles['spectrum-Table-cell']}><TypeLink links={docs.links} type={docs.exports.IndianCalendar} /></td> |
| 163 | + <td className={tableStyles['spectrum-Table-cell']}> |
| 164 | + <code className={typographyStyles['spectrum-Code4']}> |
| 165 | + <span className="token hljs-string">'indian'</span> |
| 166 | + </code> |
| 167 | + </td> |
| 168 | + <td className={tableStyles['spectrum-Table-cell']}>{docs.exports.IndianCalendar.description}</td> |
| 169 | + </tr> |
| 170 | + <tr className={tableStyles['spectrum-Table-row']}> |
| 171 | + <td className={tableStyles['spectrum-Table-cell']}><TypeLink links={docs.links} type={docs.exports.IslamicCivilCalendar} /></td> |
| 172 | + <td className={tableStyles['spectrum-Table-cell']}> |
| 173 | + <code className={typographyStyles['spectrum-Code4']}> |
| 174 | + <span className="token hljs-string">'islamic-civil'</span> |
| 175 | + </code> |
| 176 | + </td> |
| 177 | + <td className={tableStyles['spectrum-Table-cell']}>{renderHTMLfromMarkdown(docs.exports.IslamicCivilCalendar.description)}</td> |
| 178 | + </tr> |
| 179 | + <tr className={tableStyles['spectrum-Table-row']}> |
| 180 | + <td className={tableStyles['spectrum-Table-cell']}><TypeLink links={docs.links} type={docs.exports.IslamicTabularCalendar} /></td> |
| 181 | + <td className={tableStyles['spectrum-Table-cell']}> |
| 182 | + <code className={typographyStyles['spectrum-Code4']}> |
| 183 | + <span className="token hljs-string">'islamic-tbla'</span> |
| 184 | + </code> |
| 185 | + </td> |
| 186 | + <td className={tableStyles['spectrum-Table-cell']}>{renderHTMLfromMarkdown(docs.exports.IslamicTabularCalendar.description)}</td> |
| 187 | + </tr> |
| 188 | + <tr className={tableStyles['spectrum-Table-row']}> |
| 189 | + <td className={tableStyles['spectrum-Table-cell']}><TypeLink links={docs.links} type={docs.exports.IslamicUmalquraCalendar} /></td> |
| 190 | + <td className={tableStyles['spectrum-Table-cell']}> |
| 191 | + <code className={typographyStyles['spectrum-Code4']}> |
| 192 | + <span className="token hljs-string">'islamic-umalqura'</span> |
| 193 | + </code> |
| 194 | + </td> |
| 195 | + <td className={tableStyles['spectrum-Table-cell']}>{renderHTMLfromMarkdown(docs.exports.IslamicUmalquraCalendar.description)}</td> |
| 196 | + </tr> |
| 197 | + <tr className={tableStyles['spectrum-Table-row']}> |
| 198 | + <td className={tableStyles['spectrum-Table-cell']}><TypeLink links={docs.links} type={docs.exports.JapaneseCalendar} /></td> |
| 199 | + <td className={tableStyles['spectrum-Table-cell']}> |
| 200 | + <code className={typographyStyles['spectrum-Code4']}> |
| 201 | + <span className="token hljs-string">'japanese'</span> |
| 202 | + </code> |
| 203 | + </td> |
| 204 | + <td className={tableStyles['spectrum-Table-cell']}>{renderHTMLfromMarkdown(docs.exports.JapaneseCalendar.description)}</td> |
| 205 | + </tr> |
| 206 | + <tr className={tableStyles['spectrum-Table-row']}> |
| 207 | + <td className={tableStyles['spectrum-Table-cell']}><TypeLink links={docs.links} type={docs.exports.PersianCalendar} /></td> |
| 208 | + <td className={tableStyles['spectrum-Table-cell']}> |
| 209 | + <code className={typographyStyles['spectrum-Code4']}> |
| 210 | + <span className="token hljs-string">'persian'</span> |
| 211 | + </code> |
| 212 | + </td> |
| 213 | + <td className={tableStyles['spectrum-Table-cell']}>{renderHTMLfromMarkdown(docs.exports.PersianCalendar.description)}</td> |
| 214 | + </tr> |
| 215 | + <tr className={tableStyles['spectrum-Table-row']}> |
| 216 | + <td className={tableStyles['spectrum-Table-cell']}><TypeLink links={docs.links} type={docs.exports.TaiwanCalendar} /></td> |
| 217 | + <td className={tableStyles['spectrum-Table-cell']}> |
| 218 | + <code className={typographyStyles['spectrum-Code4']}> |
| 219 | + <span className="token hljs-string">'roc'</span> |
| 220 | + </code> |
| 221 | + </td> |
| 222 | + <td className={tableStyles['spectrum-Table-cell']}>{renderHTMLfromMarkdown(docs.exports.TaiwanCalendar.description)}</td> |
| 223 | + </tr> |
| 224 | + </tbody> |
| 225 | +</table> |
| 226 | + |
| 227 | +## Interface |
| 228 | + |
| 229 | +<ClassAPI links={docs.links} class={docs.exports.Calendar} /> |
0 commit comments