set-time
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

GitHub Workflow Status: CI Version Coverage License

Set Time

Sets time to Date instance.

Installation

# using yarn: 
yarn add set-time
 
# using npm: 
npm install --save set-time

Usage

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?

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 file for details.

Readme

Keywords

Package Sidebar

Install

npm i set-time

Weekly Downloads

1

Version

0.1.0

License

MIT

Unpacked Size

390 kB

Total Files

16

Last publish

Collaborators

  • muniftanjim