mirror of https://github.com/layui/layui
feat(util): `util.toDateString` 规范化字符串日期 (#2543)
parent
7075fddfeb
commit
e029605793
|
@ -217,6 +217,16 @@ layui.use(['lay', 'util', 'layer'], function(){
|
||||||
toDateString($('#test2').val())
|
toDateString($('#test2').val())
|
||||||
}, 50)
|
}, 50)
|
||||||
|
|
||||||
|
console.log(util.toDateString('2025-3-8 12:30:00') === '2025-03-08 12:30:00');
|
||||||
|
console.log(util.toDateString('2025-03-08 12:30:00') === '2025-03-08 12:30:00');
|
||||||
|
console.log(util.toDateString('2025-03-08') === '2025-03-08 00:00:00');
|
||||||
|
console.log(util.toDateString('2025-3-8') === '2025-03-08 00:00:00');
|
||||||
|
console.log(util.toDateString('2025-3') === '2025-03-01 00:00:00');
|
||||||
|
console.log(util.toDateString(null) === '');
|
||||||
|
console.log(util.toDateString(undefined) === util.toDateString(new Date()));
|
||||||
|
console.log(util.toDateString('') === '');
|
||||||
|
console.log(util.toDateString('2025/3/8 12:30:00') === '2025-03-08 12:30:00');
|
||||||
|
console.log(util.toDateString('2025/3/8') === '2025-03-08 00:00:00');
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
|
|
@ -261,11 +261,36 @@ layui.define('jquery', function(exports){
|
||||||
// 引用自 dayjs
|
// 引用自 dayjs
|
||||||
// https://github.com/iamkun/dayjs/blob/v1.11.9/src/constant.js#L30
|
// 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}|a|A|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 REGEX_PARSE = /^(\d{4})[-/]?(\d{1,2})?[-/]?(\d{0,2})[T\s]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?[.:]?(\d+)?$/i;
|
||||||
var that = this;
|
var that = this;
|
||||||
var date = new Date(function(){
|
|
||||||
if(!time) return;
|
var normalizeDate = function(date) {
|
||||||
return isNaN(time) ? time : (typeof time === 'string' ? parseInt(time) : time)
|
if(typeof date === 'undefined'){
|
||||||
}() || new Date())
|
return new Date();
|
||||||
|
}
|
||||||
|
if(!isNaN(date)){
|
||||||
|
return new Date(typeof date === 'string' ? parseInt(date) : date);
|
||||||
|
}
|
||||||
|
if(typeof date === 'string' && !/Z$/i.test(date)){
|
||||||
|
var d = date.match(REGEX_PARSE);
|
||||||
|
if(d){
|
||||||
|
var m = d[2] - 1 || 0;
|
||||||
|
var ms = (d[7] || '0').substring(0, 3);
|
||||||
|
return new Date(
|
||||||
|
d[1],
|
||||||
|
m,
|
||||||
|
d[3] || 1,
|
||||||
|
d[4] || 0,
|
||||||
|
d[5] || 0,
|
||||||
|
d[6] || 0,
|
||||||
|
ms
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Date(date)
|
||||||
|
}
|
||||||
|
var date = normalizeDate(time);
|
||||||
|
|
||||||
if(!date.getDate()) return hint.error('Invalid millisecond for "util.toDateString(millisecond)"'), '';
|
if(!date.getDate()) return hint.error('Invalid millisecond for "util.toDateString(millisecond)"'), '';
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue