preface

Here we recommend a simple and practical JavaScript date and time processing tool: easedate.js.

On the basis of the original Date object, easedate. Js provides Date output formatting, Date and time addition and subtracting calculation, time difference calculation, condition judgment, chain operation and other functions, convenient for the Date and time related processing in the project!

Welcome to github.com/springlong/…

Using easydate

Node. Js:

npm install easydate.js
Copy the code
const easydate = require('easydate.js');
Copy the code

Amd:

define(['easydate.js'].function(easydate){});Copy the code

The script tag:

<script src="easydate.js"></script>
Copy the code

About easydate

useeasydateFunction creation instance

// Given the date string
const edate = easydate('2018/03/17 16:08:32');

// Given a timestamp
const edate2 = easydate(1521274112000);

// Give an existing date object
const edate3 = easydate(new Date());

// Default current system time
const edate4 = easydate();
Copy the code

conditional

For Date comparison class methods, parameter types can be Date string, timestamp, Date object instance, or easyDate object instance.

// (Boolean) Check the validity of date and time
edate.isValid();

// (Boolean) Check whether it is a leap year
edate.isLeapYear();

//(Boolean) Checks whether the current date precedes the target date
edate.isBefore('2018/03/18');

// (Boolean) Checks whether the current date is after the target date
edate.isAfter('2018/03/16');

// (Boolean) Checks whether the current date matches the target date
edate.isSame('2018/03/17 16:08:32');

// (Boolean) Determine whether the current date is between two dates
edate.isBetween('2018/03/18'.'2018/03/16');
Copy the code

The information section that gets the date and time

// (Date) Date object
edate.date;

// (number) Returns a snapshot at the millisecond level
edate.valueOf();

// (number) Returns the year
edate.getFullYear();

// (number) Returns the month. The value ranges from 1 to 12
edate.getMonth();

// (number) Returns the day of the month
edate.getDate();

// (number) Returns the day of the week. The value ranges from 0 to 6, indicating Sunday to Saturday
edate.getDay();

// (number) returns the clock
edate.getHours();

// (number) returns a minute
edate.getMinutes();

// (number) returns the second
edate.getSeconds();

// (number) Returns the number of milliseconds
edate.getMilliseconds();

// (number) Returns the week of the year in which the current date and time belong
edate.getWeekth();

// (number) Returns the number of days in the current month
edate.getMonthDays();
Copy the code

Date-time formatted output

Format character code:

  1. yyyy– Four-digit year
  2. yy– Two-digit year
  3. MM– Two-digit month
  4. MIn –
  5. dd– Two-digit number of days
  6. d– the number of days
  7. HH– Two-digit 24-hour clock
  8. H– 24-hour clock
  9. hh– Two-digit 12-hour clock
  10. h– 12-hour clock
  11. mm– Two digit minutes
  12. m– minutes
  13. ss– Two digit seconds
  14. s– second
  15. tt– Morning/afternoon
  16. S– millisecond
// Format output '2018/03/17 16:08:32'
Format (' YYYY-MM-DD HH: MM :ss')
edate.format();

// Format output '18/3/17 16:8:32'
edate.format('yy-M-d H:m:s');

// Format output '2018/03/17 16:08:32'
edate.format('yyyy-MM-dd HH:mm:ss');

// Format output '2018 Mar 17 04:08:32 PM '
edate.format(Tt hh: MM :ss);

Sat Mar 17 2018 16:08:32 GMT+0800 (Chinese standard time)
edate.toString();

// Return date-time string '2018/3/17 4:08:32' in local format
edate.toLocaleString();

// Return the date string '2018/3/17' in local format
edate.toLocaleDateString();

// Return time string '4:08:32 PM' in local format
edate.toLocaleTimeString();

// Return date-time string 'Sat, 17 Mar 2018 08:08:32 GMT' according to universal time
// Use toUTCString() insteadEdate. The toGMTString ();// Return date-time string 'Sat, 17 Mar 2018 08:08:32 GMT' according to universal timeEdate. ToUTCString ();Copy the code

Date and time calculation

The result of the operation returns the EasyDate instance itself, so chaining can be done.

Positive and negative numbers are supported. A positive number adds a specified time, and a negative number subtracts a specified time.

// edate:
/ / 2018/03/17 16:08:32

/ / seconds + 1
/ / 2018/03/17 16:08:33
edate.calc('second'.1);  

/ / minutes + 1
/ / 2018/03/17 16:09:33
edate.calc('minute'.1);  

/ / the clock + 1
/ / 2018/03/17 17:09:33
edate.calc('hour'.1);  

/ / number of + 1
/ / 2018/03/18 17:09:33
edate.calc('day'.1);  

/ / + 1 month
/ / 2018/04/18 17:09:33
edate.calc('month'.1);  

/ / + 1 year
/ / 2019/04/18 17:09:33
edate.calc('year'.1);  

// Truncate the date section
/ / 2018/04/18 00:00:00
edate.toDatePart();  

// Create a clone
/ / 2018/04/18 00:00:00
edate.clone();

Copy the code

