# Calculate UK Bank Holidays

Page 1 of 1

## 8 Replies - 903 Views - Last Post: 27 June 2013 - 10:35 AM

### #1 condmach

Reputation: 0
• Posts: 3
• Joined: 24-June 13

# Calculate UK Bank Holidays

Posted 24 June 2013 - 04:05 AM

Could use a little assistance adding a lookup (or hardcode if easier) to find UK bank holidays then excluding them from the following calculation...

```Function NewTimeOut(Counter)
{
Var CalculatedDays=0;
Var day=today().getDay();
While (Counter > 0) {
CalculatedDays++;
day= (day%7)+1;
if (day!=6 && day!=7){
Counter--;
}
}
Return CalculatedDays;
};

```

Thanks.

Is This A Good Question/Topic? 0

## Replies To: Calculate UK Bank Holidays

### #2 Atli

• D.I.C Lover

Reputation: 3637
• Posts: 5,765
• Joined: 08-June 10

## Re: Calculate UK Bank Holidays

Posted 24 June 2013 - 04:36 AM

What language is that supposed to be? Definitely doesn't look like Java. Kind of looks like Javascript (which is a completely different thing!) except that Javascript syntax doesn't allow mixed casing for keywords like function and var; which means this snippet would never run if it's supposed to be Javascript.

### #3 Dogstopper

Reputation: 2860
• Posts: 10,964
• Joined: 15-July 08

## Re: Calculate UK Bank Holidays

Posted 24 June 2013 - 05:38 AM

Moving to Javascript. You might consider fixing all your capitalization. Function != function != funCtion.

### #4 condmach

Reputation: 0
• Posts: 3
• Joined: 24-June 13

## Re: Calculate UK Bank Holidays

Posted 25 June 2013 - 01:15 AM

My apologies, seems Word was capitalising the code.

Here it is again:

```
function NewTimeOut(Counter){
var CalculatedDays=0;
var day=today().getDay();
while (Counter > 0) {
CalculatedDays++;
day= (day %7) +1;
if (day !=6 && day !=7){
Counter--;
}
}
return CalculatedDays;
};

vWTimeOut=NewTimeOut(TimeOut1);

```

This is currently working, but need to add bank holidays into the equation.

Thanks.

### #5 Dormilich

• 痛覚残留

Reputation: 3402
• Posts: 9,623
• Joined: 08-June 10

## Re: Calculate UK Bank Holidays

Posted 25 June 2013 - 01:43 AM

my first question is, what is this function supposed to calculate? if it’s the number of work days between two dates, I’d rather calculate the amount of weekends (via week count) and holidays in the given time span. for holidays I’d use an array of the holidays and filter off any dates that are outside the date range.

### #6 JMRKER

Reputation: 125
• Posts: 818
• Joined: 25-October 08

## Re: Calculate UK Bank Holidays

Posted 25 June 2013 - 07:01 AM

condmach, on 25 June 2013 - 02:15 AM, said:

My apologies, seems Word was capitalising the code.

Here it is again:

```
function NewTimeOut(Counter){
var CalculatedDays=0;
var day=today().getDay();
while (Counter > 0) {
CalculatedDays++;
day= (day %7) +1;
if (day !=6 && day !=7){
Counter--;
}
}
return CalculatedDays;
};

vWTimeOut=NewTimeOut(TimeOut1);

```

This is currently working, but need to add bank holidays into the equation.

Thanks.

Difficult to see how you are going to get UK bank holidays from that code.
For example:
1. What are the actions of the function today()?
2. What will the values of 'TimeOut' be passed to the function NewTimeOut()?
What do you expect to happen if the value is accidently negative?
3. Looks to me that CalculatedDays will be number of M,T,W,T,F days (ignores Sat-Sun).
How does the number of weekdays relate to UK holidays?

What does the rest of your code look like (including HTML and CSS)?

### #7 condmach

Reputation: 0
• Posts: 3
• Joined: 24-June 13

## Re: Calculate UK Bank Holidays

Posted 27 June 2013 - 12:29 AM

There is no more code.

This simply takes a timeout number eg 3 days and depending on the current day of the week adds on days to cater for week ends... and returns NewTimeOut

Eg If Friday – Today + 3 Days Timeout value would be Monday, but this is only 1 working day, this code adds 2 days (1 weekend) to make it Wednesday. i.e. NewTimeout 5 Days

Eg If Friday – Today + 7 Days Timeout value would be next Friday, this code adds 4 days (2 weekends) to make it following Tuesday. i.e. NewTimeout 11 Days

With a Bank Holiday on the Monday
Eg If Friday – Today + 3 Days Timeout value would be Monday, we need code which adds 2 days (1 weekend) plus an extra day for the Holiday. i.e. NewTimeout 6 Days

Thanks.

### #8 JMRKER

Reputation: 125
• Posts: 818
• Joined: 25-October 08

## Re: Calculate UK Bank Holidays

Posted 27 June 2013 - 07:51 AM

So how is the script to know if 'Today' (BTW: an unknown and not specified value)
is a normal Monday or a UK bank holiday Monday?

And is the 'Today' always positive?
Is 'Friday' always a future day (not last Friday) and it is always represented as a value of '5'?

It may be all the code you have, but you need to specify more information to solve it.
Sort of like solving a polynomial formula with only one known.
/>/>

Just a guess as to what you want to do with some HTML code to change input for calculations.
```<!DOCTYPE HTML>
<html>
<title> Untitled </title>
<meta charset="utf-8">

<body>
<input type="text" id="futureDays" value="" size="3">

<!-- following is a poorly named function BTW -->
Weekday count </button>

<script type="text/javascript">
// From: http://www.dreamincode.net/forums/topic/323511-calculate-uk-bank-holidays/page__gopid__1867754&#entry1867754

function NewTimeOut(Counter) {
var CalculatedDays=0;
var today = new Date().getDay();  // possible values 0-6 (Sun-Sat)
while (Counter > 0) {

// following logic STILL DOES NOT KNOW if Monday is a UK holiday!!!
if ((today > 0) && (today < 6)) { CalculatedDays++; }

today = ((today+1) % 7);
Counter--;
}
return CalculatedDays;
};

</script>

</body>
</html>

```

This post has been edited by JMRKER: 27 June 2013 - 08:11 AM

• Cheese and Sprinkles

Reputation: 409
• Posts: 1,729
• Joined: 30-April 10

## Re: Calculate UK Bank Holidays

Posted 27 June 2013 - 10:35 AM

condmach, on 25 June 2013 - 08:15 AM, said:

My apologies, seems Word was capitalising the code.

Why are you using Word? Using Word for editing is bad practice. It will make more issues than help. Just throwing that out there.