0

谁能指出我正确的方向?我试图找出在特定范围内有多少天。如果我有一个预订日期范围,例如“2013 年 1 月 18 日 21:00”和“2013 年 2 月 4 日 15:00”,并且想知道 2 月 1 日到 4 月 30 日之间有多少天。经过大量的摆弄,我可能会拥有它..?

    /* The test date range */
    var startTime = "18 Jan 2013 21:00";
    var endTime = "4 Feb 2013 15:00";

    /* Set up the range to test against*/
    var rangeStart = Date.parse ("1 Feb 2013 00:00");
    var rangeEnd = Date.parse ("30 Apr 2013 00:00");

    var start = Date.parse(startTime); 
    var end = Date.parse(endTime);

    var numOfHrs =  Math.ceil((end-start) / (1000*60*60));
    var numOfDays = Math.ceil(numOfHrs/24);
    var rangeCrossed = 0;   


    if (end > rangeStart){ /* A valid crossover period may exist */

        /* Start time before or equal range start and end less than or equal the range end */
        if ((start<=rangeStart) && (end <= rangeEnd)) { rangeCrossed = end-rangeStart } 
        /*Start greater than or equal to the range start and less than the range end */
        else if ((start>=rangeStart) && (start<rangeEnd)) 
                {
                /* start and end both within range */
                if (end<=rangeEnd) { rangeCrossed = end-start } 
                /* start after range start and end after range end */
                else {rangeCrossed = (rangeEnd-start) + (end-rangeEnd)}; 
                } 
        /*full range crossed */     
        else if (start<=rangeStart && end >=rangeEnd) { rangeCrossed = rangeEnd-rangeStart};    
        }

    alert((rangeCrossed/(1000*60*60)/24))
4

0 回答 0