1.1.1 • Published 3 years ago

date-update v1.1.1

Weekly downloads
7
License
MIT
Repository
github
Last release
3 years ago

date-update — an update for js' Date class

Js' Date is a very useful class, but sometimes to doing something "simple" you have to write a lot of boring code (even with other similar libraries).

That's why I've wrote this package that add some methods to Date objects which simplify the usage of them.

This package mainly helps in manage the date "as a value" (mathematical and data stuff) and NOT in formatting or similar.

Any suggestions are welcome, fell free to make a pull request!

Usage

Install it form npm

$ npm install date-update

and import it into your project:

require('date-update');

Added methods

All of these method create a new Date object and don't modify itself

In this extension all time unit of measure have a string that represent itself:

  • Years: y
  • Month: M
  • Days: d
  • Hours: h
  • Minutes: m
  • Seconds: s
  • Milliseconds: ms
  • trim(a, b): \ returns a new date resetting the date/time not in [a; b], where a and b are a time unit. \ For example, if you use it in this way

    	```javascript
    	var time = new Date("2020-08-16T08:19:46.590Z").trim('h', 'ms')
    	```
    	you will obtain `"1970-01-01T08:19:46.590Z"`, i.e. resets years, months and days and keep hours, minutes, seconds and milliseconds
    
    	> **NB**: it supports even "circular interval", for example `new Date("2020-08-16T08:19:46.590Z").trim('s', 'd')` will produce `2020-08-16T00:00:46.590Z`, i.e. keep seconds, milliseconds, year, month and day only.
  • add(s): \ add an amount of time (specified by an expression of time unit) to the date. \ The expression must be a sequence of integer and time unit (for example "2M -4h" which add 2 month and subtract 4 hours):

    	```javascript
    	var date = new Date("2020-08-16T08:19:46.590Z").add("+5M -15d")
    	```
    	date will be `"2021-01-01T08:19:46.590Z"`
    
    	> **NB**: it supports even "broken value" (for example "+365d" will add a year, if the actual year is not bissextile) and also repetition of the same time unit (for example "+3h -5h")
  • fixOffset: \ Since this library reason in UTC to avoid some other problems, to get the correct local date you have to adjuts the timezone offset. This function returns a new date with the correct offset.

    	For example with a timezone +-x where x!=0:
    	```javascript
    	new Date('2021/01/01').trim('y','d').getDate() // 31
    	```
    	because of the "setUTC...", to avoid this problem easily use fixOffset like below:
    	```javascript
    	new Date('2021/01/01').trim('y','d').fixOffset().getDate() // 1
    	```
1.1.1

3 years ago

1.1.0

3 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.1.0

4 years ago