最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Find the time closest to current time with 5 date objects javascript - Stack Overflow

programmeradmin1浏览0评论

I have the current time as currentTime.

And I have 5 date objects:

a = 2:20 am b = 2:40 am c = 3:00 am d = 4:00 pm e = 6:00 pm

The time is 3:30 pm

Now, keeping in mind that a to e have successfully been turned into Date() objects.

How would I cycle through those 5, given the current time, to determine which time es closest to the current time, but after it. So for instance I want to find in this case that d is the next time ing.

I'm just not sure of the way to do that.

I have the current time as currentTime.

And I have 5 date objects:

a = 2:20 am b = 2:40 am c = 3:00 am d = 4:00 pm e = 6:00 pm

The time is 3:30 pm

Now, keeping in mind that a to e have successfully been turned into Date() objects.

How would I cycle through those 5, given the current time, to determine which time es closest to the current time, but after it. So for instance I want to find in this case that d is the next time ing.

I'm just not sure of the way to do that.

Share Improve this question asked Jul 15, 2013 at 22:01 David GDavid G 6,8714 gold badges32 silver badges51 bronze badges 4
  • Use a for loop to iterate through them. Subtract the date from the target date and get the absolute value. Keep track of the closest - if the current iteration's date is closer, store that, otherwise move on. – Ian Commented Jul 15, 2013 at 22:11
  • A Date object only represents a full date-and-time. Not just a time. Are you saying that the times exist on every day? So if the time was 7:00 pm, should I get 2:20 am on the next day? Or return null? – Matt Johnson-Pint Commented Jul 15, 2013 at 22:24
  • 1 @Ian - almost right, but not the absolute value. OP wants the nearest time greater than the current time. So discard negative values that result from the subtraction and only look at the positive ones. – Michael Geary Commented Jul 15, 2013 at 23:22
  • @MichaelGeary Oops, I didn't see , but after it. :) You're right then! – Ian Commented Jul 15, 2013 at 23:25
Add a ment  | 

2 Answers 2

Reset to default 9
// Given an array of Date objects and a start date,
// return the entry from the array nearest to the
// start date but greater than it.
// Return undefined if no such date is found.
function nextDate( startDate, dates ) {
    var startTime = +startDate;
    var nearestDate, nearestDiff = Infinity;
    for( var i = 0, n = dates.length;  i < n;  ++i ) {
        var diff = +dates[i] - startTime;
        if( diff > 0  &&  diff < nearestDiff ) {
            nearestDiff = diff;
            nearestDate = dates[i];
        }
    }
    return nearestDate;
}

var testDates = [
    new Date( 2013, 6, 15, 16, 30 ),
    new Date( 2013, 6, 15, 16, 45 ),
    new Date( 2013, 6, 15, 16, 15 )
];

console.log( nextDate( new Date( 2013, 6, 15, 16, 20 ), testDates ) );
console.log( nextDate( new Date( 2013, 6, 15, 16, 35 ), testDates ) );

    console.log( nextDate( new Date( 2013, 6, 15, 16, 50 ), testDates ) );

There might be a neater/faster way to do it, but I would make a second array with the differences between the value and the currentTime for a to e and then use this second array to sort the first array (take the diff of the diff of both values).

发布评论

评论列表(0)

  1. 暂无评论