How to check is today a working day (mon-fri) - javascript

I'm searching to method how to check is today a working day monday-friday. I'm using moment.js. I'm getting today date as moment()

You can still use moment
const today = moment().format('dddd')
This will return the date string.
If you want the today week number than :
const today = moment().day();
which returns a number between 0 - 6
So day 6 and 0 are non working days.
I would wrap it in a function
function isWorkDay(date = moment()){
const day = moment(date).day())
return day && day < 6
}

Related

How to find if today day of the month is greater then a particular date on the month with date-fns

How can I find if todays date of the month is greater then 10 with date-fns
Basically I can get the first day of the month with startOfMonth
then add 10 days to that with addDays and then use ifAfter of isBefore.
The code could be looking like that:
greaterThan = 10;
today = new Date();
targetDate = addDays(startOfMonth(today), greaterThan);
isGreater = isAfter(today, targetDate);
Is there a shortcut withing date-fns to achieve in less code lines?
var greaterThan10 = new Date().getDate() > 10;
Cause the (numerical) day of today will be greater (or not) in any other month as well? Or am I missing something?

Momentjs - Get most recent Friday

I'm trying to get the start of (12:00am, or, 00:00am) of the most recent Friday. This has been working:
moment().isoWeekday(5).startOf('day').toDate()
But it only works Friday->Sunday, on Monday morning it will then refer to the upcoming Friday, in which case this would work:
moment().add('-1', 'week').day(5).startOf('day').toDate()
but I need it be dynamic and done in one line if possible, to where I don't to perform any checks on the current day.
Is there a way to always get the most recent Friday? Regardless of what the current day is.
Edit I'm also trying to get this to return the current day (friday) if executed on a Friday.
If you don't want to use a library, it's pretty straight forward
var date = new Date();
while ( date.getDay() !== 5 ) date.setDate(date.getDate() -1);
console.log(date)
With moment
var date = moment();
var friday = date.day(date.day() >= 5 ? 5 :-2);
and if millisecond accuracy doesn't matter, you could call moment() twice to make it one line (but I would much raher use a variable)
var friday = moment().day(moment().day() >= 5 ? 5 :-2);
Check this:
var date = moment().utc().isoWeekday(5);
if(moment().day() < 5) {
date = date.add(-1, 'week');
}
console.log('Recent friday starts:', date.startOf('day').toDate());
console.log('Recent friday ends:', date.endOf('day').toDate());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.1/moment.js"></script>

Adding time to a date in javascript [duplicate]

This question already has answers here:
Incrementing a date in JavaScript
(19 answers)
Closed 6 years ago.
I have this script;
showDiff();
function showDiff() {
var date1 = new Date("2016/03/14 00:00:00");
var date2 = new Date();
var diff = (date2 - date1);
var diff = Math.abs(diff);
var result;
if (diff > 432000000) {
result = 100 + "%";
} else {
result = (diff/4320000) + "%";
}
document.getElementById("showp").innerHTML = result;
document.getElementById("pb").style.width = result;
setTimeout(showDiff,1000);
}
Now I want to get exactly one week added to date1 when atleast one week has passed since that time. That date has to be saved so that one week later, another week can be added to date1. So basically every monday there has to be one week added to date1. How do I this?
The Date object has both a getDate() and setDate() function (date referring to the day of the month, no the full calendar date), so it's really as simple as getting a Date object and setting its date to +7 days from itself.
Example:
var weekFromNow = new Date();
weekFromNow = weekFromNow.setDate(weekFromNow.getDate()+7);
Just to clarify, the Date object contains a full calendar date and time, with its date property referring just to the day in the month (also different from its day property, which is the day of the week).

Subtract working days from a date using Javascript

