Hi everybody, for my app I have deals which are active between two hour intervals, for example an active deal between 16:00 and 00:00 (24 hour format), now given the current time with moments and two variables who represent star and end hours of the deal there can be two possibilities:
1) current time is between two time intervals, in that case I want to show a text like this:
Deal ends in 1 hour and 23 minutes
2) current time is already past said intervals or it’s before start time, in that case I want to show a text like this:
Deal starts in 20 hours 39 minutes
What I currently have is this function, which sometimes gives me a negative set of numbers, I have been stuck in this for a while so I hope I get some help.
“` const getTimeLeftOrTimeToStart = (startDate = ‘16.00’, endDate = ’00:00′) => {
var currentTimeIsBetweenDealHours; var currentTimeIsPartOrBeforeDealHours var now = moment().utc(); var end = moment(startDate, ‘HH:mm’).utc(); var end = moment(endDate, ‘HH:mm’).utc(); var duration = moment.duration(end.diff(now)); //Get Days and subtract from duration var days = duration.asDays(); duration.subtract(moment.duration(days,’days’)); //Get hours and subtract from duration var hours = duration.hours(); duration.subtract(moment.duration(hours,’hours’)); //Get Minutes and subtract from duration var minutes = duration.minutes(); duration.subtract(moment.duration(minutes,’minutes’)); //Get seconds var seconds = duration.seconds(); let remaining = hours+’hours y ‘+minutes+’ minutes’; //This prints -22hours and -41 minutes //Here I would also check if current time is between deal hours, past or before. };
“`
submitted by /u/Gabotron_ES
[link] [comments]