|
| 1 | +# Installation |
| 2 | + |
| 3 | +Get started with date-and-time in your project using your preferred package manager. |
| 4 | + |
| 5 | +## Package Manager Installation |
| 6 | + |
| 7 | +::: code-group |
| 8 | + |
| 9 | +```bash [npm] |
| 10 | +npm install date-and-time |
| 11 | +``` |
| 12 | + |
| 13 | +```bash [yarn] |
| 14 | +yarn add date-and-time |
| 15 | +``` |
| 16 | + |
| 17 | +```bash [pnpm] |
| 18 | +pnpm add date-and-time |
| 19 | +``` |
| 20 | + |
| 21 | +::: |
| 22 | + |
| 23 | +## Requirements |
| 24 | + |
| 25 | +### Runtime Requirements |
| 26 | + |
| 27 | +- **Node.js**: Version 18.0.0 or higher |
| 28 | +- **Browsers**: ES2021 support required |
| 29 | + |
| 30 | +### Development Requirements (optional) |
| 31 | + |
| 32 | +- **TypeScript**: Version 4.5 or higher for full type support |
| 33 | +- **Modern bundler**: For optimal tree-shaking (Webpack 5+, Rollup, Vite, etc.) |
| 34 | + |
| 35 | +## Import Methods |
| 36 | + |
| 37 | +date-and-time supports both ES Modules and CommonJS, allowing you to use the import style that best fits your project. |
| 38 | + |
| 39 | +### ES Modules (Recommended) |
| 40 | + |
| 41 | +```typescript |
| 42 | +import { format, parse } from 'date-and-time'; |
| 43 | + |
| 44 | +format(new Date(), 'YYYY/MM/DD'); |
| 45 | +// => 2025/08/23 |
| 46 | +``` |
| 47 | + |
| 48 | +### CommonJS |
| 49 | + |
| 50 | +```typescript |
| 51 | +const { format, parse } = require('date-and-time'); |
| 52 | + |
| 53 | +format(new Date(), 'YYYY/MM/DD'); |
| 54 | +// => 2025/08/23 |
| 55 | +``` |
| 56 | + |
| 57 | +## Importing Locales and Timezones |
| 58 | + |
| 59 | +Locales and timezones are distributed as separate modules to support tree shaking: |
| 60 | + |
| 61 | +### Locale Imports |
| 62 | + |
| 63 | +```typescript |
| 64 | +// Import specific locales |
| 65 | +import ja from 'date-and-time/locales/ja'; |
| 66 | +import es from 'date-and-time/locales/es'; |
| 67 | +import fr from 'date-and-time/locales/fr'; |
| 68 | + |
| 69 | +// Use in formatting |
| 70 | +format(new Date(), 'YYYY年M月D日', { locale: ja }); |
| 71 | +format(new Date(), 'D [de] MMMM [de] YYYY', { locale: es }); |
| 72 | +format(new Date(), 'D MMMM YYYY', { locale: fr }); |
| 73 | +``` |
| 74 | + |
| 75 | +For a complete list of all supported locales with import examples, see [Supported Locales](../locales). |
| 76 | + |
| 77 | +### Timezone Imports |
| 78 | + |
| 79 | +```typescript |
| 80 | +// Import specific timezones |
| 81 | +import Tokyo from 'date-and-time/timezones/Asia/Tokyo'; |
| 82 | +import New_York from 'date-and-time/timezones/America/New_York'; |
| 83 | +import London from 'date-and-time/timezones/Europe/London'; |
| 84 | + |
| 85 | +// Use in operations |
| 86 | +format(new Date(), 'YYYY-MM-DD HH:mm:ss', { timeZone: Tokyo }); |
| 87 | +format(new Date(), 'YYYY-MM-DD HH:mm:ss', { timeZone: New_York }); |
| 88 | +``` |
| 89 | + |
| 90 | +For a complete list of all supported timezones with import examples, see [Supported Timezones](../timezones). |
| 91 | + |
| 92 | +### Numeral Systems |
| 93 | + |
| 94 | +```typescript |
| 95 | +// Import numeral systems |
| 96 | +import arab from 'date-and-time/numerals/arab'; |
| 97 | +import beng from 'date-and-time/numerals/beng'; |
| 98 | + |
| 99 | +format(new Date(), 'DD/MM/YYYY', { numeral: arab }); |
| 100 | +// => ٠٨/٠٧/٢٠٢٥ |
| 101 | +``` |
| 102 | + |
| 103 | +## Plugin Imports |
| 104 | + |
| 105 | +Some advanced features are available as plugins: |
| 106 | + |
| 107 | +```typescript |
| 108 | +import { format } from 'date-and-time'; |
| 109 | +// Import specific plugins |
| 110 | +import microsecond from 'date-and-time/plugins/microsecond'; |
| 111 | +import ordinal from 'date-and-time/plugins/ordinal'; |
| 112 | +import zonename from 'date-and-time/plugins/zonename'; |
| 113 | + |
| 114 | +// Use plugin-specific tokens with plugins specified in options |
| 115 | +format(new Date(), 'MMMM DDD, YYYY', { plugins: [ordinal] }); // with ordinal plugin |
| 116 | +format(new Date(), 'HH:mm:ss.SSSSSS', { plugins: [microsecond] }); // with microsecond plugin |
| 117 | +``` |
| 118 | + |
| 119 | +## CDN Usage |
| 120 | + |
| 121 | +For browser-only projects, you can use date-and-time directly from a CDN: |
| 122 | + |
| 123 | +### ES Modules via CDN |
| 124 | + |
| 125 | +```html |
| 126 | +<script type="module"> |
| 127 | + import { format } from 'https://cdn.skypack.dev/date-and-time'; |
| 128 | + |
| 129 | + console.log(format(new Date(), 'YYYY/MM/DD')); |
| 130 | +</script> |
| 131 | +``` |
| 132 | + |
| 133 | +### Via unpkg |
| 134 | + |
| 135 | +```html |
| 136 | +<script type="module"> |
| 137 | + import { format } from 'https://unpkg.com/date-and-time@4/dist/index.js'; |
| 138 | + |
| 139 | + console.log(format(new Date(), 'YYYY/MM/DD')); |
| 140 | +</script> |
| 141 | +``` |
| 142 | + |
| 143 | +## Bundle Size |
| 144 | + |
| 145 | +::: tip Tree Shaking Benefits |
| 146 | +date-and-time is built with tree-shaking in mind. You only bundle the functions and locales you actually use, resulting in optimal bundle sizes. Modern bundlers like Webpack 5, Rollup, and Vite automatically eliminate unused code, ensuring your application stays lightweight. |
| 147 | +::: |
| 148 | + |
| 149 | +## Verification |
| 150 | + |
| 151 | +After installation, verify that the library works correctly: |
| 152 | + |
| 153 | +```typescript |
| 154 | +import { format } from 'date-and-time'; |
| 155 | + |
| 156 | +console.log(format(new Date(), 'YYYY/MM/DD HH:mm:ss')); |
| 157 | +// Should output current date in YYYY/MM/DD HH:mm:ss format |
| 158 | +``` |
| 159 | + |
| 160 | +## Next Steps |
| 161 | + |
| 162 | +Now that you have date-and-time installed, you can: |
| 163 | + |
| 164 | +1. **[Quick Start](./quick-start)** - Learn the basics with simple examples |
| 165 | +2. **[API Reference](/api/)** - Dive deep into all available functions |
| 166 | + |
| 167 | +## Troubleshooting |
| 168 | + |
| 169 | +### TypeScript Issues |
| 170 | + |
| 171 | +If you encounter TypeScript errors, ensure you're using TypeScript 4.5+ and have the latest version of date-and-time: |
| 172 | + |
| 173 | +```bash |
| 174 | +npm install typescript@latest date-and-time@latest |
| 175 | +``` |
| 176 | + |
| 177 | +### Module Resolution Issues |
| 178 | + |
| 179 | +For Node.js projects using ES Modules, ensure your `package.json` includes: |
| 180 | + |
| 181 | +```json |
| 182 | +{ |
| 183 | + "type": "module" |
| 184 | +} |
| 185 | +``` |
| 186 | + |
| 187 | +For CommonJS projects, this field should be omitted or set to `"commonjs"`. |
0 commit comments