angular - 在 TypeScript 中将日期和时间字符串合并为一个日期
问题描述
我有一个DatePicker和一个Select手动添加时间。
DatePicker 返回:2019 年 2 月 20 日星期三 00:00:00 GMT-0400(大西洋标准时间)
选择返回:上午 7:00
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);
如何将它们组合成一个日期以将其保存在数据库中?
解决方案
尝试以下方法 -
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]);
推荐阅读
- c++ - 检查 N 及其双精度是否存在
- git - 如何使詹金斯构建状态徽章动态显示其文件所在的分支
- python - 为什么我不能在 for 循环工作时更改字典?
- express - 如何等到所有异步调用完成
- swift - Xcode 12 无法构建可可豆荚 - 生成伞头
- amazon-s3 - Dotnet 核心代码从 gitlab CI/CD 以 Zip 形式发布推送到 s3
- azure - 仅使用 Azure 数据工厂复制特定日期的最新文件夹/文件
- r - 使用具有重复类别的 ggplot 条形图
- c++ - 当我在 C++ 中使用 cin 时输入 EOF 会发生什么?
- python - 根据类别对部分训练集应用不同的数据增强