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] =?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);},