我想使用BRACKET_CLOSED
格式的JavaScript日期,使用JavaScript的2018-03-29 16:02
,格式为yyyy-mm-dd hh-mm
。
这是我正在使用以下代码的当前代码。我对当前的实施感到不满意,因为它的执行时间很长。
为此,我已经参考了这篇MDN文章,但可以找出解决方案。
Date.prototype.toLocaleString()
我想使上述功能类似:
function _formatDateTime(timestamp: number): string {
const timestampDate = new Date(timestamp);
let month = timestampDate.getUTCMonth() + 1 + '';
let date = timestampDate.getUTCDate() + '';
const year = timestampDate.getUTCFullYear();
if (month.length === 1) month = '0' + month;
if (date.length === 1) date = '0' + date;
const dateLabel = [year, month, date].join('-');
const time = _formatTime(timestamp);
return [dateLabel, time].join(' ');
}
function _formatTime(timestamp: number): string {
const timestampDate = new Date(timestamp);
let hours = timestampDate.getUTCHours() + '';
let minutes = timestampDate.getUTCMinutes() + '';
if (hours.length === 1) hours = '0' + hours;
if (minutes.length === 1) minutes = '0' + minutes;
return [hours, minutes].join(':');
}
在这里,我们需要在function _formatDateTime(timestamp: number): string {
return new Date(timestamp).toLocaleString();
}
函数的参数中提供语言环境和选项。