1.0.0 • Published 6 years ago

js-day v1.0.0

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

js-time

缩减 day.js, 增加 ie兼容,增加相距时间。

codecov Build Status

安装

npm install js-time.js --save

使用

import time from 'js-time.js';

time().format('YYYY-MM-DD HH:mm:ss'); // 2018-08-15 15:44:33

API

  • format

    time().format(); // 2018-08-15 15:44:33
    
    time().format('YYYY-MM-DD'); // 2018-08-15
    
    time(1534325803000).format('YYYY-MM-DD') // 2018-08-15
  • ago

    /*
    * 小于10秒
    * 刚刚
    */
    time('2018-8-1 1:0:0').ago('2018-8-1 0:59:51'); // 刚刚
    
    /*
    * 大于10秒,小于60秒
    * x 秒前
    */
    time('2018-8-1 1:0:0').ago('2018-8-1 0:59:30'); // 30秒前
    
    /*
    * 大于60秒,小于1小时
    * x 分钟前
    */
    time('2018-8-1 1:0:0').ago('2018-8-1 0:30:30'); // 29分钟前
    
    /*
    * 大于1小时,小于1天
    * x 小时前
    */
    time('2018-8-1 1:0:0').ago('2018-7-31 5:30:30'); // 19小时前
    
    /*
    * 大于1天,小于1个月
    * x 天前
    */
    time('2018-8-1 1:0:0').ago('2018-7-11 5:30:30'); // 20天前
    
    /*
    * 大于1个月,小于1年
    * x 个月前
    */
    time('2018-8-1 1:0:0').ago('2018-3-11 5:30:30'); // 4个月前
    
    /*
    * 大于1年
    * x 年前
    */
    time('2018-8-1 1:0:0').ago('2015-3-11 5:30:30'); // 3年前
  • isValid

    time('sss').isValid(); // false
    time().isValid(); // true
  • isSame

    time('2018-1-1').isSame('2018-1-1'); // true
  • isBefore

    time('2018-1-1').isBefore('2018-1-2'); // true
  • isAfter

    time('2018-1-1').isAfter('2018-1-2'); // false
  • year

    time().year(); // 2018
  • month

    time().month(); // 7
  • day

    time().day(); // 3
  • date

    time().date(); // 15
  • hour

    time().hour(); // 17
  • minute

    time().minute(); // 16
  • second

    time().second(); // 18
  • millisecond

    time().millisecond(); // 130
  • unix

    time().unix(); // 1534324615
  • valueOf

    time().unix(); // 1534324916443
  • toDate

    time().toDate(); // Wed Aug 15 2018 17:24:10 GMT+0800 (中国标准时间)
  • toObject

    time().toObject(); // ["years":2018,"months":7,"date":15,"hours":17,"minutes":23,"seconds":28,"milliseconds":623]
  • dayNum

    time('2018-8-20').dayNum('2018-8-19') // 1 (几天之前)
  • monthDays

    // 获取当月天数 无月份默认1月
    
    time('2018-8-20').monthDays() // 31 
    time('2018-2').monthDays() // 29
    time('2018').monthDays() // 31

License

MIT