Node.js日期時間處理工具: calendar2

  1. 簡述與安裝使用
  2. Date對象增強
  3. 日期(時間)等值對比
  4. 日期(時間)的增減
  5. 查詢當前日期起止范圍
  6. 靜態(tài)方法:通過起止范圍獲取日歷數組

1. 簡述與安裝使用

請別問為什么是 2

詳細文檔:https://www.npmjs.com/package/calendar2

calendar2 主要用于增強對日期和時間數據的處理,它包含兩個對象:

  • Calendar
  • CalendarTypes

Calendar 對象繼承自 Date 對象,因此可以使用 Date 對象中的所有函數。
CalendarTypes 對象是一個日期時間單位的枚舉,僅包含一些常量如:YEAR(年份)、QUARTER(季度)、MONTH(月份)、WEEK(星期)、WEEKOFMONTH(包含選取當月所有的周范圍列表,僅在函數 toBothDate() 調用時有效)、DAY(日期)、HOURS(小時)、MINUTES(分鐘)、SECONDS(秒鐘)。

1.1 安裝

npm install calendar2 --save

1.2 使用

// 引入工具
const {Calendar, CalendarTypes} = require('calendar2');

// 實例化處理對象
const cal1 = new Calendar(); // 當前時間
const cal2 = new Calendar(new Date('2019-08-09')); // 構造傳入一個Date對象
const cal3 = new Calendar('2019-08-09'); // 構造傳入一個日期字符串

2. Date對象增強

image

我們可以通過 Date 對象的一些函數獲取到想要分割的日期或時間數據,比如 getFullYear() 可以獲取 2021 這樣的具體年份。在 calendar2 中我們增加了幾個增強函數,來處理一些特殊的分割數據:

  • getQuarter()季度索引。獲取當前日期所在本年的季度索引, 以 0 作為起始表示第一季度,以此類推。若解析錯誤則返回 null。
  • getDayOfQuarter()季度中的第幾天。用于獲取當前日期,是本季度中的第幾天,返回一個 Number 格式的天數統計數字。
  • toDate()獲取日期部分。獲取當前日期中的日期部分,默認返回日期格式為 yyyy-MM-dd。
  • toTime()獲取時間部分。獲取當前日期中的時間部分,返回字符串格式為 HH: mm:ss。
  • toDatetime()獲取日期+時間部分。獲取當前日期中的完整日期和時間部分,返回字符串格式為 yyyy-MM-dd HH: mm:ss。
  • toFormat( format_str )格式化日期文本。用于返回自定義格式化的日期/時間文本,默認格式為 yyyy-MM-dd hh: mm:ss,具體可用格式化方案參考 https://www.npmjs.com/package/date-format

示例

const now = new Calendar(); // 2020-06-04 16:42:05
 
now.getFullYear();     // 年份: 2020
now.getDayOfYear();     // 本日是該年中的第幾天: 156
now.getQuarter();   // 季度索引: 1
now.getDayOfQuarter();   // 本日是該季度中的第幾天: 65
now.getMonth();       // 月索引: 5
now.getDate();         // 本日是該月中的第幾天: 4
now.getHours();       // 小時: 16
now.getMinutes();   // 分鐘: 42
now.getSeconds();   // 秒鐘: 5
 
now.toDate();        // 日期文本: '2020-06-04'
now.toTime();        // 時間文本: '16:42:05'
now.toDatetime();// 日期+時間: '2020-06-04 16:42:05'

now.toFormat('yyyy年MM月dd日 hh:mm');  // 自定義格式化: '2020年06月04日 16:42'

3. 日期(時間)等值對比

image

3.1 對比日期 equalsDate(otherDate)

由于 object != object, 因此函數簡化了日期對比方式。對比一個日期,如果一致則返回 true,反之返回 false。僅對比日期部分。
參數 : string | Date | Calendar
返回值 : boolean

示例

const cal = new Calendar('2020-01-01 12:12:12');
cal.equalsDate('2020-01-01 16:16:16');  // 返回: true
cal.equalsDate(new Date('2020-01-02 12:12:12'));        // 返回: false
cal.equalsDate(new Calendar('2020-01-01 20:20:20'));        // 返回: true

