我有一个DatePicker和一个Select,并且手动添加了时间。
DatePicker返回:2019年2月20日星期三00:00:00 GMT-0400(大西洋标准时间)
选择返回:7:00 AM
const date = 'Wed Feb 20 2019 00:00:00 GMT-0400 (Atlantic Standard Time)';
const time = '7:00 AM';
console.log(new Date(date + time));
我收到错误:无效的日期
我也尝试过:
const date = 'Wed Feb 20 2019 00:00:00 GMT-0400 (Atlantic Standard Time)';
const time = '7:00 AM';
const yy = new Date(date).getFullYear();
const mm = new Date(date).getMonth() + 1;
const dd = new Date(date).getDate();
const completeDate = new Date(mm + dd + yy + time);
console.log(yy);
console.log(mm);
console.log(dd);
console.log(completeDate);
如何将它们合并为一个Date并将其保存在数据库中?
答案 0 :(得分:0)
尝试这样。对于子午线,将时间转换为24小时格式
const date = 'Wed Feb 20 2019 00:00:00 GMT-0400 (Atlantic Standard Time)';
d = new Date(date);
const time = '7:00';
d.setHours(time.split(':')[0]);
d.setMinutes(time.split(':')[1]);
答案 1 :(得分:0)
它既可以工作,也可以管理AM / PM
const date = 'Wed Feb 20 2019 00:00:00 GMT-0400 (Atlantic Standard Time)';
const time = '7:00 AM';
const yy = new Date(date).getFullYear();
const mm = new Date(date).getMonth() + 1;
const dd = new Date(date).getDate();
times=time.split(" ")[0].split(":");
if(time.split(" ")[1]=="PM")times[0]=Number(times[0])+12;
const completeDate = new Date(yy, mm, dd, times[0], times[1]);
答案 2 :(得分:0)
尝试以下方法-
const date = 'Wed Feb 20 2019 00:00:00 GMT-0400 (Atlantic Standard Time)';
const time = '7:00 AM';
const yy = new Date(date).getFullYear();
const mm = new Date(date).getMonth() + 1;
const dd = new Date(date).getDate();
var interMedDt = new Date(mm + '-' + dd + '-' + yy);
interMedDt.setHours((time.split(' ')[0]).split(':')[0]);
interMedDt.setMinutes((time.split(' ')[0]).split(':')[1]);
答案 3 :(得分:0)
我认为日期字符串的时间部分将始终为00:00:00
const date = 'Wed Feb 20 2019 00:00:00 GMT-0400 (Atlantic Standard Time)';
const time = '7:00 AM';
const t1: any = time.split(' ');
const t2: any = t1[0].split(':');
t2[0] = (t1[1] === 'PM' ? (1*t2[0] + 12) : t2[0]);
const time24 = (t2[0] < 10 ? '0' + t2[0] : t2[0]) + ':' + t2[1];
const completeDate = date.replace("00:00", time24.toString());
注意:
您不需要
const time24 = (t2[0] < 10 ? '0' + t2[0] : t2[0]) + ':' + t2[1] + ':00';
const completeDate = date.replace("00:00:00", time24.toString());
因为date.replace
将始终替换第一个匹配项。