2013-06-13 6 views
7

मैं जिसके बाद मेरी जरूरतों के लिए ठीक काम करता है:दिनांक/समय घंटे गिनती से सप्ताहांत को शामिल न करें

function funLoad(str1,str3,str4) 
{ 

    var dym1 = str1.split("/"); 

    var d=new Date(); 
    var dym2 = d.getMonth() + 1 + "-" + d.getDate() + "-" + d.getFullYear() + " " + d.getHours() + ":" + d.getMinutes() + ":00"; 
    //var dym2 = "6 10 2013 09:00:00"; 

    var start = Date.parse(dym1[1] + "-" + dym1[0] + "-" + dym1[2] + " " + str3 + ":" + str4 + ":00"); 
    var end = Date.parse(dym2); 

    return (start-end)/(1000*60*60); 

} 

$("#btn1").click(function(event){ 
    alert(funLoad($("#txt1").val(),$("#ddlHourTime").val(),$("#ddlMinuteTime").val())); 
}); 

यहाँ एक jsfiddle है: http://jsfiddle.net/QTVWd/8/

मुझे खुशी है कि IE8 में यह शायद ही होगा काम , लेकिन मैं एक इंट्रानेट वातावरण में विकास कर रहा हूं जहां हर कोई केवल आईई 8 का उपयोग करता है, इसलिए यह कोई मुद्दा नहीं है। इसे अनदेखा करते हुए, इस स्क्रिप्ट को केवल चयनित तिथि और समय मिल जाता है, इसे वर्तमान दिनांक/समय से तुलना करता है, और घंटे देता है। अब तक सब अच्छा है।

समस्या यह है कि यह सप्ताहांत की गणना करता है। मैं केवल सप्ताह के दिनों में घंटों की गिनती कैसे करूं और सप्ताहांत को छोड़ दें यदि शुरुआत और समाप्ति तिथि सप्ताहांत शामिल है? तो उदाहरण के लिए, यदि प्रारंभ तिथि शुक्रवार को है, और समाप्ति तिथि सोमवार को है, तो इसे सप्ताहांत पर 48 घंटे को छोड़कर केवल शुक्रवार और सोमवार को घंटों की गणना करनी चाहिए।

+0

कृपया ध्यान रखें कि नहीं हर दिन 24 घंटे लंबा है हो सकता है, दिन के उजाले संभालने बचत समय प्रयुक्त समय क्षेत्र में प्रभावी होता है (चूंकि हम यहां विशेष रूप से सप्ताहांत के दिनों के बारे में बात कर रहे हैं, एक साल में दो शनिवार हैं जो समस्याग्रस्त हैं)। जहां तक ​​मैं एक त्वरित रूप से देख सकता हूं, अब तक के किसी भी उत्तर का जवाब नहीं है। – CBroe

उत्तर

0

यह फ़ंक्शन मैंने जेएसफ़िड में काम किया जो आपको ढूंढना चाहिए। यह सुनिश्चित करने के लिए कि आप सब कुछ ठीक से गणना कर रहे हैं, आप इसे अपने पैसों के माध्यम से चलाने के लिए चाहते हैं।

updated jsfiddle

