https://github.com/muniftanjim/js-set-time
Sets time to Date instance.
https://github.com/muniftanjim/js-set-time
date datetime time
Last synced: 10 months ago
JSON representation
Sets time to Date instance.
- Host: GitHub
- URL: https://github.com/muniftanjim/js-set-time
- Owner: MunifTanjim
- License: mit
- Created: 2020-07-20T18:52:21.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2023-01-08T05:56:22.000Z (about 3 years ago)
- Last Synced: 2025-04-01T04:34:12.485Z (10 months ago)
- Topics: date, datetime, time
- Language: TypeScript
- Homepage: https://npm.im/set-time
- Size: 147 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/MunifTanjim/js-set-time/actions?query=workflow%3ACI)
[](https://npmjs.org/package/set-time)
[](https://codecov.io/gh/MunifTanjim/js-set-time)
[](https://github.com/MunifTanjim/js-set-time/blob/main/LICENSE)
# Set Time
Sets time to Date instance.
## Installation
```sh
# using yarn:
yarn add set-time
# using npm:
npm install --save set-time
```
## Usage
```js
import { setTime } from 'set-time'
let date
const timeString = '12:00:00+06:00'
date = setTime(new Date(), timeString)
date = setTime('2020-01-01T00:00:00Z', timeString)
```
## F.A.Q.
**How can I validate the `timeString` pattern acceptable to `setTime`?**
```js
import { timeStringRegex } from 'set-time'
timeStringRegex.test(timeString)
```
**Why doesn't `setTime` accept date string with UTC time offset _(e.g. `2020-01-01T00:00:00+06:00`)_?**
Because, that would create confusion.
If it was allowed, one would expect the output of `setTime('2020-01-01T00:00:00+06:00', '12:00:00+06:00')` to be `2020-01-01T12:00:00+06:00` (i.e. `2020-01-01T06:00:00Z`).
But the actual output would be `2019-12-31T06:00:00Z` (i.e. `2019-12-31T12:00:00+06:00`).
It's because, `new Date('2020-01-01T00:00:00+06:00')` would always result in `2020-12-31T18:00:00Z` as JavaScript `Date` is actually a specific moment in time.
## License
Licensed under the MIT License. Check the [LICENSE](./LICENSE) file for details.