2023-03-14 16:50:00 vue3 去掉年份
如果您想从日期中去掉年份,可以使用Date对象的toLocaleDateString方法,并将其与options对象一起使用以指定日期格式。在options对象中,将year属性设置为undefined,即可从日期中去掉年份。
以下是一个示例代码:
const date = new Date('2023-03-14 16:50:00');
const options = {
month: 'numeric',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
hour12: false,
year: undefined
};
const formattedDate = date.toLocaleDateString('en-US', options);
console.log(formattedDate); // "3/14, 16:50"
在上面的示例代码中,我们首先创建了一个Date对象,然后定义了一个options对象,该对象指定了日期格式,并将year属性设置为undefined。最后,我们调用toLocaleDateString方法,并将它们作为参数传递给它,以获取格式化后的日期字符串。
输出结果为:"3/14, 16:50",其中年份已被去掉。
原文地址: https://www.cveoy.top/t/topic/qyO 著作权归作者所有。请勿转载和采集!