From f94e3d1c260da133d950f403fac2a34db019b27c Mon Sep 17 00:00:00 2001 From: sight <1453017105@qq.com> Date: Sat, 5 Aug 2023 09:33:39 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E4=BC=98=E5=8C=96=20`util.toDateString`?= =?UTF-8?q?=EF=BC=8C=20=E6=9B=B4=E5=A4=9A=E6=A0=BC=E5=BC=8F=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/util.js | 53 +++++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/src/modules/util.js b/src/modules/util.js index d992f127..2a0197aa 100644 --- a/src/modules/util.js +++ b/src/modules/util.js @@ -258,31 +258,46 @@ layui.define('jquery', function(exports){ //若 null 或空字符,则返回空字符 if(time === null || time === '') return ''; - var that = this - ,date = new Date(function(){ + var REGEX_FORMAT = /\[([^\]]+)]|y{1,4}|M{1,2}|d{1,2}|H{1,2}|h{1,2}|m{1,2}|s{1,2}|SSS/g; + var that = this; + var date = new Date(function(){ if(!time) return; return isNaN(time) ? time : (typeof time === 'string' ? parseInt(time) : time) }() || new Date()) - ,ymd = [ - that.digit(date.getFullYear(), 4) - ,that.digit(date.getMonth() + 1) - ,that.digit(date.getDate()) - ] - ,hms = [ - that.digit(date.getHours()) - ,that.digit(date.getMinutes()) - ,that.digit(date.getSeconds()) - ]; - + if(!date.getDate()) return hint.error('Invalid Msec for "util.toDateString(Msec)"'), ''; + + var years = date.getFullYear(); + var month = date.getMonth(); + var days = date.getDate(); + var hours = date.getHours(); + var minutes = date.getMinutes(); + var seconds = date.getSeconds(); + var milliseconds = date.getMilliseconds(); + + var matches = { + yy: function(){return String(years).slice(-2);}, + yyyy: function(){return that.digit(years, 4);}, + M: function(){return String(month + 1);}, + MM: function(){return that.digit(month + 1);}, + d: function(){return String(days);}, + dd: function(){return that.digit(days);}, + H: function(){return String(hours);}, + HH: function(){return that.digit(hours);}, + h: function(){return String(hours % 12 || 12);}, + hh: function(){return that.digit(hours % 12 || 12);}, + m: function(){return String(minutes);}, + mm: function(){return that.digit(minutes);}, + s: function(){return String(seconds);}, + ss: function(){return that.digit(seconds);}, + SSS: function(){return that.digit(milliseconds, 3);} + } format = format || 'yyyy-MM-dd HH:mm:ss'; - return format.replace(/yyyy/g, ymd[0]) - .replace(/MM/g, ymd[1]) - .replace(/dd/g, ymd[2]) - .replace(/HH/g, hms[0]) - .replace(/mm/g, hms[1]) - .replace(/ss/g, hms[2]); + + return format.replace(REGEX_FORMAT, function(match, $1) { + return $1 || (matches[match] && matches[match]()) || match; + }); }, // 转义 html From a0161b4e6cb7ed6342d41e0a88cbf7497d641b89 Mon Sep 17 00:00:00 2001 From: sight <1453017105@qq.com> Date: Sat, 5 Aug 2023 20:00:43 +0800 Subject: [PATCH 2/8] update code --- src/modules/util.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/modules/util.js b/src/modules/util.js index 2a0197aa..1581de74 100644 --- a/src/modules/util.js +++ b/src/modules/util.js @@ -255,9 +255,11 @@ layui.define('jquery', function(exports){ // 转化为日期格式字符 toDateString: function(time, format){ - //若 null 或空字符,则返回空字符 + // 若 null 或空字符,则返回空字符 if(time === null || time === '') return ''; - + + // 引用自 dayjs + // https://github.com/iamkun/dayjs/blob/v1.11.9/src/constant.js#L30 var REGEX_FORMAT = /\[([^\]]+)]|y{1,4}|M{1,2}|d{1,2}|H{1,2}|h{1,2}|m{1,2}|s{1,2}|SSS/g; var that = this; var date = new Date(function(){ From 754c517c18b799462525e28f6913f3ce6c8dd326 Mon Sep 17 00:00:00 2001 From: sight <1453017105@qq.com> Date: Sun, 6 Aug 2023 19:40:15 +0800 Subject: [PATCH 3/8] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20util.toDateString=20?= =?UTF-8?q?=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/util/index.md | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/docs/util/index.md b/docs/util/index.md index 44591b52..4a3fac95 100644 --- a/docs/util/index.md +++ b/docs/util/index.md @@ -100,12 +100,35 @@ var result = util.timeAgo(1672531200000); // 2023-01-01 00:00:00 `var result = util.toDateString(time, format);` - 参数 `time` : 毫秒数或日期对象 -- 参数 `format` : 日期字符格式。默认格式:`yyyy-MM-dd HH:mm:ss` 。可自定义,如: `yyyy年MM月dd日` +- 参数 `format` : 日期字符格式。默认格式:`yyyy-MM-dd HH:mm:ss` 。可自定义,如: `yyyy年MM月dd日`。 ``` var result = util.toDateString(1672531200000, 'yyyy-MM-dd'); // 2023-01-01 + +// 中括号中的字符会原样保留 2.8.13+ +var result2 = util.toDateString(new Date('2023-01-01 11:35:25'), 'ss[s]'); // 25s ``` +所有可用格式列表 + +| 格式 | 示例 | 描述 | +| --- | --- | --- | +| yy 2.8.13+ | 23 | 年,两位数 | +| yyyy | 2023 | 年,四位数 | +| M 2.8.13+ | 1-12 | 月 | +| MM | 01-12 | 月,两位数 | +| d 2.8.13+ | 1-31 | 日 | +| dd | 01-31 | 日,两位数 | +| H 2.8.13+ | 0-23 | 小时 | +| HH | 00-23 | 小时,两位数 | +| h 2.8.13+ | 1-12 | 小时,12 小时制 | +| hh 2.8.13+ | 01-12 | 小时,12 小时制,两位数 | +| m 2.8.13+ | 0-59 | 分钟 | +| mm | 00-59 | 分钟,两位数 | +| s 2.8.13+ | 0-59 | 秒 | +| ss | 00-59 | 秒,两位数 | +| SSS 2.8.13+ | 000-999 | 毫秒,三位数 | +

数字前置补零

`util.digit(num, length);` From 36f70d74f9a342063a190d40aebb7d759e299a69 Mon Sep 17 00:00:00 2001 From: sight <1453017105@qq.com> Date: Sun, 6 Aug 2023 22:26:34 +0800 Subject: [PATCH 4/8] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20`util.toDateString`=20?= =?UTF-8?q?meridiem=20=E6=A0=BC=E5=BC=8F=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/util/index.md | 3 ++- src/modules/util.js | 19 ++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/docs/util/index.md b/docs/util/index.md index 4a3fac95..de955bb5 100644 --- a/docs/util/index.md +++ b/docs/util/index.md @@ -100,7 +100,7 @@ var result = util.timeAgo(1672531200000); // 2023-01-01 00:00:00 `var result = util.toDateString(time, format);` - 参数 `time` : 毫秒数或日期对象 -- 参数 `format` : 日期字符格式。默认格式:`yyyy-MM-dd HH:mm:ss` 。可自定义,如: `yyyy年MM月dd日`。 +- 参数 `format` : 日期字符格式。默认格式:`yyyy-MM-dd HH:mm:ss` 。可自定义,如: `yyyy年MM月dd日` ``` var result = util.toDateString(1672531200000, 'yyyy-MM-dd'); // 2023-01-01 @@ -123,6 +123,7 @@ var result2 = util.toDateString(new Date('2023-01-01 11:35:25'), 'ss[s]'); // 25 | HH | 00-23 | 小时,两位数 | | h 2.8.13+ | 1-12 | 小时,12 小时制 | | hh 2.8.13+ | 01-12 | 小时,12 小时制,两位数 | +| A 2.8.13+ | 凌晨/早上/上午/中午/下午/晚上 | 时段 | | m 2.8.13+ | 0-59 | 分钟 | | mm | 00-59 | 分钟,两位数 | | s 2.8.13+ | 0-59 | 秒 | diff --git a/src/modules/util.js b/src/modules/util.js index 1581de74..1ae8740b 100644 --- a/src/modules/util.js +++ b/src/modules/util.js @@ -260,7 +260,7 @@ layui.define('jquery', function(exports){ // 引用自 dayjs // https://github.com/iamkun/dayjs/blob/v1.11.9/src/constant.js#L30 - var REGEX_FORMAT = /\[([^\]]+)]|y{1,4}|M{1,2}|d{1,2}|H{1,2}|h{1,2}|m{1,2}|s{1,2}|SSS/g; + var REGEX_FORMAT = /\[([^\]]+)]|y{1,4}|M{1,2}|d{1,2}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|SSS/g; var that = this; var date = new Date(function(){ if(!time) return; @@ -277,6 +277,22 @@ layui.define('jquery', function(exports){ var seconds = date.getSeconds(); var milliseconds = date.getMilliseconds(); + var meridiem = function(hour, minute){ + var hm = hour * 100 + minute; + if (hm < 600) { + return '凌晨'; + } else if (hm < 900) { + return '早上'; + } else if (hm < 1100) { + return '上午'; + } else if (hm < 1300) { + return '中午'; + } else if (hm < 1800) { + return '下午'; + } + return '晚上'; + }; + var matches = { yy: function(){return String(years).slice(-2);}, yyyy: function(){return that.digit(years, 4);}, @@ -288,6 +304,7 @@ layui.define('jquery', function(exports){ HH: function(){return that.digit(hours);}, h: function(){return String(hours % 12 || 12);}, hh: function(){return that.digit(hours % 12 || 12);}, + A: function(){return meridiem(hours, minutes);}, m: function(){return String(minutes);}, mm: function(){return that.digit(minutes);}, s: function(){return String(seconds);}, From 81906c4f7c51105bbfc95b483c351dc3420c8e52 Mon Sep 17 00:00:00 2001 From: sight <1453017105@qq.com> Date: Sun, 6 Aug 2023 22:27:30 +0800 Subject: [PATCH 5/8] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20util=20=E7=A4=BA?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/util.html | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/examples/util.html b/examples/util.html index b4e9f1fa..96fc5f4b 100644 --- a/examples/util.html +++ b/examples/util.html @@ -29,6 +29,14 @@ body{padding: 50px;}
+请编辑格式: +
+ +
+ + +
+
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
@@ -121,6 +129,17 @@ layui.use(['util', 'layer'], function(){ alert(othis.html()) } }); + + // 转换日期格式 + var timer = null + if (timer) clearInterval(timer) + var toDateString = function (format) { + var dateString = util.toDateString(new Date(), format) // 执行转换日期格式的方法 + $('#test3').html(dateString) + } + timer = setInterval(() => { + toDateString($('#test2').val()) + }, 50) }); From 4fba81d3a34cd9b6ac70c0c046c14dcaa786b704 Mon Sep 17 00:00:00 2001 From: sight <1453017105@qq.com> Date: Sun, 6 Aug 2023 23:05:52 +0800 Subject: [PATCH 6/8] =?UTF-8?q?`util.toDateString`=20=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=20meridiem?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/util/index.md | 23 ++++++++++++++++++++--- examples/util.html | 6 +++++- src/modules/util.js | 8 +++++--- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/docs/util/index.md b/docs/util/index.md index de955bb5..cd386a29 100644 --- a/docs/util/index.md +++ b/docs/util/index.md @@ -97,19 +97,36 @@ var result = util.timeAgo(1672531200000); // 2023-01-01 00:00:00

转换日期格式字符

-`var result = util.toDateString(time, format);` +`var result = util.toDateString(time, format, options);` - 参数 `time` : 毫秒数或日期对象 - 参数 `format` : 日期字符格式。默认格式:`yyyy-MM-dd HH:mm:ss` 。可自定义,如: `yyyy年MM月dd日` +- 参数 `options` 2.8.13+ : 该方法的属性可选项,详见下表: + +| 属性名 | 描述 | 类型 | 默认值 | +| --- | --- | --- | --- | +| customMeridiem | 自定义 meridiem 格式 | Function | - | ``` var result = util.toDateString(1672531200000, 'yyyy-MM-dd'); // 2023-01-01 // 中括号中的字符会原样保留 2.8.13+ var result2 = util.toDateString(new Date('2023-01-01 11:35:25'), 'ss[s]'); // 25s + +// 自定义 meridiem +var result3 = util.toDateString( + '2023-01-01 11:35:25', + 'hh:mm:ss A' + { + customMeridiem: function(hours, minutes){ + return (hours < 12 ? 'AM' : 'PM') + //.split('').join('.') // 有句点,A.M. + //.toLowerCase() // 小写,a.m. + } +); // 11:35:25 AM ``` -所有可用格式列表 +所有可用的格式列表 | 格式 | 示例 | 描述 | | --- | --- | --- | @@ -123,7 +140,7 @@ var result2 = util.toDateString(new Date('2023-01-01 11:35:25'), 'ss[s]'); // 25 | HH | 00-23 | 小时,两位数 | | h 2.8.13+ | 1-12 | 小时,12 小时制 | | hh 2.8.13+ | 01-12 | 小时,12 小时制,两位数 | -| A 2.8.13+ | 凌晨/早上/上午/中午/下午/晚上 | 时段 | +| A 2.8.13+ | 凌晨/早上/上午/中午/下午/晚上 | meridiem | | m 2.8.13+ | 0-59 | 分钟 | | mm | 00-59 | 分钟,两位数 | | s 2.8.13+ | 0-59 | 秒 | diff --git a/examples/util.html b/examples/util.html index 96fc5f4b..a8eba5da 100644 --- a/examples/util.html +++ b/examples/util.html @@ -134,7 +134,11 @@ layui.use(['util', 'layer'], function(){ var timer = null if (timer) clearInterval(timer) var toDateString = function (format) { - var dateString = util.toDateString(new Date(), format) // 执行转换日期格式的方法 + var dateString = util.toDateString(new Date(), format, {customMeridiem: function(hours, minutes){ + return (hours < 12 ? 'AM' : 'PM') + //.split('').join('.') // 有句点 '.' + //.toLowerCase() // 小写 + }}); $('#test3').html(dateString) } timer = setInterval(() => { diff --git a/src/modules/util.js b/src/modules/util.js index 1ae8740b..9bcd4acf 100644 --- a/src/modules/util.js +++ b/src/modules/util.js @@ -254,7 +254,7 @@ layui.define('jquery', function(exports){ }, // 转化为日期格式字符 - toDateString: function(time, format){ + toDateString: function(time, format, options){ // 若 null 或空字符,则返回空字符 if(time === null || time === '') return ''; @@ -277,8 +277,8 @@ layui.define('jquery', function(exports){ var seconds = date.getSeconds(); var milliseconds = date.getMilliseconds(); - var meridiem = function(hour, minute){ - var hm = hour * 100 + minute; + var defaultMeridiem = function(hours, minutes){ + var hm = hours * 100 + minutes; if (hm < 600) { return '凌晨'; } else if (hm < 900) { @@ -293,6 +293,8 @@ layui.define('jquery', function(exports){ return '晚上'; }; + var meridiem = options.customMeridiem || defaultMeridiem; + var matches = { yy: function(){return String(years).slice(-2);}, yyyy: function(){return that.digit(years, 4);}, From ba806543a5c63e414848b916b0ec0001a2726893 Mon Sep 17 00:00:00 2001 From: sight <1453017105@qq.com> Date: Sun, 6 Aug 2023 23:18:07 +0800 Subject: [PATCH 7/8] update code --- examples/util.html | 6 +----- src/modules/util.js | 2 +- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/examples/util.html b/examples/util.html index a8eba5da..2e904b2c 100644 --- a/examples/util.html +++ b/examples/util.html @@ -134,11 +134,7 @@ layui.use(['util', 'layer'], function(){ var timer = null if (timer) clearInterval(timer) var toDateString = function (format) { - var dateString = util.toDateString(new Date(), format, {customMeridiem: function(hours, minutes){ - return (hours < 12 ? 'AM' : 'PM') - //.split('').join('.') // 有句点 '.' - //.toLowerCase() // 小写 - }}); + var dateString = util.toDateString(new Date(), format); $('#test3').html(dateString) } timer = setInterval(() => { diff --git a/src/modules/util.js b/src/modules/util.js index 9bcd4acf..6430b310 100644 --- a/src/modules/util.js +++ b/src/modules/util.js @@ -293,7 +293,7 @@ layui.define('jquery', function(exports){ return '晚上'; }; - var meridiem = options.customMeridiem || defaultMeridiem; + var meridiem = (options && options.customMeridiem) || defaultMeridiem; var matches = { yy: function(){return String(years).slice(-2);}, From 2d5529cbe65d9b6d0f6294745a1ab516cda4ad27 Mon Sep 17 00:00:00 2001 From: sight <1453017105@qq.com> Date: Mon, 7 Aug 2023 08:53:15 +0800 Subject: [PATCH 8/8] update code --- examples/util.html | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/util.html b/examples/util.html index 2e904b2c..432ca666 100644 --- a/examples/util.html +++ b/examples/util.html @@ -132,7 +132,6 @@ layui.use(['util', 'layer'], function(){ // 转换日期格式 var timer = null - if (timer) clearInterval(timer) var toDateString = function (format) { var dateString = util.toDateString(new Date(), format); $('#test3').html(dateString)