|
1 | 1 | # Date Time API |
2 | 2 |
|
3 | | -[](LICENSE) [](https://www.npmjs.com/package/@atlassian/date-time) [](https://github.com/atlassian/date-time-api/actions/workflows/release.yml?query=event%3Apush) [](CONTRIBUTING.md) |
| 3 | +[](LICENSE) |
| 4 | +[](https://www.npmjs.com/package/@atlassian/date-time) |
| 5 | +[](https://github.com/atlassian/date-time-api/actions/workflows/ci.yml) |
| 6 | +[](CONTRIBUTING.md) |
4 | 7 |
|
5 | | -## Introduction |
| 8 | +A comprehensive library for date and time manipulation in JavaScript. Provides utilities for parsing, validating, and formatting dates and times. |
6 | 9 |
|
7 | | -The Date Time API is a comprehensive library designed to simplify date and time manipulation in JavaScript. It provides a wide range of utilities for parsing, validating and formatting dates and times, making it easier for developers to handle date-time operations in their applications. |
| 10 | +## Installation |
| 11 | + |
| 12 | +```bash |
| 13 | +npm install @atlassian/date-time |
| 14 | +``` |
8 | 15 |
|
9 | 16 | ## Usage |
10 | 17 |
|
11 | 18 | ```javascript |
12 | 19 | import * as dateTime from "@atlassian/date-time"; |
| 20 | +``` |
13 | 21 |
|
14 | | -// Parse a date string |
15 | | -const date = dateTime.parse("2024-01-01"); |
16 | | -console.log(date); // Outputs: "Mon Jan 01 2024 00:00:00 GMT+0000 (Coordinated Universal Time)" |
17 | | - |
18 | | -// Get date pattern |
19 | | -const pattern = dateTime.getDatePattern("en-US"); |
20 | | -console.log(pattern); // Outputs: "mm/dd/yyyy" |
21 | | - |
22 | | -// Validate a date string |
23 | | -const isValid = dateTime.validate("2024-01-01"); |
24 | | -console.log(isValid); // Outputs: true |
| 22 | +### Parsing & Validation |
25 | 23 |
|
26 | | -// Format a plain date |
27 | | -const plainDate = dateTime.formatPlainDate(new Date()); |
28 | | -console.log(plainDate); // Outputs: "2024-01-01" |
| 24 | +```javascript |
| 25 | +dateTime.parse("2024-01-01"); |
| 26 | +// → Mon Jan 01 2024 00:00:00 GMT+0000 |
29 | 27 |
|
30 | | -// Format a plain time |
31 | | -const plainTime = dateTime.formatPlainTime(new Date()); |
32 | | -console.log(plainTime); // Outputs: "12:00:00" |
| 28 | +dateTime.validate("2024-01-01"); |
| 29 | +// → true |
33 | 30 |
|
34 | | -// Format a plain date-time |
35 | | -const plainDateTime = dateTime.formatPlainDateTime(new Date()); |
36 | | -console.log(plainDateTime); // Outputs: "2024-01-01T12:00:00" |
| 31 | +dateTime.getDatePattern("en-US"); |
| 32 | +// → "mm/dd/yyyy" |
| 33 | +``` |
37 | 34 |
|
38 | | -// Format a numeric date |
39 | | -const numericDate = dateTime.formatNumericDate(new Date(), "en-US"); |
40 | | -console.log(numericDate); // Outputs: "1/1/2024" |
| 35 | +### Formatting Dates & Times |
41 | 36 |
|
42 | | -// Format a date |
43 | | -const formattedDate = dateTime.formatDate(new Date(), "en-US"); |
44 | | -console.log(formattedDate); // Outputs: "Jan 1, 2024" |
| 37 | +```javascript |
| 38 | +dateTime.formatPlainDate(new Date()); |
| 39 | +// → "2024-01-01" |
45 | 40 |
|
46 | | -// Format a time |
47 | | -const formattedTime = dateTime.formatTime(new Date(), "en-US"); |
48 | | -console.log(formattedTime); // Outputs: "12:00:00 PM" |
| 41 | +dateTime.formatPlainTime(new Date()); |
| 42 | +// → "12:00:00" |
49 | 43 |
|
50 | | -// Format a date-time |
51 | | -const formattedDateTime = dateTime.formatDateTime(new Date(), "en-US"); |
52 | | -console.log(formattedDateTime); // Outputs: "Jan 1, 2024, 12:00:00 PM" |
| 44 | +dateTime.formatPlainDateTime(new Date()); |
| 45 | +// → "2024-01-01T12:00:00" |
| 46 | +``` |
53 | 47 |
|
54 | | -// Format a date-time with options |
55 | | -const formattedDateTimeWithOptions = dateTime.formatDateTimeByOptions({ second: undefined }, new Date(), "en-US"); |
56 | | -console.log(formattedDateTimeWithOptions); // Outputs: "Jan 1, 2024, 12:00 PM" |
| 48 | +### Locale-aware Formatting |
57 | 49 |
|
58 | | -// Format a duration |
59 | | -const duration = dateTime.formatDuration(new Date(2024, 0, 1), new Date(2024, 0, 2), "en-US"); |
60 | | -console.log(duration); // Outputs: "1 day 1 hour 1 minute 1 second" |
| 50 | +```javascript |
| 51 | +dateTime.formatNumericDate(new Date(), "en-US"); |
| 52 | +// → "1/1/2024" |
61 | 53 |
|
62 | | -// Format a duration with custom options |
63 | | -const durationWithOptions = dateTime.formatDurationByOptions({ unitDisplay: "narrow" }, new Date(2024, 0, 1), new Date(2024, 0, 2), "en-US"); |
64 | | -console.log(durationWithOptions); // Outputs: "1d 1h 1m 1s" |
65 | | -``` |
| 54 | +dateTime.formatDate(new Date(), "en-US"); |
| 55 | +// → "Jan 1, 2024" |
66 | 56 |
|
67 | | -## Installation |
| 57 | +dateTime.formatTime(new Date(), "en-US"); |
| 58 | +// → "12:00:00 PM" |
68 | 59 |
|
69 | | -To install the Date Time API, use npm: |
| 60 | +dateTime.formatDateTime(new Date(), "en-US"); |
| 61 | +// → "Jan 1, 2024, 12:00:00 PM" |
70 | 62 |
|
71 | | -```bash |
72 | | -npm install @atlassian/date-time |
| 63 | +dateTime.formatDateTimeByOptions({ second: undefined }, new Date(), "en-US"); |
| 64 | +// → "Jan 1, 2024, 12:00 PM" |
73 | 65 | ``` |
74 | 66 |
|
75 | | -## Build |
| 67 | +### Duration Formatting |
76 | 68 |
|
77 | | -To build the Date Time API, use npm: |
| 69 | +```javascript |
| 70 | +dateTime.formatDuration(new Date(2024, 0, 1), new Date(2024, 0, 2), "en-US"); |
| 71 | +// → "1 day 1 hour 1 minute 1 second" |
78 | 72 |
|
79 | | -```bash |
80 | | -npm run build |
| 73 | +dateTime.formatDurationByOptions({ unitDisplay: "narrow" }, new Date(2024, 0, 1), new Date(2024, 0, 2), "en-US"); |
| 74 | +// → "1d 1h 1m 1s" |
81 | 75 | ``` |
82 | 76 |
|
83 | | -## Tests |
84 | | - |
85 | | -To run the tests, use the following command: |
| 77 | +## Development |
86 | 78 |
|
87 | 79 | ```bash |
88 | | -npm test |
| 80 | +npm run typecheck # Type checking |
| 81 | +npm run lint # ESLint + Prettier check |
| 82 | +npm run format # Auto-fix lint + format |
| 83 | +npm test # Run unit tests |
| 84 | +npm run build # Build for production |
| 85 | +npm run checks # Run all checks |
89 | 86 | ``` |
90 | 87 |
|
91 | 88 | ## Contributions |
|
0 commit comments