1.0.4 • Published 5 years ago

hg-date v1.0.4

Weekly downloads
13
License
ISC
Repository
github
Last release
5 years ago

hg-date

A minimal Dates lib, esay to manipulate Dates.

Installation Guide


NPM:

$ npm install hg-date --save
let HgDate = require('hg-date')
import HgDate from 'hg-date'

CDN:

<script src="https://unpkg.com/hg-date"></script>
<script>
  new HgDate()
</script>

Usage

new HgDate().format('YYYY-MM-DD')
//=> 2019-08-28
new HgDate('2019-08-28').add(1, 'd').format('YYYY-MM-DD')
//=> 2019-08-29
new HgDate('20190828145430567').format('YYYY-MM-DD hh:mm:ss.S')
//=> 2019-08-28 14:54:30.567
new HgDate('2019-08-28').diff('2019-08-25').getAs('day')
//=> 3

API


HgDate: class

  • (static) Duration
  • format
  • add
  • substract
  • startOf
  • endOf
  • getTime
  • diff

METHODS

constructor


param:

  • date?:string | Date | number - the time of the date

    date string can be follows and compatible with all browsers:

    1. YYYY-MM-DD
    2. YYYY/MM/DD
    3. YYYYMMDD
    4. YYYY-MM-DD hh:mm
    5. YYYY/MM/DD hh:mm
    6. YYYYMMDDhhmm
    7. YYYY-MM-DD hh:mm:ss
    8. YYYY/MM/DD hh:mm:ss
    9. YYYYMMDDhhmmss
    10. YYYY-MM-DD hh:mm:ss.S
    11. YYYY/MM/DD hh:mm:ss.S
    12. YYYYMMDDhhmmssS

example:

new HgDate() 
new HgDate('2019-05-11')
new HgDate(new Date())
new HgDate(1566978539522)

format


param:

  • tokenStr: string - the date string that you want to format.the detail see Format Token Table.

return:

  • string

example:

new HgDate().format('YYYY|MM|DD hh:mm')
// => 2019|05|11 14:30
new HgDate().format('YYYY/MM/DD hh:mm:ss.S')
// => 2019/05/11 14:30:20.320

Format Token


tokenoutputdesc
YYYY2019years
MM12mouth
DD24days
hh14hours
mm34minutes
ss59seconds
S199milliseconds

add


param:

  • amt: number - the amount you want to add.
  • unit: Unit - this value of Unit Table(Unit or Unit Abbr).

return:

  • HgDate

example:

new HgDate('2019-05-11').add(4, 'd').format('YYYY-MM-DD')
// => 2019-05-15 00:00
new HgDate('2019-05-11 12:50').add(4, 'minute').format('YYYY-MM-DD hh:mm')
// => 2019-05-11 12:54

Unit Table


Unit AbbrUnit
msmillisecond
ssecond
mminute
hhour
dday
Mmonth
yyear

substract


param:

  • amt: number - the amount you want to substract.
  • unit: Unit - this value of Unit Table(Unit or Unit Abbr).

return:

  • HgDate

example:

new HgDate('2019-05-11').substract(4, 'd').format('YYYY-MM-DD')
// => 2019-05-07 00:00
new HgDate('2019-05-11 12:50').substract(4, 'minute').format('YYYY-MM-DD hh:mm')
// => 2019-05-11 12:46

startOf


param:

  • unit: Unit - this value of Unit Table(Unit or Unit Abbr).

return:

  • HgDate

example:

new HgDate('2019-05-11 13:14').startOf('d').format('YYYY-MM-DD hh:mm')
// => 2019-05-11 00:00
new HgDate('2019-05-11 12:50:34').startOf('minute').format('YYYY-MM-DD hh:mm:ss')
// => 2019-05-11 12:50:00

endOf


param:

  • unit: Unit - this value of Unit Table(Unit or Unit Abbr).

return:

  • HgDate

example:

new HgDate('2019-05-11 13:14').endOf('d').format('YYYY-MM-DD hh:mm')
// => 2019-05-11 23:59
new HgDate('2019-05-11 12:50:34').endOf('minute').format('YYYY-MM-DD hh:mm:ss')
// => 2019-05-11 12:50:59

getTime


param:

return: the millisecond of the date(same as date)

  • number

example:

new HgDate().getTime()
// => 1566978539522

diff


param:

  • date: HgDate | Date | string | number

return: the millisecond of the date(same as date)

  • Duration (see class Duration)

example:

new HgDate('2019-09-28').diff('2019-08-25').getAs('day')
//=> 3
new HgDate('2019-08-28 12:03').diff(new HgDate('2019-08-25 12:02')).get('m')
//=> 1.5
new HgDate('2019-08-28 12:03').diff(new HgDate('2019-08-25 12:02')).get('m', true)
//=> 1

Duration: class

  • getTime
  • getAs
  • get

METHODS

construtor


param:

  • number - millisecond

example:

new HgDate.Duration(5000)

getTime


param:

return: the millisecond of the date(same as date)

  • number

example:

new HgDate.Duration(5421).getTime()
// => 5421

get


param:

  • unit: Unit - this value of Unit Table(Unit or Unit Abbr).

    The length of a duration in months is defined as 30 days.
    The length of a duration in years is defined as 365 days.

return: the millisecond of the date(same as date)

  • number

example:

new HgDate.Duration(5421).get('s')
// => 5
new HgDate.Duration(5421).get('ms')
// => 521

getAs


param:

  • unit: Unit - this value of Unit Table(Unit or Unit Abbr).
  • isRound?: boolean = false - is need round.if isRound = false this result will round to 2 decimal places.

    The length of a duration in months is defined as 30 days.
    The length of a duration in years is defined as 365 days.

return: the millisecond of the date(same as date)

  • number

example:

new HgDate.Duration(5421).getAs('s')
// => 5.42
new HgDate.Duration(5421).getAs('s', true)
// => 5

1.0.4

5 years ago

1.0.2

5 years ago

1.0.3

5 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago