2.0.0 • Published 5 years ago

corie-date v2.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

corie-date

npm package

一个模块化且性能卓越的日期库,可代替Moment.js并有着相同的API

English Document

目录

安装

使用script标签加载corie-date

<script src="https://cdn.jsdelivr.net/npm/corie-date/dist/corie-date.min.js"></script>

使用requirejs加载corie-date

define(['corie-date'], function(corieDate) {
  // your code
});

使用CommonJS的方式加载corie-date

npm install corie-date --save
// old way
var corieDate = require('corie-date');

// modern way
import corieDate from 'corie-date';

// typescript
import * as corieDate from 'corie-date';

API使用

解析

var corieDate = require('corie-date');
// 当前时间
var now = corieDate();
// 解析字符串
var date = corieDate('2018-07');
var date = corieDate('2018-07-01');
var date = corieDate('2018-07-01 17:10:02');
var date = corieDate('2018-07-01 17:10:02 +08:00');
var date = corieDate('2018-07-01T09:10:02.234Z');
var date = corieDate('2018-07', 'YYYY-MM');
var date = corieDate('2018-07-01', 'YYYY-MM-DD');
var date = corieDate('2018-07-01 17:10:02', 'YYYY-MM-DD hh:mm:ss');
var date = corieDate('2018-07-01 23:27:44 +08:00', 'YYYY-MM-DD hh:mm:ss');
var date = corieDate('2018-07-01T09:10:02.234Z', 'YYYY-MM-DD hh:mm:ss.SSSZ');

取值/赋值

// 毫秒
corieDate().millisecond(123);
corieDate().millisecond(); // Number
corieDate().milliseconds(123);
corieDate().milliseconds(); // Number

// 秒
corieDate().second(1);
corieDate().second(); // Number
corieDate().seconds(1);
corieDate().seconds(); // Number

// 分
corieDate().minute(1);
corieDate().minute(); // Number
corieDate().minutes(1);
corieDate().minutes(); // Number

// 时
corieDate().hour(1);
corieDate().hour(); // Number
corieDate().hours(1);
corieDate().hours(); // Number

// 日期
corieDate().date(1);
corieDate().date(); // Number
corieDate().dates(1);
corieDate().dates(); // Number

// 星期
corieDate().day(1);
corieDate().day(); // Number
corieDate().days(1);
corieDate().days(); // Number

// 天
corieDate().dayOfYear(123);
corieDate().dayOfYear(); // Number
corieDate("2012-02", "YYYY-MM").daysInMonth() // 29
corieDate("2012-01", "YYYY-MM").daysInMonth() // 31

// 月
corieDate().month(1);
corieDate().month(); // Number
corieDate().months(1);
corieDate().months(); // Number

// 年
corieDate().year(2000);
corieDate().year(); // Number
corieDate().years(2000);
corieDate().years(); // Number

corieDate().get('year');
corieDate().get('month');  // 0 to 11
corieDate().get('date');
corieDate().get('day');
corieDate().get('hour');
corieDate().get('minute');
corieDate().get('second');
corieDate().get('millisecond');

corieDate().set('year', 2013);
corieDate().set('month', 3);  // April
corieDate().set('date', 1);
corieDate().set('day', 1);
corieDate().set('hour', 13);
corieDate().set('minute', 20);
corieDate().set('second', 30);
corieDate().set('millisecond', 123);

操作

corieDate().add('year', 3);
corieDate().add('month', 3);
corieDate().add('date', 1);
corieDate().add('day', 1);
corieDate().add('hour', 13);
corieDate().add('minute', 20);
corieDate().add('second', 30);
corieDate().add('millisecond', 123);

corieDate().subtract('year', 3);
corieDate().subtract('month', 3);
corieDate().subtract('date', 1);
corieDate().subtract('day', 1);
corieDate().subtract('hour', 13);
corieDate().subtract('minute', 20);
corieDate().subtract('second', 30);
corieDate().subtract('millisecond', 123);

corieDate().startOf('year');
corieDate().startOf('month');  // 0 to 11
corieDate().startOf('date');
corieDate().startOf('day');
corieDate().startOf('hour');
corieDate().startOf('minute');
corieDate().startOf('second');
corieDate().startOf('millisecond');

corieDate().endOf('year');
corieDate().endOf('month');  // 0 to 11
corieDate().endOf('date');
corieDate().endOf('day');
corieDate().endOf('hour');
corieDate().endOf('minute');
corieDate().endOf('second');
corieDate().endOf('millisecond');

显示

// 格式化
corieDate().format('YYYY-MM-DD HH:mm:ss');
corieDate().format('YYYY-MM-DD');
corieDate().format('yyyy-MM-dd');
corieDate().format('YYYY/MM/DD');
corieDate().format('YYYY-MM-DD hh:mm:ss');
corieDate().format('YYYY-MM-DD HH:mm:ss');
corieDate().format('YYYY-MM-DDTHH:mm:ss');
corieDate().format('YY/M/D');
corieDate().format('YY-M-D H:m:s');
corieDate().format('YY-M-d h:m:s');
corieDate().format('YYYY-MM-DD hh:mm:ss.SSS');
corieDate().format('YYYY-MM-DD hh:mm:ss.SSS +08:00');
corieDate().format('YYYY-MM-DD hh:mm:ss.SSS -01:00');
corieDate().format('YYYY-MM-DD hh:mm:ss Z');

