JS calculates the length of leave (excluding week 6th, holidays not cleared)

Source: Internet
Author: User
Tags date1

JS calculates the length of leave (excluding week 6th, holidays not cleared)2017-12-26 also Hao

<! DOCTYPE html>

<title></title>

<meta http-equiv= "Content-type" content= "Text/html;charset=utf-8" >

<link href= "./bootstrap/css/bootstrap.min.css" rel= "stylesheet" media= "screen" >

<link href= "./css/bootstrap-datetimepicker.min.css" rel= "stylesheet" media= "screen" >

<body>

<input type= "text" id= "Days" value= ""/>

<label class= "Control-label" > Days </label>

<br><br><br><br><br>

<div class= "Form-group" >

<label for= "group_name" class= "Col-sm-2 Control-label" > Start time </label>

<div class= "Input-group date form_datetime col-sm-6"

data-date-format= "Yyyy-mm-dd Hh:ii:ss"

Data-link-field= "DTP_INPUT1"

style= "padding:0px 15px;" >

<input class= "Form-control" size= "+" type= "text" id= "Time_start" >

<span class= "Input-group-addon" ><span class= "Glyphicon Glyphicon-calendar" ></span></span>

</div>

<br><br><br><br>

<div class= "Form-group" >

<label for= "group_name" class= "col-sm-2 Control-label" > End time </label>

<div class= "Input-group date form_datetime col-sm-6"

data-date-format= "Yyyy-mm-dd Hh:ii:ss"

Data-link-field= "Dtp_input2"

style= "padding:0px 15px;" >

<input class= "Form-control" size= "+" type= "text" id= "Time_end" >

<span class= "Input-group-addon" >

<span class= "Glyphicon Glyphicon-calendar" ></span></span>

</div>

<input type= "hidden" id= "Dtp_input2"/><br/>

</div>

<br><br><br>

<button id= "Submit1" > Save </button>

<script type= "Text/javascript" src= "Jquery/jquery-1.8.3.min.js" charset= "UTF-8" ></script>

<script type= "Text/javascript" src= "Bootstrap/js/bootstrap.min.js" ></script>

<script type= "Text/javascript" src= "Js/bootstrap-datetimepicker.js" charset= "UTF-8" ></script>

<script type= "Text/javascript" src= "Js/locales/bootstrap-datetimepicker.fr.js" charset= "UTF-8" ></script >

<script type= "Text/javascript" src= "H.js" charset= "UTF-8" ></script>

<script type= "Text/javascript" charset= "Utf-8" >

$ (document). Ready (function () {

$ ('. Form_datetime '). DateTimePicker ({

Language: ' ZH-CN ',

Weekstart:1,

Todaybtn:1,

Autoclose:1,

Todayhighlight:1,

Startview:2,

forceparse:0,

Showmeridian:1

});

})

$ (' #submit1 '). Click (function () {

Leavetime ("#time_start", "#time_end", Caleendfun);

})

function Caleendfun (leavetimeobje) {

$ (' #days '). Val (Leavetimeobje)

}

function Leavetime (timestart,timeend,enfund) {

var starttime = $ (Timestart). Val ();

var endtime = $ (timeend). Val ();

var datetime1 = new Date (starttime), datetime2 = new Date (endtime);

var date1 = datetime1.sethours (0,0,0); Start date, MS indicates

var date2 = datetime2.sethours (0,0,0); The end date, in milliseconds, indicates

var starttimeleft=starttime.substring (0,10);//Get Time to Month

var endtimeleft=endtime.substring (0,10);

var begindate = new Date (Starttimeleft.replace (/-/g, "/"));

var endDate = new Date (Endtimeleft.replace (/-/g, "/"));

var daynum = parseint (Math.Abs (begindate-enddate)/1000/60/60/24);

var startweek = Begindate.getday ();

var starhour=number (starttime.substring (11,13));

var endhour=number (endtime.substring (11,13));

var travelhours = 0; Save the number of leave hours

var traveltimehours = 0;

var inow = 0;

var dayinweek= new Date (starttime). GetDay ();/////start date is the day of the week

var dayendweek = new Date (endtime). GetDay ();//The end date is the day of the week

9:00--18:00 hours according to working hours

Transform to work time zone

if (Date1 ==date2) {//Start end time All in one day

if (starhour<=12) {

if (9<= endhour && endhour<13) {

Travelhours = 4;

}else{

Travelhours = 8;

}

}else if (starhour>=13) {

Travelhours = 4;

}

}else{

If the start time is less than 8 points, the calculation of work starts from 8 o'clock

if (starhour<9) {

Starhour = 9;

}

If the start time is greater than 17 points,

if (starhour>18) {

Starhour = 18;

}

if (endhour<9) {

Endhour = 9;

}

1.3

if (endhour>18) {

Endhour = 18;

}

if (0< dayinweek && dayinweek <6 && dayendweek = 0 | | 0< dayinweek && dayinweek <6 && Dayendweek = 6) {

Traveltimehours + = (Weekendbetween (daynum)-1) *8;

}else if (0< dayendweek && dayendweek <6 && dayinweek = 0 | | 0< dayendweek && dayendweek <6 && Dayinweek = 6) {

Traveltimehours + = (Weekendbetween (daynum)-1) *8;

}else if (dayinweek!=6 && dayinweek!=0 && dayendweek!=6 && dayendweek!=0) {

Traveltimehours + = (Weekendbetween (daynum)-2) *8;

}

if (Dayinweek!=6 && dayinweek!=0) {

if (starhour>=9 && starhour<=12) {

traveltimehours+=8;

}

if (starhour>=13 && starhour<=18) {

traveltimehours+=4;

}

}

if (Dayendweek!=6 && dayendweek!=0) {

if (endhour>=9 && endhour<=12) {

traveltimehours+=4;

}

if (endhour>=13 && endhour<=18) {

traveltimehours+=8;

}

}

}

var timedays = TRAVELHOURS/8;

var travelTime = TRAVELTIMEHOURS/8;

var traveltimeday = timedays + travelTime;

if (enfund) {

Enfund (Traveltimeday)

}

Removal week 6th;

function Weekendbetween (daynum) {

var inow = 0;

Daynum =daynum +1;

for (var i=0; i<daynum; i++) {

var firstday = startweek% 7;

if (firstday==0) firstday=7;

if (FirstDay < 6) {

inow++;

}

if (FirstDay = = 7) {

FirstDay = 1;

}

Startweek + +;

}

return inow;

}

}

</script>

</body>

JS calculates the length of leave (excluding week 6th, holidays not cleared)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.