Sets the specified portion of the date time

Returns the easyDate instance itself, setMonth() with months ranging from 1 to 12.

// edate:
/ / 2018/04/18 00:00:00

// Set the year 2018/04/18 00:00:00
edate.setFullYear(2019);  

// Set the month 2018/06/18 00:00:00
edate.setMonth(6);  

// Set the day of the month
edate.setDate(25);

// Set clock 2018/06/25 10:00:00
edate.setHours(10);

// Set minutes 2018/06/25 10:32:00
edate.setMinutes(32);

// Set seconds 2018/06/25 10:32:58
edate.setSeconds(58);

// Set milliseconds 2018/06/25 10:32:58 100
edate.setMilliseconds(100);
Copy the code

Calculation time difference

For Date comparison class methods, parameter types can be Date string, timestamp, Date object instance, or easyDate object instance.

// edate:
/ / the 2018-03-23 12:00:00
edate.diff('the 2018-03-24 16:30:55'.'json');   // {year: 0, month: 0, day: 1, hour: 4, minite: 30, second: 55}
edate.diff('the 2018-03-24 16:30:55'.'year');   // 0 (less than 1 year)
edate.diff('the 2018-03-24 16:30:55'.'month');  // 0 (less than 1 month)
edate.diff('the 2018-03-24 16:30:55'.'day');    // 1 (1 day difference)
edate.diff('the 2018-03-24 16:30:55'.'hour');   // 28 (difference of 28 hours)
edate.diff('the 2018-03-24 16:30:55'.'minute'); // 1710 (1710 min difference)
edate.diff('the 2018-03-24 16:30:55'.'second'); // 102655 (difference of 102655 seconds)
edate.diff('the 2018-03-24 16:30:55');           // 1 (1 day difference)
Copy the code

The chain operation

// output: 2018/03/23 12:00:00
easydate('2018/03/17 16:08:32').toDatePart().calc('day'.6).calc('hour'.12).format();
Copy the code

A static method

For dateStr arguments to Date comparison class methods, the types can be Date string, timestamp, Date object instance, or easyDate object instance.

// easydate.isValid(dateStr)
// Determine the validity of the date and time
// Return value type: Boolean
easydate.isValid();  // false
easydate.isValid('2018-03-28 xx:33:25');  // false
easydate.isValid('the 2018-03-28 18:33:25');  // true

// easydate.isLeapYear(year)
// Check whether it is a leap year
// Return value type: Boolean
easydate.isLeapYear();  // false
easydate.isLeapYear(2018);  // false
easydate.isLeapYear(2020);  // true

// easydate.getWeekth(dateStr)
// Returns a date the week of the year in which the time is
// Return type: number
// NaN is returned if the date is invalid
easydate.getWeekth('2018-01-01');  / / 1
easydate.getWeekth('2018-12-29');  / / 52
easydate.getWeekth('2018-12-31');  / / 53

// easydate.getMonthDays(dateStr)
// Returns the number of days in the month in which a date and time are located
// Return type: number
// NaN is returned if the date is invalid
easydate.getMonthDays('2018-01-01');  / / 31
easydate.getMonthDays('2018-02-01');  / / 28
easydate.getMonthDays('2018-03-01');  / / 31
easydate.getMonthDays('2018-04-01');  / / 30

// easydate.diff (startDate, endDate, unit)
// Calculates the time difference between two dates, expressed in "days" by default
easydate.diff('2018-01-01'.false);  // NaN
easydate.diff('2018-01-01'.'2018-01-01');  // 0 (0 day difference)
easydate.diff('2018-03-14'.'2018-03-30');  // 16 (16 days difference)
easydate.diff('the 2018-01-31 10:30:55'.'the 2019-02-03 12:50:56'.'json');   // {year: 0, month: 0, day: 25, hour: 2, minite: 20, second: 1, maxUnit: 'day'}
easydate.diff('the 2018-01-31 10:30:55'.'the 2019-02-03 12:50:56'.'json');   // {year: 1, month: 0, day: 3, hour: 2, minite: 20, second: 1, maxUnit: 'year'}
easydate.diff('the 2018-01-31 10:30:55'.'the 2019-02-03 12:50:56'.'year');   // 1 (1 year difference)
easydate.diff('the 2018-01-31 10:30:55'.'the 2019-02-03 12:50:56'.'month');  // 12 (difference of 12 months)
easydate.diff('the 2018-01-31 10:30:55'.'the 2019-02-03 12:50:56'.'day');    // 368 (368 days difference)
easydate.diff('the 2018-01-31 10:30:55'.'the 2019-02-03 12:50:56'.'hour');   // 8834 (8834 hours difference)
easydate.diff('the 2018-01-31 10:30:55'.'the 2019-02-03 12:50:56'.'minute'); // 530060 (530060 minutes)
easydate.diff('the 2018-01-31 10:30:55'.'the 2019-02-03 12:50:56'.'second'); // 31803601 (difference of 31803601 seconds)
Copy the code

subsequent

If you think it’s good, or have better suggestions and ideas, please visit GitHub.