`util.toDateString` 支持自定义 meridiem

pull/1314/head
sight 2023-08-06 23:05:52 +08:00
parent 81906c4f7c
commit 4fba81d3a3
3 changed files with 30 additions and 7 deletions

View File

@ -97,19 +97,36 @@ var result = util.timeAgo(1672531200000); // 2023-01-01 00:00:00
<h3 id="toDateString" class="ws-anchor ws-bold">转换日期格式字符</h3> <h3 id="toDateString" class="ws-anchor ws-bold">转换日期格式字符</h3>
`var result = util.toDateString(time, format);` `var result = util.toDateString(time, format, options);`
- 参数 `time` : 毫秒数或日期对象 - 参数 `time` : 毫秒数或日期对象
- 参数 `format` : 日期字符格式。默认格式:`yyyy-MM-dd HH:mm:ss` 。可自定义,如: `yyyy年MM月dd日` - 参数 `format` : 日期字符格式。默认格式:`yyyy-MM-dd HH:mm:ss` 。可自定义,如: `yyyy年MM月dd日`
- 参数 `options` <sup>2.8.13+</sup> : 该方法的属性可选项,详见下表:
| 属性名 | 描述 | 类型 | 默认值 |
| --- | --- | --- | --- |
| customMeridiem | 自定义 meridiem 格式 | Function | - |
``` ```
var result = util.toDateString(1672531200000, 'yyyy-MM-dd'); // 2023-01-01 var result = util.toDateString(1672531200000, 'yyyy-MM-dd'); // 2023-01-01
// 中括号中的字符会原样保留 2.8.13+ // 中括号中的字符会原样保留 2.8.13+
var result2 = util.toDateString(new Date('2023-01-01 11:35:25'), 'ss[s]'); // 25s 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 | 小时,两位数 | | HH | 00-23 | 小时,两位数 |
| h <sup>2.8.13+</sup> | 1-12 | 小时12 小时制 | | h <sup>2.8.13+</sup> | 1-12 | 小时12 小时制 |
| hh <sup>2.8.13+</sup> | 01-12 | 小时12 小时制,两位数 | | hh <sup>2.8.13+</sup> | 01-12 | 小时12 小时制,两位数 |
| A <sup>2.8.13+</sup> | 凌晨/早上/上午/中午/下午/晚上 | 时段 | | A <sup>2.8.13+</sup> | 凌晨/早上/上午/中午/下午/晚上 | meridiem |
| m <sup>2.8.13+</sup> | 0-59 | 分钟 | | m <sup>2.8.13+</sup> | 0-59 | 分钟 |
| mm | 00-59 | 分钟,两位数 | | mm | 00-59 | 分钟,两位数 |
| s <sup>2.8.13+</sup> | 0-59 | 秒 | | s <sup>2.8.13+</sup> | 0-59 | 秒 |

View File

@ -134,7 +134,11 @@ layui.use(['util', 'layer'], function(){
var timer = null var timer = null
if (timer) clearInterval(timer) if (timer) clearInterval(timer)
var toDateString = function (format) { 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) $('#test3').html(dateString)
} }
timer = setInterval(() => { timer = setInterval(() => {

View File

@ -254,7 +254,7 @@ layui.define('jquery', function(exports){
}, },
// 转化为日期格式字符 // 转化为日期格式字符
toDateString: function(time, format){ toDateString: function(time, format, options){
// 若 null 或空字符,则返回空字符 // 若 null 或空字符,则返回空字符
if(time === null || time === '') return ''; if(time === null || time === '') return '';
@ -277,8 +277,8 @@ layui.define('jquery', function(exports){
var seconds = date.getSeconds(); var seconds = date.getSeconds();
var milliseconds = date.getMilliseconds(); var milliseconds = date.getMilliseconds();
var meridiem = function(hour, minute){ var defaultMeridiem = function(hours, minutes){
var hm = hour * 100 + minute; var hm = hours * 100 + minutes;
if (hm < 600) { if (hm < 600) {
return '凌晨'; return '凌晨';
} else if (hm < 900) { } else if (hm < 900) {
@ -293,6 +293,8 @@ layui.define('jquery', function(exports){
return '晚上'; return '晚上';
}; };
var meridiem = options.customMeridiem || defaultMeridiem;
var matches = { var matches = {
yy: function(){return String(years).slice(-2);}, yy: function(){return String(years).slice(-2);},
yyyy: function(){return that.digit(years, 4);}, yyyy: function(){return that.digit(years, 4);},