3.2 對比日期+時間 equalsDateTime(otherDateTime)

僅對比日期+時間部分。
參數 : string | Date | Calendar
返回值 : boolean

示例

const cal = new Calendar('2020-01-01 12:12:12');
cal.equalsDateTime('2020-01-01 16:16:16');  // 返回: false
cal.equalsDateTime(new Date('2020-01-02 12:12:12'));        // 返回: false
cal.equalsDateTime(new Calendar('2020-01-01 12:12:12'));        // 返回: true

4. 日期(時間)的增減 add(value, types)

image

在當前日期值基礎上進行 ± 運算。函數并不會返回一個新值,而是對原有 Calendar 對象進行日期變更。
參數 : string | Date | Calendar
返回值 : boolean

示例

const cal = new Calendar('2020-05-20 18:01:01');

// 以下代碼結果是以單獨執(zhí)行為例, 因為 add 函數會變更 cal 對象的當前日期值, 
// 因此同時執(zhí)行的話不太容易與實例化中的日期時間進行參照對比
cal.add(1, CalendarTypes.DAY); // 增加 1 天 :  2020-05-21 18:01:01
cal.add(3, CalendarTypes.WEEK); // 增加 3 周 :  2020-06-10 18:01:01
cal.add(-6, CalendarTypes.MONTH); // 減少 6 月 :  2019-11-20 18:01:01
cal.add(+11, CalendarTypes.HOURS); // 增加 11 小時 :  2020-05-21 05:01:01

5. 查詢當前日期起止范圍

image

函數:toBothDate(types [, option])
查詢 當前日期 所處范圍的 開始日期 和 結束日期
參數 :

  • types [CalendarTypes] 篩選范圍,可選枚舉有:YEAR、QUARTER、MONTH、WEEK、WEEKOFMONTH。
  • option.first_weekday: 配置當前周首個的第一天是星期幾,默認為 0 代表星期天,可選值范圍:[0 ~ 6];
  • option.format_str: 配置當前日期范圍返回的文本格式,默認為 yyyy-MM-dd;
  • 返回值 : object

示例

const cal = new Calendar('2020-05-20');

/**
 * 獲取當前日期所在年份的 起止日期
 * 返回值: {
 *     today: '2020-05-20',
 *     dayOfYear: 141,
 *     beginDay: { year: 2020, month: 0, day: 1, text: '2020-1-1' }, 
 *     endDay: { year: 2020, month: 11, day: 31, text: '2020-12-31' }
 * }
 */
cal.toBothDate(CalendarTypes.YEAR);
 
 
/**
 * 獲取當前日期所在季度的 起止日期
 * 返回值: {
 *     today: '2020年05月20日',
 *     dayOfQuarter: 50,
 *     beginDay: { year: 2020, month: 3, day: 1, text: '2020年04月01日' }, 
 *     endDay: { year: 2020, month: 5, day: 30, text: '2020年06月30日' }  
 * }
 */
cal.toBothDate(CalendarTypes.QUARTER, {format_str: 'yyyy年MM月dd日'});
 
 
/**
 * 獲取當前日期所在月份的 起止日期
 * 返回值: {
 *     today: '2020-05-20 00:00',
 *     dayOfMonth: 20,
 *     beginDay: { year: 2020, month: 4, day: 1, text: '2020-05-01 00:00' }, 
 *     endDay: { year: 2020, month: 4, day: 31, text: '2020-05-31 23:59' } 
 * }
 */
cal.toBothDate(CalendarTypes.MONTH, {format_str: 'yyyy-MM-dd hh:mm'});
 
 
/**
 * 獲取當前日期所在 周 的 起止日期
 * 周首日默認從 0 (星期天) 開始計算
 * 返回值: {
 *     today: '2020年05月20日',
 *     todayIndex: 3, // 表示當前周的第幾天, 索引從 0 開始計算
 *     beginDay: { year: 2020, month: 4, day: 17, text: '2020年05月17日' }, 
 *     endDay: { year: 2020, month: 4, day: 23, text: '2020年05月23日' }
 * }
 */
cal.toBothDate(CalendarTypes.WEEK, {format_str: 'yyyy年MM月dd日'});
 
