Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
518 views
in Technique[技术] by (71.8m points)

javascript - 我如何从开始时间中减去结束时间而在Javascript计时器中没有负数分钟(How do i subtract End Time from Start Time without having negative minutes in Javascript timer)

I need to subtract end time from start time to create a count down timer, I also want to avoid getting negative minutes.

(我需要从开始时间中减去结束时间来创建倒数计时器,我也想避免得到负数分钟。)

how do I do this.

(我该怎么做呢。)

var timeStart = new Date().getHours();
var timeEnd = new Date("01/01/2007 " + valuestop).getHours();
var timeStartMin = new Date().getMinutes();
var timeEndMin = new Date("01/01/2007 " + valuestop).getMinutes();

var difference = timeEnd - timeStart;
var differenceMin = timeEndMin - timeStartMin;

the timer works fine, but VAR differenceMin is negative

(计时器工作正常,但VAR DifferenceMin为负)

  ask by Kunle translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I did not understand why you are getting differences between start and end because mostly for countdowns we use end-time and now difference.

(我不明白您为什么会在开始和结束之间产生差异,因为在倒数计时中,我们大多使用结束时间和现在的差异。)

But anyway I am sending what you want and if you want you can use this function to get a difference between end and now too.

(但是无论如何,我都会发送您想要的东西,如果您想要的话,您也可以使用此功能来获得现在和现在之间的差异。)

var getTimeDifference=function(from,to){
    var difMs = (from - to);
    if(difMs<=0){
        return 0 + " days, " + 0 + " hours, " + 0 + " mins";
    }else{
        var difDays = Math.floor(difMs / 86400000);
        var difHrs = Math.floor((difMs % 86400000) / 3600000);
        var difMins = Math.round(((difMs % 86400000) % 3600000) / 60000);
        return diffDays + " days, " + difHrs + " hours, " + diffMins + " mins";
    } 
}
var startTime= new Date('12-30-2019 20:00:00');
var endTime= new Date('11-1-2019 16:00:00');
getTimeDifference(endTime,startTime);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...