3 回答
TA贡献1878条经验 获得超4个赞
让日期=新日期(document.querySelector('#myDate').value)
日期部分 ===> date.toLocaleDateString() 。
时间部分 ====> date.toLocaleTimeString() 用于时间部分。
TA贡献1799条经验 获得超9个赞
使用它来将您的日期转换为所需的格式,
function formatDate(date) {
date = date.split(" ");
let dateOne = date[0];
let time = date[1];
time = time.split(":");
time.forEach((val,index)=>{
if(val.length <2){
val = "0" + val;
time[index] = val;
}
})
time = time.join(":");
let str = `${dateOne}T${time}.000Z`;
let offset = (new Date()).getTimezoneOffset()*60*1000;
date = new Date( (new Date(`${dateOne}T${time}.000Z`)).getTime() + offset);
const arr = [date.getMonth() + 1, date.getDate(), date.getFullYear()]
var hours = date.getHours();
var minutes = date.getMinutes();
var ampm = hours >= 12 ? 'PM' : 'AM';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0'+minutes : minutes;
var strTime = hours + ':' + minutes + ' ' + ampm;
var date = arr.join("/");
return date + " " +strTime;
}
let date = '2019-10-21 1:41:00';
console.log(formatDate(date));
TA贡献2041条经验 获得超4个赞
您可以使用 javascript 日期对象来获取所需的格式。用这个,
function formatDate(date) {
const arr = [date.getMonth() + 1, date.getDate(), date.getFullYear()]
var hours = date.getHours();
var minutes = date.getMinutes();
var ampm = hours >= 12 ? 'PM' : 'AM';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0'+minutes : minutes;
var strTime = hours + ':' + minutes + ' ' + ampm;
var date = arr.join("/");
return date + " " +strTime;
}
console.log(formatDate(new Date));
添加回答
举报