/**
 * 獲取當前日期所在 周 的 起止日期
 * 周首日默認從 1 (星期一) 開始計算
 * 返回值: {
 *     today: '2020年05月20日',
 *     todayIndex: 2,
 *     beginDay: { year: 2020, month: 4, day: 18, text: '2020年05月18日' },
 *     endDay: { year: 2020, month: 4, day: 24, text: '2020年05月24日' }
 * }
 */
cal.toBothDate(CalendarTypes.WEEK, {first_weekday: 1, format_str: 'yyyy年MM月dd日'});
 
 
/**
 * 獲取當前日期所在 周 的 起止日期, 包含當前月所包含的所有周列表
 * 周日期的計算是從 星期日 作為第一天, 進行計數
 * 返回值: {
 *     beginDay: { year: 2020, month: 4, day: 17, text: '2020-5-17' }, 
 *     endDay: { year: 2020, month: 4, day: 23, text: '2020-5-23' },
 *     today: '2020-05-20',
 *     todayIndex: 3,               // 當前日期所在weeks中的索引位置, 若無則值為 -1
 *     weeks: [ {
 *         begin: '2020-04-26',     // 完整的開始日期
 *         end: '2020-05-02',       // 完整的結束日期
 *         begin_format: '2020-04-26', // 日期格式化之后的開始時間文本
 *         end_format: '2020-05-02', // 日期格式化之后的結束時間文本
 *         isCurrentWeek: false,    // 是否是當前日期所在的周
 *         raw: '4/26  ~  5/2',     // (待廢棄, 勿用)
 *         title: '4/26  ~  5/2',   // 簡單字符串表示
 *         beginDate: 26,           // 開始日期的日份
 *         endDate: 2               // 結束日期的日份
 *         beginOfMonth: '4/26',    // 包含月/日的開始日期
 *         endOfMonth: '5/2',       // 包含月/日的結束日期
 *       },
 *       ...
 *       {
 *         begin: '2020-05-31',
 *         end: '2020-06-06',
 *         begin_format: '2020-05-31', // 日期格式化之后的開始時間文本
 *         end_format: '2020-06-06', // 日期格式化之后的結束時間文本
 *         isCurrentWeek: false,
 *         raw: '5/31  ~  6/6',
 *         title: '5/31  ~  6/6',
 *         beginDate: 31,
 *         endDate: 6,
 *         beginOfMonth: '5/31',
 *         endOfMonth: '6/6',
 *       }
 *     ],
 * }
 */
cal.toBothDate(CalendarTypes.WEEKOFMONTH);

6. 靜態(tài)方法:通過起止范圍獲取日歷數組

image

一些時候,我們希望給定一個時間范圍,以此來獲取這個時間范圍中的所有單位(比如日期,或者月份)數組。此時可以使用靜態(tài)方法 getDateUnits(option)。

函數:Calendar.getDateUnits(option)
獲取某范圍內的所有日期單位, 范圍確定方式有兩種:

  1. 通過 beginend 確定日期范圍。優(yōu)先級靠前(參數都滿足的情況下優(yōu)先使用方案1范圍)
  2. 通過 seed 作為當前的種子日期, 以 incr 作為種子增量, 范圍為 seed ~ (seed + incr) 之間;

參數 :

  • option.begin [String|Date] 起始日期
  • option.end [String|Date] 結束日期
  • option.incr [Integer] 日期增量
  • option.seed [String|Date] 種子日期
  • option.particle [CalendarTypes] 需要返回的日期單位數組粒度,僅支持:YEAR、MONTH、DAY,默認為: CalendarTypes.DAY (返回的數據會包含范圍內的每一天)
  • option.format [String] 返回數據格式化方式,默認為 yyyy-MM-dd
  • option.order [String] 排序方式, 支持方式: ASC(正序)、DESC(倒序), 默認為 ASC
  • 返回值 : {begin_date, end_date, dates}

示例1: 通過 begin 和 end 獲取日期范圍

// 1-1 日范圍篩選
Calendar.getDateUnits({
    begin: '2020-06-29',
    end: '2020-07-4',
});
 