समारोह funLoad (str1, str3, str4) {

var dym1 = str1.split("/"); 

// get the given(start) epoch value (using midnight for time - will adjust for actual time further below) 
var start_date = new Date(dym1[2], (dym1[1]-1), dym1[0], 0, 0, 0, 0);  
var start_epoch = start_date.getTime(); 

// get todays(end) epoch value (using midnight for time - will adjust for actual time further below)  
var end_d = new Date(); 
var end_epoch = new Date(end_d.getFullYear(), end_d.getMonth(), end_d.getDate(), 0, 0, 0, 0).getTime(); 
var end_date_is_weekend = (end_d.getDay()==0 || end_d.getDay()==6)?1:0;  

// get value for 1 day in milliseconds 
var one_day = 1000*60*60*24; 

// how many days between given date and todays date 
var total_days = Math.ceil((end_epoch - start_epoch)/one_day)-1; 

// figure out how many of those total days are weekend days 
var date_to_check = start_epoch + one_day; 
var total_weekend_days = 0; 
var next_day_is_weekend = 0; 
for(var x=0; x<total_days; x++){ 
    d=new Date(date_to_check); 
    if(d.getDay()==0 || d.getDay()==6){ 
     total_weekend_days++; 
     if(x==0){next_day_is_weekend=1;} 
    } 
    date_to_check += one_day 
} 

// determine number of week days 
var total_week_days = total_days - total_weekend_days; 

// get the total hours from the total week days 
var total_hours = total_week_days*24; 

// still need to add the remaining hours from the given(start) date and the number of hours that have already past in the end(todays) date 

// add in the remaining hours left in the start date 
var hours_remaining_in_start_date = 0; 

// rounding up to next nearest start date hour 
if(str4 > 30){ 
    if(str3 == 23 && next_day_is_weekend==0){ 
     // rounding up puts the start date into the start of the next day so subtract 
     // one day from the total, UNLESS the next day is a weekend. 
     total_week_days -= 1; 

     hours_remaining_in_start_date = -24; 

    }else{ 
     hours_remaining_in_start_date = 24 - (str3+1);      
    } 
}else{ 
    hours_remaining_in_start_date = 24 - str3;   
}  

total_hours += hours_remaining_in_start_date; 

    // add in the hours from the end date (today date), UNLESS today is a weekend 
var hours_past_in_end_date = 0; 
var end_hour = end_d.getHours(); 
if(end_date_is_weekend==0){ 
    // rounding up to next nearest end date hour 
    if(end_d.getMinutes() > 30){ 
     if(end_hour == 23){ 
      // rounding up makes it a full day 
      hours_past_in_end_date = 24; 
     }else{ 
      // rounding up just adds an hour 
      hours_past_in_end_date = end_hour+1; 
     } 
    }else{ 
     hours_past_in_end_date = end_hour;  
    } 
} 
total_hours += hours_past_in_end_date; 



$('#result').html('start epoch:'+start_epoch+', end epoch:'+end_epoch+', total days'+total_days+', total weekends'+total_weekend_days+', total week days:'+total_week_days+', hours_remaining_in_start_date:'+hours_remaining_in_start_date+', hours_past_in_end_date:'+hours_past_in_end_date+', total hours:'+total_hours); 

}

+0

पहला jsfiddle लिंक कुछ भयानक के लिए चला गया। सही स्थान पर इंगित करने के लिए बस इसे अपडेट किया गया। उसके लिए माफ़ करना। – Drew

0

यह काम करेगा:

var calcWorkHours = function calcWorkHours(start, end) { 
     var total = (end - start)/(1000 * 60 * 60 * 24), // compute difference in days 
      totalDays = parseInt(total, 10),    // trim fractional portion 
      remainder = (total - totalDays) * 24,   // save fractional portion in hours 
      startingDay = start.getDay(),     // starting weekday 
      endingDay = end.getDay(),      // ending weekday 
      possibleWorkingDays = (totalDays * 5), 
      actualOccurringWeekends = (startingDay - endingDay) * 2, 
      workDays = (possibleWorkingDays - actualOccurringWeekends)/7; 
     if (endingDay === 6) { 
      workDays -= 1; // ends on a weekend, subtract a day 
     } 
     if (startingDay === 0) { 
      workDays -= 1; // starts on a weekend, subtract a day 
     } 
     return (workDays * 24) + remainder; // convert to hours and add back fractional portion 
    }, 
    parseInputs = function parseInputs(date, hour, minute) { 
     var dmy = date.split("/"), 
      now = new Date(), 
      thisMinute = now.getMonth() + 1 + "-" + now.getDate() + "-" + now.getFullYear() + " " + now.getHours() + ":" + now.getMinutes() + ":00", 
      start = Date.parse(dmy[1] + "-" + dmy[0] + "-" + dmy[2] + " " + hour + ":" + minute + ":00"), 
      end = Date.parse(thisMinute); 
     start = new Date(start); 
     end = new Date(end); 
     return calcWorkHours(start, end); 
    }; 

कार्रवाई यहाँ में यह देखें: http://jsfiddle.net/QTVWd/9/

अद्यतन

तुम सच घंटे एक ऋणात्मक संख्या के रूप में व्यक्त की जरूरत है, बस मापदंडों calcWorkHours में स्विच:

return calcWorkHours(end, start); 
संबंधित मुद्दे