简体   繁体   English

显示12小时格式的预计到达时间

[英]showing ETA in 12 hours format react native

does any one know this could be achieved in react native, I am working on an location based app that requires showing ETA to the customer. 有没有人知道这可以通过react native实现,我正在开发一个基于位置的应用程序,需要向客户展示ETA。 However, I do not want to show just 25mins away, I would like to implement 'arriving 'Arriving Today, 9:45 AM. 但是,我不想只显示25分钟,我想实施上午9:45到达今天的“到达”。 which implies the current time is 9:20 and the ETA is added to the current time. 这表示当前时间为9:20,并将ETA添加到当前时间。 something like the image below 如下图所示

在此处输入图片说明

You can achieve it using the below function: 您可以使用以下功能来实现:

  getETA(minutesLeftInArrival) {
    let arrivalTimeLeft = minutesLeftInArrival * 60 * 1000; // convert to unix timestamp
    let currentTime = Date.now(); //TIME RIGHT NOW
    let timeToArrive = currentTime + arrivalTimeLeft; // TIME OF ARRIVAL
    let time = new Date(timeToArrive).toTimeString(); // TIME in 24 hour format
    time = time.split(" ")[0];  // Remove GMT+...... from time
    time = time.replace(/:\d\d([ ap]|$)/,'$1'); // remove seconds from time
    let H = +time.substr(0, 2);
    let hour = H % 12 || 12; 
    let ampm = (H < 12 || H === 24) ? "AM" : "PM"; // Return AM or PM depedning of 24 Hour time
    time = hour + time.substr(2, 3) + " " + ampm;


    return time; // RETURNS 12 HOUR TIME
  }

https://snack.expo.io/@ammarahmed/getetatime https://snack.expo.io/@ammarahmed/getetatime

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM