1- # timeSolver 使用說明
1+ # timeSolver Usage
22
3- 這份文件說明 ` timeSolver ` 函式庫的主要方法、參數與常用格式範例,方便開發者快速上手。
3+ This document describes the main functions, parameters, and common format examples for the ` timeSolver ` library to help developers get started quickly.
44
5- ## 安裝與引入
5+ ## Installation and Import
66
77CommonJS:
88
@@ -21,27 +21,27 @@ Browser (UMD bundle):
2121``` html
2222<script src =" dist/timeSolver.umd.min.js" ></script >
2323<script >
24- // 全域物件 timeSolver 可直接使用
24+ // global ` timeSolver` is available
2525 console .log (timeSolver .getString (new Date (), ' YYYYMMDD' ));
2626 </script >
2727```
2828
29- ## 常用方法總覽
29+ ## Common API Overview
3030
31- - ` timeSolver.add(date, count, unit) ` — 在 ` date ` 上加上 ` count ` 個 ` unit ` 。
32- - ` timeSolver.subtract(date, count, unit) ` — 在 ` date ` 上減去 ` count ` 個 ` unit ` 。
33- - ` timeSolver.between(d1, d2, unit) ` — 回傳 ` d2 - d1 ` 的差距,單位為 ` unit ` 。
34- - ` timeSolver.equal(d1, d2) ` — 判斷兩個日期是否相同(字串比對)。
35- - ` timeSolver.after(d1, d2, unit) ` — 判斷 ` d1 ` 是否在 ` d2 ` 之後(以 ` unit ` 計算)。
36- - ` timeSolver.before(d1, d2, unit) ` — 判斷 ` d1 ` 是否在 ` d2 ` 之前(以 ` unit ` 計算)。
37- - ` timeSolver.afterToday(d) ` / ` timeSolver.beforeToday(d) ` — 相對於今天的判斷。
38- - ` timeSolver.getString(date, format) ` — 將 ` date ` 轉成指定格式的字串。
39- - ` timeSolver.isValid(dateString, format?) ` — 驗證字串是否為合法日期;若提供 ` format ` ,則以指定格式驗證。
40- - ` timeSolver.getAbbrWeek(date) ` / ` timeSolver.getFullWeek(date) ` — 取得星期(縮寫或全名)。
41- - ` timeSolver.getAbbrMonth(date) ` / ` timeSolver.getFullMonth(date) ` — 取得月份(縮寫或全名)。
42- - ` timeSolver.getQuarterByMonth(m) ` / ` timeSolver.getFirstMonthByQuarter(q) ` — 季度工具。
31+ - ` timeSolver.add(date, count, unit) ` — Add ` count ` units to ` date ` .
32+ - ` timeSolver.subtract(date, count, unit) ` — Subtract ` count ` units from ` date ` .
33+ - ` timeSolver.between(d1, d2, unit) ` — Return the difference ` d2 - d1 ` in ` unit ` .
34+ - ` timeSolver.equal(d1, d2) ` — Check whether two dates are equal (string comparison).
35+ - ` timeSolver.after(d1, d2, unit) ` — Check whether ` d1 ` is after ` d2 ` (by ` unit ` ).
36+ - ` timeSolver.before(d1, d2, unit) ` — Check whether ` d1 ` is before ` d2 ` (by ` unit ` ).
37+ - ` timeSolver.afterToday(d) ` / ` timeSolver.beforeToday(d) ` — Compare relative to today.
38+ - ` timeSolver.getString(date, format) ` — Format ` date ` as a string using ` format ` .
39+ - ` timeSolver.isValid(dateString, format?) ` — Validate a date string; if ` format ` is provided, validate against that format.
40+ - ` timeSolver.getAbbrWeek(date) ` / ` timeSolver.getFullWeek(date) ` — Get weekday (abbr or full name).
41+ - ` timeSolver.getAbbrMonth(date) ` / ` timeSolver.getFullMonth(date) ` — Get month (abbr or full name).
42+ - ` timeSolver.getQuarterByMonth(m) ` / ` timeSolver.getFirstMonthByQuarter(q) ` — Quarter utilities.
4343
44- ### 範例
44+ ### Examples
4545
4646``` js
4747const d = new Date (' 2020-01-01T00:00:00Z' );
@@ -51,57 +51,57 @@ timeSolver.between('2020-01-01','2020-01-02','H'); // 24
5151timeSolver .getString (d, ' YYYY-MM-DD HH:MM:SS.SSS' ); // e.g. '2020-01-01 00:00:00.000'
5252```
5353
54- ## 支援的時間單位 ( unit)
54+ ## Supported Time Units ( ` unit ` )
5555
56- 函式內部接受字串或縮寫,會轉成對應的單位編號。可使用的值包含:
56+ The library accepts various strings or abbreviations for units and converts them to internal unit indices. Supported values include:
5757
58- - ` MILLISECOND ` 或 ` mill ` 或 不給(預設)
59- - ` SECOND ` 或 ` S ` 或 ` s `
60- - ` MINUTE ` 或 ` MIN `
61- - ` HOUR ` 或 ` H `
62- - ` DAY ` 或 ` D `
63- - ` MONTH ` 或 ` M `
64- - ` YEAR ` 或 ` Y `
58+ - ` MILLISECOND ` or ` mill ` or omitted (default)
59+ - ` SECOND ` or ` S ` or ` s `
60+ - ` MINUTE ` or ` MIN `
61+ - ` HOUR ` or ` H `
62+ - ` DAY ` or ` D `
63+ - ` MONTH ` or ` M `
64+ - ` YEAR ` or ` Y `
6565
66- 例如: ` timeSolver.add(date, 5, 'H') ` 表示加 5 小時。
66+ Example: ` timeSolver.add(date, 5, 'H') ` adds 5 hours.
6767
68- ## getString 支援的格式
68+ ## ` getString ` Supported Formats
6969
70- ` timeSolver.getString(date, format) ` 支援下列格式字串(大小寫會被標準化):
70+ ` timeSolver.getString(date, format) ` supports the following format patterns (case-insensitive):
7171
72- - ` YYYY ` — 年份,例如 ` 2020 `
72+ - ` YYYY ` — year, e.g. ` 2020 `
7373- ` YYYYMM ` — ` 202001 `
7474- ` YYYYMMDD ` — ` 20200101 `
75- - ` YYYY/MM/DD ` , ` YYYY-MM-DD ` , ` YYYY.MM.DD ` — 常見日期分隔格式
76- - ` MMDDYYYY ` , ` DDMMYYYY ` — 月日年或日月年順序
77- - 帶時間的格式:
75+ - ` YYYY/MM/DD ` , ` YYYY-MM-DD ` , ` YYYY.MM.DD ` — common separators
76+ - ` MMDDYYYY ` , ` DDMMYYYY ` — month-day-year or day-month-year orders
77+ - Date & time formats:
7878 - ` YYYY/MM/DD HH:MM:SS `
79- - ` YYYY/MM/DD HH:MM:SS.SSS ` (含毫秒)
79+ - ` YYYY/MM/DD HH:MM:SS.SSS ` (milliseconds)
8080 - ` YYYY-MM-DD HH:MM:SS ` / ` YYYY-MM-DD HH:MM:SS.SSS `
8181 - ` YYYY.MM.DD HH:MM:SS ` / ` YYYY.MM.DD HH:MM:SS.SSS `
8282 - ` YYYYMMDD HH:MM:SS ` / ` YYYYMMDD HH:MM:SS.SSS `
8383 - ` MM/DD/YYYY HH:MM:SS ` / ` MM/DD/YYYY HH:MM:SS.SSS `
8484 - ` MM-DD-YYYY HH:MM:SS ` / ` MM-DD-YYYY HH:MM:SS.SSS `
8585 - ` MM.DD.YYYY HH:MM:SS ` / ` MM.DD.YYYY HH:MM:SS.SSS `
86- - 時間 -only: ` HH:MM:SS ` / ` HH:MM:SS.SSS `
86+ - Time -only: ` HH:MM:SS ` / ` HH:MM:SS.SSS `
8787
88- 範例:
88+ Example:
8989
9090``` js
9191timeSolver .getString (new Date (' 2020-06-15T13:45:30.123Z' ), ' YYYY-MM-DD HH:MM:SS.SSS' )
9292// => "2020-06-15 13:45:30.123"
9393```
9494
95- ## isValid 使用說明
95+ ## ` isValid ` Usage
9696
97- - ` timeSolver.isValid('2020/01/01') ` → ` true ` (若 format 未給則使用 Date 解析)
98- - ` timeSolver.isValid('2020/02/30', 'YYYY/MM/DD') ` → ` false ` (不合法日期)
97+ - ` timeSolver.isValid('2020/01/01') ` → ` true ` (when ` format ` is omitted, ` Date ` parsing is used)
98+ - ` timeSolver.isValid('2020/02/30', 'YYYY/MM/DD') ` → ` false ` (invalid date)
9999
100- 當 ` format ` 被提供時,會根據內建的正規表達式驗證日期與(若含時間)時間格式,並在必要時檢查時間部分是否存在。
100+ When ` format ` is provided, the library validates the date (and time portion, if present) using built-in patterns and performs additional checks when necessary.
101101
102- ## timeLook(簡易效能量測)
102+ ## ` timeLook ` (lightweight timing profiler)
103103
104- 用來標記程式執行區段並列印報表:
104+ Use ` timeLook ` to mark code sections and print a report:
105105
106106``` js
107107timeSolver .timeLookStart ();
@@ -112,11 +112,11 @@ timeSolver.timeLook('step2');
112112timeSolver .timeLookReport ();
113113```
114114
115- 報表會在 console 顯示每段的花費時間與相對百分比,並標記最耗時的段落。
115+ The report prints each segment's elapsed time and relative percentage, highlighting the most time-consuming section.
116116
117- ## 其他資訊
117+ ## Additional Notes
118118
119- - 函式大多接受 ` Date ` 物件或可被 ` new Date(...) ` 解析的字串作為日期參數。
120- - 若輸入無效日期,會在內部 ` console.error ` 並回傳 ` null ` (例如 ` _v ` 檢查)。
119+ - Most functions accept a ` Date ` object or a string parseable by ` new Date(...) ` .
120+ - For invalid input dates the library logs an internal ` console.error ` and returns ` null ` .
121121
122- 若你需要,我可以把這份文件翻譯成英文、或把每個方法拆成單獨的示例檔案放在 ` docs/examples/ ` 。
122+ If you want, I can also split this document into separate examples under ` docs/examples/ ` .
0 commit comments