@creowis/intl-date-utils 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.all-contributorsrc +35 -0
- package/CODE_OF_CONDUCT.md +128 -0
- package/CONTRIBUTING.md +198 -0
- package/LICENSE +21 -0
- package/README.md +169 -0
- package/SECURITY.md +21 -0
- package/build.js +19 -0
- package/package.json +31 -0
- package/src/dateUtils.js +280 -0
- package/src/index.d.ts +84 -0
- package/tests/dateUtils.test.js +248 -0
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"files": [
|
|
3
|
+
"README.md"
|
|
4
|
+
],
|
|
5
|
+
"imageSize": 100,
|
|
6
|
+
"commit": false,
|
|
7
|
+
"commitType": "docs",
|
|
8
|
+
"commitConvention": "angular",
|
|
9
|
+
"contributors": [
|
|
10
|
+
{
|
|
11
|
+
"login": "atapas",
|
|
12
|
+
"name": "Tapas Adhikary",
|
|
13
|
+
"avatar_url": "https://avatars.githubusercontent.com/u/3633137?v=4",
|
|
14
|
+
"profile": "https://tapasadhikary.com",
|
|
15
|
+
"contributions": [
|
|
16
|
+
"code"
|
|
17
|
+
]
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
"login": "ATechAjay",
|
|
21
|
+
"name": "Ajay Yadav",
|
|
22
|
+
"avatar_url": "https://avatars.githubusercontent.com/u/76649353?v=4",
|
|
23
|
+
"profile": "https://github.com/ATechAjay",
|
|
24
|
+
"contributions": [
|
|
25
|
+
"code"
|
|
26
|
+
]
|
|
27
|
+
}
|
|
28
|
+
],
|
|
29
|
+
"contributorsPerLine": 7,
|
|
30
|
+
"skipCi": true,
|
|
31
|
+
"repoType": "github",
|
|
32
|
+
"repoHost": "https://github.com",
|
|
33
|
+
"projectName": "intl-date-utils",
|
|
34
|
+
"projectOwner": "CreoWis"
|
|
35
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity
|
|
10
|
+
and orientation.
|
|
11
|
+
|
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
|
13
|
+
diverse, inclusive, and healthy community.
|
|
14
|
+
|
|
15
|
+
## Our Standards
|
|
16
|
+
|
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
|
18
|
+
community include:
|
|
19
|
+
|
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
|
24
|
+
and learning from the experience
|
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the
|
|
26
|
+
overall community
|
|
27
|
+
|
|
28
|
+
Examples of unacceptable behavior include:
|
|
29
|
+
|
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or
|
|
31
|
+
advances of any kind
|
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
|
33
|
+
* Public or private harassment
|
|
34
|
+
* Publishing others' private information, such as a physical or email
|
|
35
|
+
address, without their explicit permission
|
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
37
|
+
professional setting
|
|
38
|
+
|
|
39
|
+
## Enforcement Responsibilities
|
|
40
|
+
|
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
|
44
|
+
or harmful.
|
|
45
|
+
|
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
|
49
|
+
decisions when appropriate.
|
|
50
|
+
|
|
51
|
+
## Scope
|
|
52
|
+
|
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
|
54
|
+
an individual is officially representing the community in public spaces.
|
|
55
|
+
Examples of representing our community include using an official e-mail address,
|
|
56
|
+
posting via an official social media account, or acting as an appointed
|
|
57
|
+
representative at an online or offline event.
|
|
58
|
+
|
|
59
|
+
## Enforcement
|
|
60
|
+
|
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
62
|
+
reported to the community leaders responsible for enforcement at
|
|
63
|
+
hello@creowis.com.
|
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
|
65
|
+
|
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
|
67
|
+
reporter of any incident.
|
|
68
|
+
|
|
69
|
+
## Enforcement Guidelines
|
|
70
|
+
|
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
|
73
|
+
|
|
74
|
+
### 1. Correction
|
|
75
|
+
|
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
|
77
|
+
unprofessional or unwelcome in the community.
|
|
78
|
+
|
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
|
82
|
+
|
|
83
|
+
### 2. Warning
|
|
84
|
+
|
|
85
|
+
**Community Impact**: A violation through a single incident or series
|
|
86
|
+
of actions.
|
|
87
|
+
|
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
|
92
|
+
like social media. Violating these terms may lead to a temporary or
|
|
93
|
+
permanent ban.
|
|
94
|
+
|
|
95
|
+
### 3. Temporary Ban
|
|
96
|
+
|
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
|
98
|
+
sustained inappropriate behavior.
|
|
99
|
+
|
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
|
101
|
+
communication with the community for a specified period of time. No public or
|
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
|
104
|
+
Violating these terms may lead to a permanent ban.
|
|
105
|
+
|
|
106
|
+
### 4. Permanent Ban
|
|
107
|
+
|
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
|
111
|
+
|
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within
|
|
113
|
+
the community.
|
|
114
|
+
|
|
115
|
+
## Attribution
|
|
116
|
+
|
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
|
118
|
+
version 2.0, available at
|
|
119
|
+
https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
|
120
|
+
|
|
121
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct
|
|
122
|
+
enforcement ladder](https://github.com/mozilla/diversity).
|
|
123
|
+
|
|
124
|
+
[homepage]: https://www.contributor-covenant.org
|
|
125
|
+
|
|
126
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
|
127
|
+
https://www.contributor-covenant.org/faq. Translations are available at
|
|
128
|
+
https://www.contributor-covenant.org/translations.
|
package/CONTRIBUTING.md
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
# Contributing
|
|
2
|
+
|
|
3
|
+
When contributing to this repository, please first discuss the change you wish to make via [issue](https://github.com/CreoWis/next-js-launchpad/issues),
|
|
4
|
+
[email](mailto:hello@creowis.com), or any other method with the owners of this repository before making a change.
|
|
5
|
+
|
|
6
|
+
Please note we have a [Code of Conduct](https://github.com/CreoWis/next-js-launchpad/blob/main/CODE_OF_CONDUCT.md), please follow it in all your interactions with the project.
|
|
7
|
+
|
|
8
|
+
- [Pull Request Process](#prp)
|
|
9
|
+
- [Code of Conduct](#coc)
|
|
10
|
+
- [Commit Message Format](#commit)
|
|
11
|
+
|
|
12
|
+
## <a name="prp"></a> Pull Request Process
|
|
13
|
+
|
|
14
|
+
1. Ensure any install or build dependencies are removed before the end of the layer when doing a
|
|
15
|
+
build.
|
|
16
|
+
2. Update the README.md with details of changes to the interface, this includes new environment
|
|
17
|
+
variables, exposed ports, useful file locations and container parameters.
|
|
18
|
+
3. Increase the version numbers in any examples files and the README.md to the new version that this
|
|
19
|
+
Pull Request would represent. The versioning scheme we use is [SemVer](http://semver.org/).
|
|
20
|
+
4. You may merge the Pull Request in once you have the sign-off of two other developers, or if you
|
|
21
|
+
do not have permission to do that, you may request the second reviewer to merge it for you.
|
|
22
|
+
|
|
23
|
+
## <a name="coc"></a> Code of Conduct
|
|
24
|
+
|
|
25
|
+
### Our Pledge
|
|
26
|
+
|
|
27
|
+
In the interest of fostering an open and welcoming environment, we as
|
|
28
|
+
contributors and maintainers pledge to making participation in our project and
|
|
29
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
|
30
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
|
31
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
|
32
|
+
orientation.
|
|
33
|
+
|
|
34
|
+
### Our Standards
|
|
35
|
+
|
|
36
|
+
Examples of behavior that contributes to creating a positive environment
|
|
37
|
+
include:
|
|
38
|
+
|
|
39
|
+
- Using welcoming and inclusive language
|
|
40
|
+
- Being respectful of differing viewpoints and experiences
|
|
41
|
+
- Gracefully accepting constructive criticism
|
|
42
|
+
- Focusing on what is best for the community
|
|
43
|
+
- Showing empathy towards other community members
|
|
44
|
+
|
|
45
|
+
Examples of unacceptable behavior by participants include:
|
|
46
|
+
|
|
47
|
+
- The use of sexualized language or imagery and unwelcome sexual attention or
|
|
48
|
+
advances
|
|
49
|
+
- Trolling, insulting/derogatory comments, and personal or political attacks
|
|
50
|
+
- Public or private harassment
|
|
51
|
+
- Publishing others' private information, such as a physical or electronic
|
|
52
|
+
address, without explicit permission
|
|
53
|
+
- Other conduct which could reasonably be considered inappropriate in a
|
|
54
|
+
professional setting
|
|
55
|
+
|
|
56
|
+
### Our Responsibilities
|
|
57
|
+
|
|
58
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
|
59
|
+
behavior and are expected to take appropriate and fair corrective action in
|
|
60
|
+
response to any instances of unacceptable behavior.
|
|
61
|
+
|
|
62
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
|
63
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
|
64
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
|
65
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
|
66
|
+
threatening, offensive, or harmful.
|
|
67
|
+
|
|
68
|
+
### Scope
|
|
69
|
+
|
|
70
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
|
71
|
+
when an individual is representing the project or its community. Examples of
|
|
72
|
+
representing a project or community include using an official project e-mail
|
|
73
|
+
address, posting via an official social media account, or acting as an appointed
|
|
74
|
+
representative at an online or offline event. Representation of a project may be
|
|
75
|
+
further defined and clarified by project maintainers.
|
|
76
|
+
|
|
77
|
+
### Enforcement
|
|
78
|
+
|
|
79
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
80
|
+
reported by contacting the project team at [hello@creowis.com](mailto:hello@creowis.com). All
|
|
81
|
+
complaints will be reviewed and investigated and will result in a response that
|
|
82
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
|
83
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
|
84
|
+
Further details of specific enforcement policies may be posted separately.
|
|
85
|
+
|
|
86
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
|
87
|
+
faith may face temporary or permanent repercussions as determined by other
|
|
88
|
+
members of the project's leadership.
|
|
89
|
+
|
|
90
|
+
### Attribution
|
|
91
|
+
|
|
92
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
|
93
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
|
94
|
+
|
|
95
|
+
[homepage]: http://contributor-covenant.org
|
|
96
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
|
97
|
+
|
|
98
|
+
## <a name="commit"></a> Commit Message Format
|
|
99
|
+
|
|
100
|
+
We have very precise rules over how our Git commit messages must be formatted.
|
|
101
|
+
This format leads to **easier to read commit history**.
|
|
102
|
+
|
|
103
|
+
Each commit message consists of a **header**, a **body**, and a **footer**.
|
|
104
|
+
|
|
105
|
+
```
|
|
106
|
+
<header>
|
|
107
|
+
<BLANK LINE>
|
|
108
|
+
<body>
|
|
109
|
+
<BLANK LINE>
|
|
110
|
+
<footer>
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
The `header` is mandatory and must conform to the [Commit Message Header](#commit-header) format.
|
|
114
|
+
|
|
115
|
+
The `body` is mandatory for all commits except for those of type "docs".
|
|
116
|
+
When the body is present it must be at least 20 characters long and must conform to the [Commit Message Body](#commit-body) format.
|
|
117
|
+
|
|
118
|
+
The `footer` is optional. The [Commit Message Footer](#commit-footer) format describes what the footer is used for and the structure it must have.
|
|
119
|
+
|
|
120
|
+
#### <a name="commit-header"></a>Commit Message Header
|
|
121
|
+
|
|
122
|
+
```
|
|
123
|
+
<type>(<scope>): <short summary>
|
|
124
|
+
│ │ │
|
|
125
|
+
│ │ └─⫸ Summary in present tense. Not capitalized. No period at the end.
|
|
126
|
+
│ │
|
|
127
|
+
│ └─⫸ Commit Scope: common|plays (2048, analog-clock, basic-calculator, etc.)|infra|etc.
|
|
128
|
+
│
|
|
129
|
+
└─⫸ Commit Type: build|ci|docs|feat|fix|perf|refactor|test
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
The `<type>` and `<summary>` fields are mandatory, the `(<scope>)` field is optional.
|
|
133
|
+
|
|
134
|
+
##### Type
|
|
135
|
+
|
|
136
|
+
Must be one of the following:
|
|
137
|
+
|
|
138
|
+
- **build**: changes that affect the build system or external dependencies
|
|
139
|
+
- **ci**: changes to our CI configuration files and script
|
|
140
|
+
- **docs**: documentation only changes
|
|
141
|
+
- **feat**: new feature
|
|
142
|
+
- **fix**: bug fix
|
|
143
|
+
- **perf**: code change that improves performance
|
|
144
|
+
- **refactor**: code change that neither fixes a bug nor adds a feature
|
|
145
|
+
- **test**: add missing tests or correcting existing tests
|
|
146
|
+
|
|
147
|
+
##### Summary
|
|
148
|
+
|
|
149
|
+
Use the summary field to provide a succinct description of the change:
|
|
150
|
+
|
|
151
|
+
- use the imperative, present tense: "change" not "changed" nor "changes"
|
|
152
|
+
- don't capitalize the first letter
|
|
153
|
+
- no dot (.) at the end
|
|
154
|
+
|
|
155
|
+
#### <a name="commit-body"></a>Commit Message Body
|
|
156
|
+
|
|
157
|
+
Just as in the summary, use the imperative, present tense: "fix" not "fixed" nor "fixes".
|
|
158
|
+
|
|
159
|
+
Explain the motivation for the change in the commit message body. This commit message should explain _why_ you are making the change.
|
|
160
|
+
You can include a comparison of the previous behavior with the new behavior in order to illustrate the impact of the change.
|
|
161
|
+
|
|
162
|
+
#### <a name="commit-footer"></a>Commit Message Footer
|
|
163
|
+
|
|
164
|
+
The footer can contain information about breaking changes and deprecations and is also the place to reference GitHub issues, Jira tickets, and other PRs that this commit closes or is related to.
|
|
165
|
+
For example:
|
|
166
|
+
|
|
167
|
+
```
|
|
168
|
+
BREAKING CHANGE: <breaking change summary>
|
|
169
|
+
<BLANK LINE>
|
|
170
|
+
<breaking change description + migration instructions>
|
|
171
|
+
<BLANK LINE>
|
|
172
|
+
<BLANK LINE>
|
|
173
|
+
Fixes #<issue number>
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
or
|
|
177
|
+
|
|
178
|
+
```
|
|
179
|
+
DEPRECATED: <what is deprecated>
|
|
180
|
+
<BLANK LINE>
|
|
181
|
+
<deprecation description + recommended update path>
|
|
182
|
+
<BLANK LINE>
|
|
183
|
+
<BLANK LINE>
|
|
184
|
+
Closes #<pr number>
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
Breaking Change section should start with the phrase "BREAKING CHANGE: " followed by a summary of the breaking change, a blank line, and a detailed description of the breaking change that also includes migration instructions.
|
|
188
|
+
|
|
189
|
+
Similarly, a Deprecation section should start with "DEPRECATED: " followed by a short description of what is deprecated, a blank line, and a detailed description of the deprecation that also mentions the recommended update path.
|
|
190
|
+
|
|
191
|
+
### Revert commits
|
|
192
|
+
|
|
193
|
+
If the commit reverts a previous commit, it should begin with `revert: `, followed by the header of the reverted commit.
|
|
194
|
+
|
|
195
|
+
The content of the commit message body should contain:
|
|
196
|
+
|
|
197
|
+
- information about the SHA of the commit being reverted in the following format: `This reverts commit <SHA>`,
|
|
198
|
+
- a clear description of the reason for reverting the commit message.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 CreoWis
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
|
|
2
|
+
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
|
|
3
|
+
[](#contributors-)
|
|
4
|
+
<!-- ALL-CONTRIBUTORS-BADGE:END -->
|
|
5
|
+
# intl-date-utils
|
|
6
|
+
|
|
7
|
+
Lightweight date utilities powered by the modern JavaScript Intl API.
|
|
8
|
+
|
|
9
|
+
Features:
|
|
10
|
+
|
|
11
|
+
- Zero dependencies
|
|
12
|
+
- Intl-based formatting
|
|
13
|
+
- Timezone aware
|
|
14
|
+
- Relative time utilities
|
|
15
|
+
- Small bundle size
|
|
16
|
+
- Works in Node.js and modern browsers
|
|
17
|
+
- Types enabled to work with TypeScript Projects
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
Using NPM
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm install intl-date-utils
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Using Yarn
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
yarn add intl-date-utils
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Using Pnpm
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
pnpm install intl-date-utils
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
## Usage
|
|
40
|
+
|
|
41
|
+
```js
|
|
42
|
+
import { formatDate, timeAgo } from 'intl-date-utils'
|
|
43
|
+
|
|
44
|
+
formatDate(new Date())
|
|
45
|
+
timeAgo(new Date(Date.now() - 86400000))
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Available Methods
|
|
49
|
+
|
|
50
|
+
| Method | Description | Example |
|
|
51
|
+
|------|-------------|--------|
|
|
52
|
+
| `formatDate(date, locale?, style?)` | Formats a date using locale-aware Intl date styles (`full`, `long`, `medium`, `short`). | `formatDate(new Date(), "en-US", "long")` |
|
|
53
|
+
| `formatTime(date, locale?, style?)` | Formats only the time portion of a date using Intl time styles. | `formatTime(new Date(), "en-US")` |
|
|
54
|
+
| `formatDateTime(date, locale?, options?)` | Formats date and time together with optional timezone support. | `formatDateTime(new Date(), "en-US", { timeZone: "Asia/Tokyo" })` |
|
|
55
|
+
| `formatInTimezone(date, timeZone, locale?)` | Formats a date in a specific timezone regardless of system timezone. | `formatInTimezone(new Date(), "Asia/Tokyo")` |
|
|
56
|
+
| `toLocaleDateISO(date, timeZone?)` | Returns a timezone-aware ISO calendar date string (`YYYY-MM-DD`). Useful for comparing dates across timezones. | `toLocaleDateISO(new Date(), "UTC")` |
|
|
57
|
+
| `timeAgo(date, locale?)` | Returns human-readable relative time like "3 days ago" or "in 2 hours". | `timeAgo(new Date(Date.now() - 86400000))` |
|
|
58
|
+
| `getMonthNames(locale?, style?)` | Returns an array of localized month names. | `getMonthNames("en-US")` |
|
|
59
|
+
| `getDayNames(locale?, style?, startDay?)` | Returns localized weekday names with optional start day (0 = Sunday, 1 = Monday). | `getDayNames("en-US", "long", 1)` |
|
|
60
|
+
| `formatRange(start, end, locale?, style?)` | Formats a date range using Intl range formatting. | `formatRange(new Date("2026-03-10"), new Date("2026-03-15"))` |
|
|
61
|
+
| `isToday(date)` | Returns `true` if the provided date is today. | `isToday(new Date())` |
|
|
62
|
+
| `isYesterday(date)` | Returns `true` if the date is yesterday. | `isYesterday(new Date(Date.now() - 86400000))` |
|
|
63
|
+
| `isTomorrow(date)` | Returns `true` if the date is tomorrow. | `isTomorrow(addDays(new Date(), 1))` |
|
|
64
|
+
| `startOfDay(date)` | Returns a new Date representing the start of the day (00:00:00). | `startOfDay(new Date())` |
|
|
65
|
+
| `endOfDay(date)` | Returns a new Date representing the end of the day (23:59:59.999). | `endOfDay(new Date())` |
|
|
66
|
+
| `addDays(date, amount)` | Adds or subtracts days from a date. | `addDays(new Date(), 5)` |
|
|
67
|
+
| `addMonths(date, amount)` | Adds or subtracts months from a date. | `addMonths(new Date(), 2)` |
|
|
68
|
+
| `differenceInDays(a, b)` | Returns the number of calendar days between two dates. | `differenceInDays(new Date("2026-03-15"), new Date("2026-03-10"))` |
|
|
69
|
+
| `parseISO(value)` | Parses an ISO date string and returns a Date object with validation. | `parseISO("2026-03-15")` |
|
|
70
|
+
| `formatSmartDate(date, locale?)` | Smart UI formatting (Today, Yesterday, relative time, or formatted date). | `formatSmartDate(new Date())` |
|
|
71
|
+
|
|
72
|
+
## Development
|
|
73
|
+
|
|
74
|
+
Install dependencies
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
npm install
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Run tests
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
npm test
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Build library
|
|
87
|
+
|
|
88
|
+
```bash
|
|
89
|
+
npm run build
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## Test Package Locally
|
|
93
|
+
|
|
94
|
+
Create npm tarball
|
|
95
|
+
|
|
96
|
+
```bash
|
|
97
|
+
npm pack
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
This generates:
|
|
101
|
+
|
|
102
|
+
```bash
|
|
103
|
+
intl-date-utils-1.0.0.tgz
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
You can test install locally:
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
npm install ./intl-date-utils-1.0.0.tgz
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Publish to npm
|
|
113
|
+
|
|
114
|
+
Login
|
|
115
|
+
|
|
116
|
+
```bash
|
|
117
|
+
npm login
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
Publish
|
|
121
|
+
|
|
122
|
+
```bash
|
|
123
|
+
npm publish --access public
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
## License
|
|
127
|
+
|
|
128
|
+
[MIT](./LICENSE)
|
|
129
|
+
|
|
130
|
+
## 🤝 Contributing
|
|
131
|
+
|
|
132
|
+
Contributions are always welcome!
|
|
133
|
+
|
|
134
|
+
See [CONTRIBUTING.md](https://github.com/CreoWis/intl-date-utils/blob/main/CONTRIBUTING.md) for ways to get started.
|
|
135
|
+
|
|
136
|
+
Please adhere to this project's [code of conduct](https://github.com/CreoWis/intl-date-utils/blob/main/CODE_OF_CONDUCT.md).
|
|
137
|
+
|
|
138
|
+
## 🙏 Support Us
|
|
139
|
+
|
|
140
|
+
This is an OSS project maintained by [CreoWis](https://www.creowis.com/). We need all the support we can get. Please give this project a ⭐️ to encourage and show that you liked it.
|
|
141
|
+
|
|
142
|
+
If you found the project helpful, consider supporting us with a ☕
|
|
143
|
+
|
|
144
|
+
<a href="https://www.buymeacoffee.com/creowis">
|
|
145
|
+
<img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" height="50px">
|
|
146
|
+
</a>
|
|
147
|
+
|
|
148
|
+
## Contributors ✨
|
|
149
|
+
|
|
150
|
+
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
|
|
151
|
+
|
|
152
|
+
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
|
|
153
|
+
<!-- prettier-ignore-start -->
|
|
154
|
+
<!-- markdownlint-disable -->
|
|
155
|
+
<table>
|
|
156
|
+
<tbody>
|
|
157
|
+
<tr>
|
|
158
|
+
<td align="center" valign="top" width="14.28%"><a href="https://tapasadhikary.com"><img src="https://avatars.githubusercontent.com/u/3633137?v=4?s=100" width="100px;" alt="Tapas Adhikary"/><br /><sub><b>Tapas Adhikary</b></sub></a><br /><a href="https://github.com/CreoWis/intl-date-utils/commits?author=atapas" title="Code">💻</a></td>
|
|
159
|
+
<td align="center" valign="top" width="14.28%"><a href="https://github.com/ATechAjay"><img src="https://avatars.githubusercontent.com/u/76649353?v=4?s=100" width="100px;" alt="Ajay Yadav"/><br /><sub><b>Ajay Yadav</b></sub></a><br /><a href="https://github.com/CreoWis/intl-date-utils/commits?author=ATechAjay" title="Code">💻</a></td>
|
|
160
|
+
</tr>
|
|
161
|
+
</tbody>
|
|
162
|
+
</table>
|
|
163
|
+
|
|
164
|
+
<!-- markdownlint-restore -->
|
|
165
|
+
<!-- prettier-ignore-end -->
|
|
166
|
+
|
|
167
|
+
<!-- ALL-CONTRIBUTORS-LIST:END -->
|
|
168
|
+
|
|
169
|
+
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
|
package/SECURITY.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# Security Policy
|
|
2
|
+
|
|
3
|
+
## Supported Versions
|
|
4
|
+
|
|
5
|
+
Use this section to tell people about which versions of your project are
|
|
6
|
+
currently being supported with security updates.
|
|
7
|
+
|
|
8
|
+
| Version | Supported |
|
|
9
|
+
| ------- | ------------------ |
|
|
10
|
+
| 5.1.x | :white_check_mark: |
|
|
11
|
+
| 5.0.x | :x: |
|
|
12
|
+
| 4.0.x | :white_check_mark: |
|
|
13
|
+
| < 4.0 | :x: |
|
|
14
|
+
|
|
15
|
+
## Reporting a Vulnerability
|
|
16
|
+
|
|
17
|
+
Use this section to tell people how to report a vulnerability.
|
|
18
|
+
|
|
19
|
+
Tell them where to go, how often they can expect to get an update on a
|
|
20
|
+
reported vulnerability, what to expect if the vulnerability is accepted or
|
|
21
|
+
declined, etc.
|
package/build.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
|
|
2
|
+
import { build } from 'esbuild'
|
|
3
|
+
|
|
4
|
+
await build({
|
|
5
|
+
entryPoints: ['src/dateUtils.js'],
|
|
6
|
+
outfile: 'dist/dateUtils.js',
|
|
7
|
+
format: 'esm',
|
|
8
|
+
platform: 'neutral',
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
await build({
|
|
12
|
+
entryPoints: ['src/dateUtils.js'],
|
|
13
|
+
outfile: 'dist/dateUtils.min.js',
|
|
14
|
+
format: 'esm',
|
|
15
|
+
minify: true,
|
|
16
|
+
platform: 'neutral',
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
console.log('Build complete')
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@creowis/intl-date-utils",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Lightweight date utilities powered by the JavaScript Intl API",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/dateUtils.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./dist/dateUtils.js"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "node build.js",
|
|
12
|
+
"test": "node --test",
|
|
13
|
+
"prepare": "npm run build"
|
|
14
|
+
},
|
|
15
|
+
"types": "./index.d.ts",
|
|
16
|
+
"keywords": [
|
|
17
|
+
"intl",
|
|
18
|
+
"date",
|
|
19
|
+
"javascript",
|
|
20
|
+
"i18n",
|
|
21
|
+
"timezone",
|
|
22
|
+
"creowis"
|
|
23
|
+
],
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": ">=18"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"esbuild": "^0.21.0"
|
|
30
|
+
}
|
|
31
|
+
}
|
package/src/dateUtils.js
ADDED
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
// dateUtils.js
|
|
2
|
+
// Modern Intl-based date utilities
|
|
3
|
+
// Zero dependencies. Node 18+ and modern browsers.
|
|
4
|
+
|
|
5
|
+
// ─────────────────────────────────────────────
|
|
6
|
+
// Internal Helpers
|
|
7
|
+
// ─────────────────────────────────────────────
|
|
8
|
+
|
|
9
|
+
function toDate(value) {
|
|
10
|
+
const d = value instanceof Date ? value : new Date(value)
|
|
11
|
+
|
|
12
|
+
if (Number.isNaN(d.getTime())) {
|
|
13
|
+
throw new Error(`Invalid date value: ${value}`)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return d
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// ─────────────────────────────────────────────
|
|
20
|
+
// Intl Formatter Cache
|
|
21
|
+
// ─────────────────────────────────────────────
|
|
22
|
+
|
|
23
|
+
const formatterCache = new Map()
|
|
24
|
+
|
|
25
|
+
function getFormatter(locale, options) {
|
|
26
|
+
const key = locale + "|" + JSON.stringify(options)
|
|
27
|
+
|
|
28
|
+
if (!formatterCache.has(key)) {
|
|
29
|
+
formatterCache.set(key, new Intl.DateTimeFormat(locale, options))
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return formatterCache.get(key)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// ─────────────────────────────────────────────
|
|
36
|
+
// Core Formatting Utilities
|
|
37
|
+
// ─────────────────────────────────────────────
|
|
38
|
+
|
|
39
|
+
function formatDate(date, locale = "en-US", style = "medium") {
|
|
40
|
+
return getFormatter(locale, { dateStyle: style }).format(toDate(date))
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function formatTime(date, locale = "en-US", style = "short") {
|
|
44
|
+
return getFormatter(locale, { timeStyle: style }).format(toDate(date))
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function formatDateTime(date, locale = "en-US", options = {}) {
|
|
48
|
+
const { dateStyle = "medium", timeStyle = "short", timeZone } = options
|
|
49
|
+
|
|
50
|
+
return getFormatter(locale, {
|
|
51
|
+
dateStyle,
|
|
52
|
+
timeStyle,
|
|
53
|
+
timeZone
|
|
54
|
+
}).format(toDate(date))
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function formatInTimezone(date, timeZone, locale = "en-US") {
|
|
58
|
+
return getFormatter(locale, {
|
|
59
|
+
dateStyle: "long",
|
|
60
|
+
timeStyle: "short",
|
|
61
|
+
timeZone
|
|
62
|
+
}).format(toDate(date))
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// ─────────────────────────────────────────────
|
|
66
|
+
// ISO Calendar Date
|
|
67
|
+
// Useful for cross-timezone comparisons
|
|
68
|
+
// ─────────────────────────────────────────────
|
|
69
|
+
|
|
70
|
+
function toLocaleDateISO(date, timeZone = "UTC") {
|
|
71
|
+
const fmt = getFormatter("en-CA", {
|
|
72
|
+
year: "numeric",
|
|
73
|
+
month: "2-digit",
|
|
74
|
+
day: "2-digit",
|
|
75
|
+
timeZone
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
const parts = Object.fromEntries(
|
|
79
|
+
fmt.formatToParts(toDate(date)).map(p => [p.type, p.value])
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
return parts.year + "-" + parts.month + "-" + parts.day
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// ─────────────────────────────────────────────
|
|
86
|
+
// Relative Time
|
|
87
|
+
// ─────────────────────────────────────────────
|
|
88
|
+
|
|
89
|
+
const rtfCache = new Map()
|
|
90
|
+
|
|
91
|
+
function getRTF(locale, numeric = "auto") {
|
|
92
|
+
const key = locale + "|" + numeric
|
|
93
|
+
|
|
94
|
+
if (!rtfCache.has(key)) {
|
|
95
|
+
rtfCache.set(key, new Intl.RelativeTimeFormat(locale, { numeric }))
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return rtfCache.get(key)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const RELATIVE_UNITS = [
|
|
102
|
+
["year", 31536000],
|
|
103
|
+
["month", 2592000],
|
|
104
|
+
["week", 604800],
|
|
105
|
+
["day", 86400],
|
|
106
|
+
["hour", 3600],
|
|
107
|
+
["minute", 60],
|
|
108
|
+
["second", 1]
|
|
109
|
+
]
|
|
110
|
+
|
|
111
|
+
function timeAgo(date, locale = "en") {
|
|
112
|
+
const diffSeconds = (toDate(date).getTime() - Date.now()) / 1000
|
|
113
|
+
const abs = Math.abs(diffSeconds)
|
|
114
|
+
|
|
115
|
+
const rtf = getRTF(locale)
|
|
116
|
+
|
|
117
|
+
for (const [unit, seconds] of RELATIVE_UNITS) {
|
|
118
|
+
if (abs >= seconds) {
|
|
119
|
+
return rtf.format(Math.round(diffSeconds / seconds), unit)
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return rtf.format(0, "second")
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// ─────────────────────────────────────────────
|
|
127
|
+
// Calendar Helpers
|
|
128
|
+
// ─────────────────────────────────────────────
|
|
129
|
+
|
|
130
|
+
function getMonthNames(locale = "en-US", style = "long") {
|
|
131
|
+
const fmt = getFormatter(locale, { month: style })
|
|
132
|
+
|
|
133
|
+
return Array.from({ length: 12 }, (_, i) =>
|
|
134
|
+
fmt.format(new Date(2026, i, 1))
|
|
135
|
+
)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function getDayNames(locale = "en-US", style = "long", startDay = 0) {
|
|
139
|
+
const fmt = getFormatter(locale, { weekday: style })
|
|
140
|
+
|
|
141
|
+
return Array.from({ length: 7 }, (_, i) =>
|
|
142
|
+
fmt.format(new Date(2026, 0, 4 + startDay + i))
|
|
143
|
+
)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// ─────────────────────────────────────────────
|
|
147
|
+
// Date Range Formatting
|
|
148
|
+
// ─────────────────────────────────────────────
|
|
149
|
+
|
|
150
|
+
function formatRange(start, end, locale = "en-US", style = "long") {
|
|
151
|
+
const fmt = getFormatter(locale, { dateStyle: style })
|
|
152
|
+
|
|
153
|
+
return fmt.formatRange(toDate(start), toDate(end))
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// ─────────────────────────────────────────────
|
|
157
|
+
// Date Comparison Helpers
|
|
158
|
+
// ─────────────────────────────────────────────
|
|
159
|
+
|
|
160
|
+
function sameCalendarDate(a, b) {
|
|
161
|
+
return (
|
|
162
|
+
a.getFullYear() === b.getFullYear() &&
|
|
163
|
+
a.getMonth() === b.getMonth() &&
|
|
164
|
+
a.getDate() === b.getDate()
|
|
165
|
+
)
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function isToday(date) {
|
|
169
|
+
return sameCalendarDate(toDate(date), new Date())
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function isYesterday(date) {
|
|
173
|
+
const today = startOfDay(new Date())
|
|
174
|
+
const yesterday = addDays(today, -1)
|
|
175
|
+
|
|
176
|
+
return sameCalendarDate(toDate(date), yesterday)
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function isTomorrow(date) {
|
|
180
|
+
const today = startOfDay(new Date())
|
|
181
|
+
const tomorrow = addDays(today, 1)
|
|
182
|
+
|
|
183
|
+
return sameCalendarDate(toDate(date), tomorrow)
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// ─────────────────────────────────────────────
|
|
187
|
+
// Date Math Utilities
|
|
188
|
+
// ─────────────────────────────────────────────
|
|
189
|
+
|
|
190
|
+
function startOfDay(date) {
|
|
191
|
+
const d = toDate(date)
|
|
192
|
+
return new Date(d.getFullYear(), d.getMonth(), d.getDate())
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function endOfDay(date) {
|
|
196
|
+
const d = toDate(date)
|
|
197
|
+
return new Date(d.getFullYear(), d.getMonth(), d.getDate(), 23, 59, 59, 999)
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function addDays(date, amount) {
|
|
201
|
+
const d = toDate(date)
|
|
202
|
+
const result = new Date(d)
|
|
203
|
+
result.setDate(result.getDate() + amount)
|
|
204
|
+
return result
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function addMonths(date, amount) {
|
|
208
|
+
const d = toDate(date)
|
|
209
|
+
const result = new Date(d)
|
|
210
|
+
result.setMonth(result.getMonth() + amount)
|
|
211
|
+
return result
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function differenceInDays(a, b) {
|
|
215
|
+
const diff =
|
|
216
|
+
startOfDay(toDate(a)).getTime() - startOfDay(toDate(b)).getTime()
|
|
217
|
+
|
|
218
|
+
return Math.round(diff / 86400000)
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// ─────────────────────────────────────────────
|
|
222
|
+
// ISO Parsing
|
|
223
|
+
// ─────────────────────────────────────────────
|
|
224
|
+
|
|
225
|
+
function parseISO(value) {
|
|
226
|
+
const d = new Date(value)
|
|
227
|
+
|
|
228
|
+
if (Number.isNaN(d.getTime())) {
|
|
229
|
+
throw new Error(`Invalid ISO date: ${value}`)
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
return d
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// ─────────────────────────────────────────────
|
|
236
|
+
// Smart UI Formatting
|
|
237
|
+
// Perfect for feeds / dashboards
|
|
238
|
+
// ─────────────────────────────────────────────
|
|
239
|
+
|
|
240
|
+
function formatSmartDate(date, locale = "en-US") {
|
|
241
|
+
const d = toDate(date)
|
|
242
|
+
|
|
243
|
+
if (isToday(d)) {
|
|
244
|
+
return "Today " + formatTime(d, locale)
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
if (isYesterday(d)) {
|
|
248
|
+
return "Yesterday"
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
const diff = Math.abs(Date.now() - d.getTime())
|
|
252
|
+
|
|
253
|
+
if (diff < 7 * 86400000) {
|
|
254
|
+
return timeAgo(d, locale)
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
return formatDate(d, locale, "medium")
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
export {
|
|
261
|
+
addDays,
|
|
262
|
+
addMonths,
|
|
263
|
+
differenceInDays,
|
|
264
|
+
endOfDay,
|
|
265
|
+
formatDate,
|
|
266
|
+
formatDateTime,
|
|
267
|
+
formatInTimezone,
|
|
268
|
+
formatRange,
|
|
269
|
+
formatSmartDate,
|
|
270
|
+
formatTime,
|
|
271
|
+
getDayNames,
|
|
272
|
+
getMonthNames,
|
|
273
|
+
isToday,
|
|
274
|
+
isTomorrow,
|
|
275
|
+
isYesterday,
|
|
276
|
+
parseISO,
|
|
277
|
+
startOfDay,
|
|
278
|
+
timeAgo,
|
|
279
|
+
toLocaleDateISO
|
|
280
|
+
};
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
export function formatDate(
|
|
2
|
+
date: Date | number | string,
|
|
3
|
+
locale?: string,
|
|
4
|
+
style?: 'full' | 'long' | 'medium' | 'short'
|
|
5
|
+
): string
|
|
6
|
+
|
|
7
|
+
export function formatTime(
|
|
8
|
+
date: Date | number | string,
|
|
9
|
+
locale?: string,
|
|
10
|
+
style?: 'full' | 'long' | 'medium' | 'short'
|
|
11
|
+
): string
|
|
12
|
+
|
|
13
|
+
export function formatDateTime(
|
|
14
|
+
date: Date | number | string,
|
|
15
|
+
locale?: string,
|
|
16
|
+
options?: {
|
|
17
|
+
dateStyle?: 'full' | 'long' | 'medium' | 'short'
|
|
18
|
+
timeStyle?: 'full' | 'long' | 'medium' | 'short'
|
|
19
|
+
timeZone?: string
|
|
20
|
+
}
|
|
21
|
+
): string
|
|
22
|
+
|
|
23
|
+
export function formatInTimezone(
|
|
24
|
+
date: Date | number | string,
|
|
25
|
+
timeZone: string,
|
|
26
|
+
locale?: string
|
|
27
|
+
): string
|
|
28
|
+
|
|
29
|
+
export function toLocaleDateISO(
|
|
30
|
+
date: Date | number | string,
|
|
31
|
+
timeZone?: string
|
|
32
|
+
): string
|
|
33
|
+
|
|
34
|
+
export function timeAgo(
|
|
35
|
+
date: Date | number | string,
|
|
36
|
+
locale?: string
|
|
37
|
+
): string
|
|
38
|
+
|
|
39
|
+
export function getMonthNames(
|
|
40
|
+
locale?: string,
|
|
41
|
+
style?: 'long' | 'short' | 'narrow'
|
|
42
|
+
): string[]
|
|
43
|
+
|
|
44
|
+
export function getDayNames(
|
|
45
|
+
locale?: string,
|
|
46
|
+
style?: 'long' | 'short' | 'narrow',
|
|
47
|
+
startDay?: number
|
|
48
|
+
): string[]
|
|
49
|
+
|
|
50
|
+
export function formatRange(
|
|
51
|
+
start: Date | number | string,
|
|
52
|
+
end: Date | number | string,
|
|
53
|
+
locale?: string,
|
|
54
|
+
style?: 'full' | 'long' | 'medium' | 'short'
|
|
55
|
+
): string
|
|
56
|
+
|
|
57
|
+
export function isToday(date: Date | number | string): boolean
|
|
58
|
+
export function isYesterday(date: Date | number | string): boolean
|
|
59
|
+
export function isTomorrow(date: Date | number | string): boolean
|
|
60
|
+
|
|
61
|
+
export function startOfDay(date: Date | number | string): Date
|
|
62
|
+
export function endOfDay(date: Date | number | string): Date
|
|
63
|
+
|
|
64
|
+
export function addDays(
|
|
65
|
+
date: Date | number | string,
|
|
66
|
+
amount: number
|
|
67
|
+
): Date
|
|
68
|
+
|
|
69
|
+
export function addMonths(
|
|
70
|
+
date: Date | number | string,
|
|
71
|
+
amount: number
|
|
72
|
+
): Date
|
|
73
|
+
|
|
74
|
+
export function differenceInDays(
|
|
75
|
+
a: Date | number | string,
|
|
76
|
+
b: Date | number | string
|
|
77
|
+
): number
|
|
78
|
+
|
|
79
|
+
export function parseISO(value: string): Date
|
|
80
|
+
|
|
81
|
+
export function formatSmartDate(
|
|
82
|
+
date: Date | number | string,
|
|
83
|
+
locale?: string
|
|
84
|
+
): string
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import test from "node:test"
|
|
2
|
+
import assert from "node:assert/strict"
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
formatDate,
|
|
6
|
+
formatTime,
|
|
7
|
+
formatDateTime,
|
|
8
|
+
formatInTimezone,
|
|
9
|
+
toLocaleDateISO,
|
|
10
|
+
timeAgo,
|
|
11
|
+
getMonthNames,
|
|
12
|
+
getDayNames,
|
|
13
|
+
formatRange,
|
|
14
|
+
isToday,
|
|
15
|
+
isYesterday,
|
|
16
|
+
isTomorrow,
|
|
17
|
+
startOfDay,
|
|
18
|
+
endOfDay,
|
|
19
|
+
addDays,
|
|
20
|
+
addMonths,
|
|
21
|
+
differenceInDays,
|
|
22
|
+
parseISO,
|
|
23
|
+
formatSmartDate
|
|
24
|
+
} from "../src/dateUtils.js"
|
|
25
|
+
|
|
26
|
+
const sampleDate = new Date("2026-03-15T14:30:00Z")
|
|
27
|
+
|
|
28
|
+
// ---------------------------------------------------
|
|
29
|
+
// formatDate
|
|
30
|
+
// ---------------------------------------------------
|
|
31
|
+
|
|
32
|
+
test("formatDate returns formatted date string", () => {
|
|
33
|
+
const result = formatDate(sampleDate, "en-US", "long")
|
|
34
|
+
assert.equal(typeof result, "string")
|
|
35
|
+
assert.ok(result.includes("2026"))
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
// ---------------------------------------------------
|
|
39
|
+
// formatTime
|
|
40
|
+
// ---------------------------------------------------
|
|
41
|
+
|
|
42
|
+
test("formatTime returns formatted time", () => {
|
|
43
|
+
const result = formatTime(sampleDate, "en-US")
|
|
44
|
+
assert.equal(typeof result, "string")
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
// ---------------------------------------------------
|
|
48
|
+
// formatDateTime
|
|
49
|
+
// ---------------------------------------------------
|
|
50
|
+
|
|
51
|
+
test("formatDateTime formats date and time", () => {
|
|
52
|
+
const result = formatDateTime(sampleDate, "en-US")
|
|
53
|
+
assert.equal(typeof result, "string")
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
// ---------------------------------------------------
|
|
57
|
+
// formatInTimezone
|
|
58
|
+
// ---------------------------------------------------
|
|
59
|
+
|
|
60
|
+
test("formatInTimezone formats date in provided timezone", () => {
|
|
61
|
+
const result = formatInTimezone(sampleDate, "Asia/Tokyo")
|
|
62
|
+
assert.equal(typeof result, "string")
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
// ---------------------------------------------------
|
|
66
|
+
// toLocaleDateISO
|
|
67
|
+
// ---------------------------------------------------
|
|
68
|
+
|
|
69
|
+
test("toLocaleDateISO returns YYYY-MM-DD format", () => {
|
|
70
|
+
const result = toLocaleDateISO(sampleDate)
|
|
71
|
+
assert.match(result, /^\d{4}-\d{2}-\d{2}$/)
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
// ---------------------------------------------------
|
|
75
|
+
// timeAgo
|
|
76
|
+
// ---------------------------------------------------
|
|
77
|
+
|
|
78
|
+
test("timeAgo returns relative time string", () => {
|
|
79
|
+
const past = addDays(new Date(), -2)
|
|
80
|
+
const result = timeAgo(past)
|
|
81
|
+
|
|
82
|
+
assert.equal(typeof result, "string")
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
// ---------------------------------------------------
|
|
86
|
+
// getMonthNames
|
|
87
|
+
// ---------------------------------------------------
|
|
88
|
+
|
|
89
|
+
test("getMonthNames returns 12 months", () => {
|
|
90
|
+
const months = getMonthNames()
|
|
91
|
+
|
|
92
|
+
assert.equal(months.length, 12)
|
|
93
|
+
assert.equal(typeof months[0], "string")
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
// ---------------------------------------------------
|
|
97
|
+
// getDayNames
|
|
98
|
+
// ---------------------------------------------------
|
|
99
|
+
|
|
100
|
+
test("getDayNames returns 7 day names", () => {
|
|
101
|
+
const days = getDayNames()
|
|
102
|
+
|
|
103
|
+
assert.equal(days.length, 7)
|
|
104
|
+
assert.equal(typeof days[0], "string")
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
// ---------------------------------------------------
|
|
108
|
+
// formatRange
|
|
109
|
+
// ---------------------------------------------------
|
|
110
|
+
|
|
111
|
+
test("formatRange returns formatted range string", () => {
|
|
112
|
+
const start = new Date("2026-03-10")
|
|
113
|
+
const end = new Date("2026-03-15")
|
|
114
|
+
|
|
115
|
+
const result = formatRange(start, end)
|
|
116
|
+
|
|
117
|
+
assert.equal(typeof result, "string")
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
// ---------------------------------------------------
|
|
121
|
+
// startOfDay
|
|
122
|
+
// ---------------------------------------------------
|
|
123
|
+
|
|
124
|
+
test("startOfDay resets time to midnight", () => {
|
|
125
|
+
const d = startOfDay(sampleDate)
|
|
126
|
+
|
|
127
|
+
assert.equal(d.getHours(), 0)
|
|
128
|
+
assert.equal(d.getMinutes(), 0)
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
// ---------------------------------------------------
|
|
132
|
+
// endOfDay
|
|
133
|
+
// ---------------------------------------------------
|
|
134
|
+
|
|
135
|
+
test("endOfDay sets time to end of day", () => {
|
|
136
|
+
const d = endOfDay(sampleDate)
|
|
137
|
+
|
|
138
|
+
assert.equal(d.getHours(), 23)
|
|
139
|
+
assert.equal(d.getMinutes(), 59)
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
// ---------------------------------------------------
|
|
143
|
+
// addDays
|
|
144
|
+
// ---------------------------------------------------
|
|
145
|
+
|
|
146
|
+
test("addDays correctly adds days", () => {
|
|
147
|
+
const d = new Date("2026-03-10")
|
|
148
|
+
|
|
149
|
+
const result = addDays(d, 5)
|
|
150
|
+
|
|
151
|
+
assert.equal(result.getDate(), 15)
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
// ---------------------------------------------------
|
|
155
|
+
// addMonths
|
|
156
|
+
// ---------------------------------------------------
|
|
157
|
+
|
|
158
|
+
test("addMonths correctly adds months", () => {
|
|
159
|
+
const d = new Date("2026-01-10")
|
|
160
|
+
|
|
161
|
+
const result = addMonths(d, 2)
|
|
162
|
+
|
|
163
|
+
assert.equal(result.getMonth(), 2)
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
// ---------------------------------------------------
|
|
167
|
+
// differenceInDays
|
|
168
|
+
// ---------------------------------------------------
|
|
169
|
+
|
|
170
|
+
test("differenceInDays calculates difference correctly", () => {
|
|
171
|
+
const a = new Date("2026-03-15")
|
|
172
|
+
const b = new Date("2026-03-10")
|
|
173
|
+
|
|
174
|
+
const diff = differenceInDays(a, b)
|
|
175
|
+
|
|
176
|
+
assert.equal(diff, 5)
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
// ---------------------------------------------------
|
|
180
|
+
// parseISO
|
|
181
|
+
// ---------------------------------------------------
|
|
182
|
+
|
|
183
|
+
test("parseISO parses ISO string correctly", () => {
|
|
184
|
+
const d = parseISO("2026-03-15")
|
|
185
|
+
|
|
186
|
+
assert.ok(d instanceof Date)
|
|
187
|
+
assert.equal(d.getFullYear(), 2026)
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
test("parseISO throws for invalid date", () => {
|
|
191
|
+
assert.throws(() => parseISO("invalid-date"))
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
// ---------------------------------------------------
|
|
195
|
+
// isToday
|
|
196
|
+
// ---------------------------------------------------
|
|
197
|
+
|
|
198
|
+
test("isToday detects today", () => {
|
|
199
|
+
const today = new Date()
|
|
200
|
+
|
|
201
|
+
assert.equal(isToday(today), true)
|
|
202
|
+
})
|
|
203
|
+
|
|
204
|
+
// ---------------------------------------------------
|
|
205
|
+
// isYesterday
|
|
206
|
+
// ---------------------------------------------------
|
|
207
|
+
|
|
208
|
+
test("isYesterday detects yesterday", () => {
|
|
209
|
+
const yesterday = addDays(new Date(), -1)
|
|
210
|
+
|
|
211
|
+
assert.equal(isYesterday(yesterday), true)
|
|
212
|
+
})
|
|
213
|
+
|
|
214
|
+
// ---------------------------------------------------
|
|
215
|
+
// isTomorrow
|
|
216
|
+
// ---------------------------------------------------
|
|
217
|
+
|
|
218
|
+
test("isTomorrow detects tomorrow", () => {
|
|
219
|
+
const tomorrow = addDays(new Date(), 1)
|
|
220
|
+
|
|
221
|
+
assert.equal(isTomorrow(tomorrow), true)
|
|
222
|
+
})
|
|
223
|
+
|
|
224
|
+
// ---------------------------------------------------
|
|
225
|
+
// formatSmartDate
|
|
226
|
+
// ---------------------------------------------------
|
|
227
|
+
|
|
228
|
+
test("formatSmartDate returns today format", () => {
|
|
229
|
+
const result = formatSmartDate(new Date())
|
|
230
|
+
|
|
231
|
+
assert.ok(result.startsWith("Today"))
|
|
232
|
+
})
|
|
233
|
+
|
|
234
|
+
test("formatSmartDate returns yesterday string", () => {
|
|
235
|
+
const yesterday = addDays(new Date(), -1)
|
|
236
|
+
|
|
237
|
+
const result = formatSmartDate(yesterday)
|
|
238
|
+
|
|
239
|
+
assert.equal(result, "Yesterday")
|
|
240
|
+
})
|
|
241
|
+
|
|
242
|
+
test("formatSmartDate returns formatted date for older dates", () => {
|
|
243
|
+
const oldDate = addDays(new Date(), -10)
|
|
244
|
+
|
|
245
|
+
const result = formatSmartDate(oldDate)
|
|
246
|
+
|
|
247
|
+
assert.equal(typeof result, "string")
|
|
248
|
+
})
|