// 时差
var a = corieDate([2007, 0, 29]);
var b = corieDate([2007, 0, 28]);
a.diff(b) // 86400000

var a = corieDate([2007, 0, 29]);
var b = corieDate([2007, 0, 28]);
a.diff(b, 'days') // 1

var a = corieDate([2008, 6]);
var b = corieDate([2007, 0]);
a.diff(b, 'years');       // 1
a.diff(b, 'years', true); // 1.5

var a = corieDate();
var b = corieDate().add(1, 'seconds');
a.diff(b) // -1000
b.diff(a) // 1000

// 时间戳(毫秒)
corieDate().valueOf();
+corieDate();

corieDate(1318874398806).valueOf(); // 1318874398806
+corieDate(1318874398806); // 1318874398806

// Date对象
corieDate().toDate();

// 数组
corieDate().toArray();

// ISO 8601 字符串 
corieDate().toISOString() // 2013-02-04T22:44:30.652Z

查询

// 是否之前
corieDate('2010-10-20').isBefore('2010-10-21'); // true
corieDate('2010-10-20').isBefore('2010-12-31', 'year'); // false
corieDate('2010-10-20').isBefore('2011-01-01', 'year'); // true

// 是否相同
corieDate('2010-10-20').isSame('2010-10-20'); // true
corieDate('2010-10-20').isSame('2009-12-31', 'year');  // false
corieDate('2010-10-20').isSame('2010-01-01', 'year');  // true
corieDate('2010-10-20').isSame('2010-12-31', 'year');  // true
corieDate('2010-10-20').isSame('2011-01-01', 'year');  // false
corieDate('2010-01-01').isSame('2011-01-01', 'month'); // false, different year
corieDate('2010-01-01').isSame('2010-02-01', 'day');   // false, different month

// 是否之后
corieDate('2010-10-20').isAfter('2010-10-19'); // true
corieDate('2010-10-20').isAfter('2010-01-01', 'year'); // false
corieDate('2010-10-20').isAfter('2009-12-31', 'year'); // true

// 是否之间
corieDate('2010-10-20').isBetween('2010-10-19', '2010-10-25'); // true
corieDate('2010-10-20').isBetween('2010-01-01', '2012-01-01', 'year'); // false
corieDate('2010-10-20').isBetween('2009-12-31', '2012-01-01', 'year'); // true

// 是否闰年
corieDate().isLeapYear();
corieDate([2000]).isLeapYear(); // true
corieDate([2001]).isLeapYear(); // false
corieDate([2100]).isLeapYear(); // false

工具

corieDate.add();
corieDate.clone();
corieDate.dayOfYear();
corieDate.daysInMonth();
corieDate.diff();
corieDate.endOf();
corieDate.format();
corieDate.get();
corieDate.isAfter();
corieDate.isBefore();
corieDate.isBetween();
corieDate.isLeapYear();
corieDate.isSame();
corieDate.isSameOrAfter();
corieDate.isSameOrBefore();
corieDate.isValid();
corieDate.parse();
corieDate.set();
corieDate.startOf();
corieDate.subtract();

按需使用

  • 有时候我们只是想简单地操作Date对象,避免引入一个日期库,就可以单独引入需要的函数
import add from 'corie-date/src/add';
import dayOfYear from 'corie-date/src/dayOfYear';
import daysInMonth from 'corie-date/src/daysInMonth';
import diff from 'corie-date/src/diff';
import endOf from 'corie-date/src/endOf';
import format from 'corie-date/src/format';
import isAfter from 'corie-date/src/isAfter';
import isBefore from 'corie-date/src/isBefore';
import isBetween from 'corie-date/src/isBetween';
import isLeapYear from 'corie-date/src/isLeapYear';
import isSame from 'corie-date/src/isSame';
import isSameOrAfter from 'corie-date/src/isSameOrAfter';
import isSameOrBefore from 'corie-date/src/isSameOrBefore';
import isValid from 'corie-date/src/isValid';
import startOf from 'corie-date/src/startOf';
import subtract from 'corie-date/src/subtract';
import clone from 'corie-date/src/clone';
import parse from 'corie-date/src/parse';

import each from 'celia/es/each';
import forEach from 'celia/es/forEach';
import isArrayLike from 'celia/es/isArrayLike';
import isBoolean from 'celia/es/isBoolean';
import isDate from 'celia/es/isDate';
import isFunction from 'celia/es/isFunction';
import isNumber from 'celia/es/isNumber';
import isObject from 'celia/es/isObject';
import isString from 'celia/es/isString';
import isUndefined from 'celia/es/isUndefined';
import isNil from 'celia/es/isNil';
import type from 'celia/es/type';

例子

测试用例

更新说明

2.0.0

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago