3

I have been researching about if I have date in a separate string as

date = 18-9-2018

and time as

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?

The problem in hand is that I am receiving those variables from an API end point and i need to have the exact time stamp so that i could use them as local reminder notifications on exact that time and date!

here is what i have tried so far

  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 .

**

Any kind of help will be highly appreciated.

Rizwan Atta
  • 3,222
  • 2
  • 19
  • 31

2 Answers2

0

Try the below code.

    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
             }
       }
Arj 1411
  • 1,395
  • 3
  • 14
  • 36
0

Here's the Date constructor which supports every data you have:

new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]])

Tricky part here is Date constructor does not validate the values.

  • If hour = 25, it simply adds 1 day and 1 hour. Explicit validation is required:
  • hour in [0,23], min in [0,59], monthIndex in [0,11] (JS uses 0-11 for month)

function combineDateAndTime(dateStr, timeStr) {
    let [dt,month,year] = dateStr.split("-").map(t => parseInt(t));  // pattern: dd-mm-yyyy
    let [suffix] = timeStr.match(/AM|PM/);                           // AM/PM
    let [hour,min] = timeStr.match(/\d+/g).map(t => parseInt(t));    // hh:mm 
    
    if (month <= 0 && month > 12) return null;
    if (hour <= 0 && hour > 23) return null;
    if (min <= 0 && min > 59) return null;
    
    month -= 1; // monthIndex starts from 0
    if (suffix === "AM" && hour === 12) hour = 0;
    if (suffix === "PM" && hour !== 12) hour += 12;

    return new Date(year, month, dt, hour, min);
}

console.log(combineDateAndTime("18-9-2018", "12:50 AM").toString());
console.log(combineDateAndTime("18-9-2018", "12:50 PM").toString());
Abhilash
  • 2,026
  • 17
  • 21