I'd like to use a Javascript within my zapier.com-zap. Here is what I am trying to do for five consecutive days now:
I have a date (whatever custom format you need), need to subtract two working days from it and output it to DD-MM-YYYY using Javascript. Sounds really simple, but I don't get it to work.
I hope someone out there can help me with this! Thank you very much.
I forgot to mention an essential thing, sorry. If the result is a Sunday or Saturday I need the date of the last working day (Friday).
If you're willing to use external libraries, MomentJS is a really popular tool for parsing and modifying JavaScript dates and would make this really simple:
Example 1: Subtract 2 Days and Format
var date = new Date(),
formatted = moment(date).subtract(2, 'days').format('DD-MM-YYYY');
document.getElementById('date').innerHTML = date;
document.getElementById('example').innerHTML = formatted;
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.2/moment.min.js"></script>
This example takes today's date (<span id="date"></span>), subtracts 2 days from it,
and then displays it below in the desired format (DD-MM-YYYY):
<p id="example"></p>
Example 2: Subtract 2 Working Days and Format
If by working days you mean Monday to Friday, all you'd need to do here is determine whether the day held in your date variable was a Monday or Tuesday, then adjust the value passed in to MomentJS's subtract method accordingly. We can do this using MomentJS's get day of week function:
var date = new Date(),
formatted, daysToSubtract;
switch (moment(date).day()) {
// Sunday = 3 days
case 0:
daysToSubtract = 3;
break;
// Monday and Tuesday = 4 days
case 1:
case 2:
daysToSubtract = 4;
break;
// Subtract 2 days otherwise.
default:
daysToSubtract = 2;
break;
}
formatted = moment(date).subtract(daysToSubtract, 'days').format('DD-MM-YYYY');
document.getElementById('date').innerHTML = date;
document.getElementById('example').innerHTML = formatted;
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.2/moment.min.js"></script>
This example takes today's date (<span id="date"></span>), subtracts 2 working days (Monday to Friday) from it,
and then displays it below in the desired format (DD-MM-YYYY):
<p id="example"></p>
While I agree with #James Donnelly that momentjs is awesome, here is how you can achieve your task without using an extra library. I created this helper function to do exactly what you're asking. Just pass in your date and however many days you want to add/subtract to it (-2 in your case).
function addDaysToDate(date, days)
{
var result = new Date(date);
result.setDate(result.getDate() + days);
var dd = result.getDate();
var mm = result.getMonth() + 1; // January starts at 0.
var yyyy = result.getFullYear();
if (dd < 10)
{
dd = '0' + dd
}
if (mm < 10)
{
mm = '0' + mm
}
return dd + '/' + mm + '/' + yyyy;
}

JS time calculation - How many times a date has occurred between two dates

I really need some assistance with a time calculation in JS.
Put basically I need to calculate how many times a day of a month has occurred between two dates.
For Example -
A date of 15th of the month between 1st February 2014 to 14 May 2014 would be 3
A date of 15th of the month between 1st February 2014 to 16 May 2014 would be 4
I've looked at moment Jquery library but it estimates that a month is 30 days so I wouldn't be exact and take into consideration leap years - months with 28 days etc..
It really needs to be exact because its for a chargeable event calculation. The dates can spare many years so could lead to in-accuries because of the 30 day thing.
Any help would be appreciated
There are probably a million ways to do this... here's a brute force way:
// add a "addDays() method to Date"
Date.prototype.addDays = function(days)
{
var dat = new Date(this.valueOf());
dat.setDate(dat.getDate() + days);
return dat;
}
// provide two dates and a day ordinal you want to count between the two
function numOrdinalsBetweenDts(Date1, Date2, theOrdinal) {
var temp;
if(Date2 < Date1) { // put dates in the right order (lesser first)
temp = Date1;
Date1 = Date2;
Date2 = temp;
}
var workDate = Date1;
var ctr = 0;
while(workDate < Date2) { // iterate through the calendar until we're past the end
if(workDate.getDate() == theOrdinal) // if we match the ordinal, count it
ctr++;
workDate = workDate.addDays(1); // move the calendar forward a day
}
return ctr;
}
var result = numOrdinalsBetweenDts(new Date("July 21, 1901"), new Date("July 21, 2014"), 2);
console.log(result);
alert(result);
There is a slightly counter-intuitive behavior in the Javascript Date constructor where if you create a new Date with the day set to 0, it will assume the last day of the month. You can the use the following function get the number of days in a month:
function daysInMonth(month, year) {
return new Date(year, month, 0).getDate();
}
The Javascript date object is leap-year aware, so you can use this function reliably.
You then just need to count the number of months between the start and end date and check each one to make sure the day number is actually present in the month. You can short-circuit this check if the day is less than or equal to 28.

Categories

Resources