/**
 * 1-1 返回值: 
 * {
 *   begin_date: '2020-06-29',
 *   end_date: '2020-07-04',
 *   dates: [
 *     { year: 2020, month: 5, day: 29, txt: '2020-06-29' },
 *     { year: 2020, month: 5, day: 30, txt: '2020-06-30' },
 *     { year: 2020, month: 6, day: 1, txt: '2020-07-01' },
 *     { year: 2020, month: 6, day: 2, txt: '2020-07-02' },
 *     { year: 2020, month: 6, day: 3, txt: '2020-07-03' },
 *     { year: 2020, month: 6, day: 4, txt: '2020-07-04' }
 *   ]
 * }
 */
 
 
 
// 1-2 月范圍篩選
Calendar.getDateUnits({
    begin: '2020-11', // 范圍可以只到月份
    end: '2021-02-21', // 也可以是具體的日期字符串或 Date 對象
    particle: CalendarTypes.MONTH,
});
 
/**
 * 1-2 返回值: 
 * {
 *   begin_date: '2020-11',
 *   end_date: '2021-02',
 *   dates: [
 *     { year: 2020, month: 10, txt: '2020-11' },
 *     { year: 2020, month: 11, txt: '2020-12' },
 *     { year: 2021, month: 0, txt: '2021-01' },
 *     { year: 2021, month: 1, txt: '2021-02' }
 *   ]
 * }
 */
 
 
 
// 1-3 日范圍篩選
Calendar.getDateUnits({
    begin: '2019-06-29',
    end: '2025',
    particle: CalendarTypes.YEAR,
    order: 'DESC', // 倒序排序
    format: 'yyyy年', // 格式化
});
 
/**
 * 1-3 返回值: 
 * {
 *   begin_date: '2019年',
 *   end_date: '2025年',
 *   dates: [
 *     { year: 2025, txt: '2025年' },
 *     { year: 2024, txt: '2024年' },
 *     { year: 2023, txt: '2023年' },
 *     { year: 2022, txt: '2022年' },
 *     { year: 2021, txt: '2021年' },
 *     { year: 2020, txt: '2020年' },
 *     { year: 2019, txt: '2019年' }
 *   ]
 * }
 */

示例2: 通過 seed 作為當前的種子日期, 以 incr 作為種子增量獲取范圍:

// 2-1 日期向前增加 5 天 (返回包含 seed 日期的 6 條數據)
Calendar.getDateUnits({
    seed: '2020-06-29',
    incr: 5,
});
 
/**
 * 2-1 返回值: 
 * {
 *   begin_date: '2020-06-29',
 *   end_date: '2020-07-04',
 *   dates: [
 *     { year: 2020, month: 5, day: 29, txt: '2020-06-29' },
 *     { year: 2020, month: 5, day: 30, txt: '2020-06-30' },
 *     { year: 2020, month: 6, day: 1, txt: '2020-07-01' },
 *     { year: 2020, month: 6, day: 2, txt: '2020-07-02' },
 *     { year: 2020, month: 6, day: 3, txt: '2020-07-03' },
 *     { year: 2020, month: 6, day: 4, txt: '2020-07-04' }
 *   ]
 * }
 */
 
 
 
// 2-2 日期向后減少 5 天 (返回包含 seed 日期的 6 條數據)
Calendar.getDateUnits({
    seed: '2020-06-2',
    incr: -5, // 增量給與負數
});
 
/**
 * 2-2 返回值: 
 * {
 *   begin_date: '2020-05-28',
 *   end_date: '2020-06-02',
 *   dates: [
 *     { year: 2020, month: 4, day: 28, txt: '2020-05-28' },
 *     { year: 2020, month: 4, day: 29, txt: '2020-05-29' },
 *     { year: 2020, month: 4, day: 30, txt: '2020-05-30' },
 *     { year: 2020, month: 4, day: 31, txt: '2020-05-31' },
 *     { year: 2020, month: 5, day: 1, txt: '2020-06-01' },
 *     { year: 2020, month: 5, day: 2, txt: '2020-06-02' }
 *   ]
 * }
 */

謝謝觀賞,如有問題,或希望改進的地方請在評論區(qū)告知我哦。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容