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
答案 0 :(得分:3)
我不会使用replace
函数或正则表达式替换逗号,因为这很容易导致删除错误的逗号。您在示例中使用的短日期格式在日期部分中不包含逗号,但更长的日期格式(如{ day: '2-digit', month: 'long', year: 'numeric', hour: '2-digit', minute:'2-digit' }
)将会出现。
更具弹性的选项是分别格式化日期和时间部分,并将它们与之间的空格连接起来:
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));
答案 1 :(得分:1)
使用replace
:
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).replace(',',''));

答案 2 :(得分:1)
关于使用正则表达式的内容是什么?
'01/01/2018, 12:00'.replace(/^(\d{2})\/(\d{2})\/(\d{4}), (\d{2}):(\d{2})/, '$1/$2/$3 $4:$5')
var date = '03/05/2013';
var localDate = date.split('/').reverse().join('-');
var localTime = '20:41';
var localDateTime = new Date(localDate+ 'T' + localTime + ':00.000Z');
var options = {hour12: false, day: '2-digit', month: '2-digit', year: 'numeric', hour: '2-digit', minute:'2-digit'};
var output = localDateTime.toLocaleString('en', options);
var outputFormatted = output.replace(/^(\d{2})\/(\d{2})\/(\d{4}), (\d{2}):(\d{2})/, '$1/$2/$3 $4:$5');
console.log(outputFormatted);