javascript - 单独的日期字符串和时间字符串组合成解析日期时间戳

标签 javascript typescript timestamp ionic3 localnotification

我一直在研究我是否在单独的字符串中有日期

日期 = 18-9-2018

时间为

time= 01:50 PM

and if I want to create a time stamp of the above two variables how i am supposed to have that?

手头的问题是我从 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
        });
    });
}

I am able to convert the date into stamp but I am un able to figure out a way of adding the time into the date and parse out exact time Stamp on that date and time .

**

我们将不胜感激任何形式的帮助。

最佳答案

试试下面的代码。

    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
             }
       }

关于javascript - 单独的日期字符串和时间字符串组合成解析日期时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53020808/

相关文章:

swift - 拖动 UITableView 或所有 TableViewCells 显示一个标签

android - Flutter 有没有办法制作一个动态时间戳,打印出 10 分钟前、1 小时前或 2 个月前的内容

javascript - 算法,Automagic Ajax

javascript - Google Apps 脚本将已完成的项目移动到另一个电子表格

typescript - 使用带有 typescript 的身份对象代理和开 Jest 时返回未定义

Typescript 使用鉴别器的联合字符串类型创建 ADT(可鉴别联合类型)的实例

JavaScript 和 PHP : How to encode a normal string in a encoded short string?

javascript - 平滑的 anchor /ID 滚动不适用于标题的偏移量

angular - Angular 动态切换模板html,scss

java - 日期操作问题