我的日期格式为181221171403
18-year
12-month
21-day
17-hour
14-minute
03-second
如何转换以下格式。
December 21st 2018, 17:14:03.000
答案 0 :(得分:1)
var str = '181221171403';
var dateArr = str.match(/.{1,2}/g); // given ["18", "12", "21", "17", "14", "03"]
var strDate = '20'+dateArr[0]+'-'+dateArr[1]+'-'+dateArr[2]+' '+dateArr[3]+':'+dateArr[4]+':'+dateArr[5];
var d = new Date(strDate);
const monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
// December 21st 2018, 17:14:03.000
const res = monthNames[d.getMonth()]+' '+d.getDate()+' '+d.getFullYear()+' '+d.getFullYear()+' '+d.getHours()+':'+d.getMinutes()+':'+d.getSeconds()+'.000';
console.log(res);
然后您可以使用objectv JavaScript日期以shouaite格式生成日期
答案 1 :(得分:1)
您可以使用moment.js来完成这项工作。 moment documentation
中的更多内容
var stringDate = '181221171403';
var date = moment(stringDate, 'YYMMDDHHmmss');
console.log(date.format('MMMM Do YYYY, HH:mm:ss'));
<script src="https://momentjs.com/downloads/moment.js"></script>