如何在Js中的toLocaleString()上删除日期和时间之间的逗号 [英] How to remove comma between date and time on toLocaleString() in Js

查看:427
本文介绍了如何在Js中的toLocaleString()上删除日期和时间之间的逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var date ="03/05/2013";
var localDate = date.split("/").reverse().join("-");
var localTime = "20:41"
var UTCDateTime = localDate+ "T" + localTime +":00.000Z";
localDateTime = new Date(UTCDateTime)

var options = { hour12: false, day: '2-digit', month: '2-digit', year: 'numeric', hour: '2-digit', minute:'2-digit' };
console.log("Date:>>"+localDateTime.toLocaleString('en', options));

如何删除日期Date:>>05/03/2013 21:41

推荐答案

我不会用replace函数或正则表达式替换逗号,因为这很容易导致错误的逗号被删除.您在示例中使用的短日期格式在日期部分中不包含逗号,但是较长的日期格式(如{ day: '2-digit', month: 'long', year: 'numeric', hour: '2-digit', minute:'2-digit' })将会出现.

I wouldn't replace the comma with either the replace function or a regex, because that could easily result in the wrong commas being removed. The short date format that you use in your example doesn't contain a comma in the date part, but a longer date format like { day: '2-digit', month: 'long', year: 'numeric', hour: '2-digit', minute:'2-digit' } will.

更具弹性的选项是分别格式化日期和时间部分,并将它们之间用空格隔开:

A more resilient option is to format the date and time parts separately, and concatenate them with a space in between:

var date ="03/05/2013";
var localDate = date.split("/").reverse().join("-");
var localTime = "20:41"
var UTCDateTime = localDate+ "T" + localTime +":00.000Z";
localDateTime = new Date(UTCDateTime)

var dateOptions = { day: '2-digit', month: '2-digit', year: 'numeric' };
var timeOptions = { hour12: false, hour: '2-digit', minute:'2-digit' };
console.log("Date:>>" + localDateTime.toLocaleDateString('en', dateOptions) + " " + localDateTime.toLocaleTimeString('en', timeOptions));

这篇关于如何在Js中的toLocaleString()上删除日期和时间之间的逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