Javascript 单独的日期字符串和时间字符串组合成解析的日期&;时间戳
我一直在研究我是否有一个单独的字符串作为日期Javascript 单独的日期字符串和时间字符串组合成解析的日期&;时间戳,javascript,typescript,timestamp,ionic3,localnotification,Javascript,Typescript,Timestamp,Ionic3,Localnotification,我一直在研究我是否有一个单独的字符串作为日期 日期=2018年9月18日 时间是 时间=下午1:50 如果我想创建上述两个变量的时间戳 我应该有这个吗? 手头的问题是,我正在从API端点接收这些变量,我需要有准确的时间戳,以便在准确的时间和日期将它们用作本地提醒通知 这是我到目前为止所做的尝试 createTheLocalAlert(appointments) { // Schedule delayed notification appointments.descriptio
日期=2018年9月18日
时间是
时间=下午1:50
如果我想创建上述两个变量的时间戳
我应该有这个吗?
手头的问题是,我正在从API端点接收这些变量,我需要有准确的时间戳,以便在准确的时间和日期将它们用作本地提醒通知
这是我到目前为止所做的尝试
createTheLocalAlert(appointments) {
// Schedule delayed notification
appointments.description.forEach(appointment => {
let notificationText =
`you have meeting with ${appointment.name} today at ${appointment.meeting_start}`;
let theDate = appointment.appointment_date.split("-");
let newDate = theDate[1] + "/" + theDate[0] + "/" + theDate[2];
let DateWithTime = newDate + appointment.meeting_start;
// alert(new Date(newDate).getTime()); //will alert 1330210800000
// console.warn("theTime_is===>" + new Date(DateWithTime));
this.localNotifications.schedule({
text: notificationText,
trigger: {at: new Date(new Date(DateWithTime).getTime() - 7200)}, // 2 hours before meetup
led: 'FF0000',
vibrate: true,
icon: 'assets/imgs/logo.jpg',
sound: null
});
});
}
我能把日期转换成邮票,但我无法计算
找出一种将时间添加到日期并解析出确切时间的方法
在那个日期和时间上盖章
**
我们将非常感谢您提供的任何帮助。请尝试以下代码
formatDateWithTime(date,time){
if(date && time){
let splitDate = date.split('-');
let splitTime = time.split(':');
let formattedDate:any;
return formattedDate = new Date(splitDate[ 2 ], splitDate[ 1 ] - 1, splitDate[ 0 ],splitTime[ 0 ], splitTime[ 1 ],
}else{
return 0
}
}
以下是支持所有数据的日期构造函数:
new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]])
这里棘手的部分是Date
constructor不验证值
- 如果
,则只需添加hour=25
。需要进行明确的验证:1天和1小时
- [0,23]中的小时,[0,59]中的分钟,[0,11]中的monthIndex(JS使用0-11表示月份)
函数combineDateAndTime(dateStr,timeStr){
让[dt,month,year]=dateStr.split(“-”).map(t=>parseInt(t));//模式:dd-mm-yyyy
让[suffix]=timeStr.match(/AM | PM/);/AM/PM
让[hour,min]=timeStr.match(/\d+/g).map(t=>parseInt(t));//hh:mm
如果(第12个月)返回空值;
如果(23小时)返回空值;
if(min 59)返回null;
月份-=1;//monthIndex从0开始
如果(后缀==“AM”&&hour==12)hour=0;
如果(后缀==“PM”&&hour!==12)小时+=12;
返回新日期(年、月、日、时、分);
}
console.log(combineDateAndTime(“2018年9月18日”,“上午12:50”).toString();
console.log(combineDateAndTime(“2018年9月18日”,“下午12:50”).toString()代码>