R
R
robotwerter2015-04-22 18:52:48
JavaScript
robotwerter, 2015-04-22 18:52:48

How not to count weekends?

That is, there is a date, let's say 05/17/2015 15:15, I need to calculate how many hours have passed since that time, but not including weekends (you need to count only from Monday to Friday + do not include days that are marked as weekends (in a separate variable).

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ilya Shatokhin, 2015-04-22
@robotwerter

This script rounds up, but this can be quickly corrected.

var now = Date.now(); // от сейчас
var start = new Date("2015-04-21 20:18").valueOf(); // скармливаем дату в нормальном формате, либо пишите свой парсер
var hours = 0;

while (now > start) {
  if (new Date(start).getDay() < 6) // если это не выходной, здесь же можно проверять на праздничные дни
    ++hours;
  start += 1000*60*60;
}

alert(hours + "часов");

If the moment library is included
var now = moment();
var start = moment("2015-04-21 20:18", "YYYY-MM-DD HH:mm");
var hours = 0;

while (now.isAfter(start)) {
  if (start.isoWeekday() < 6)
    ++hours;
  start.add(1, 'hour');
}

alert(hours + "часов");

S
Sergey Andreev, 2015-04-22
@DragorWW

There is an API , you can use it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question