Check date range with an array - javascript

I have an array, in which I have inserted some day that are holidays.
In my app I choose an interval (startDate and endDate) and I should check that this interval is not in my array. How can I do?
For example:
holidaysArray = [
"2020-04-12",
"2020-04-13",
"*-03-17",
"*-05-01",
"*-06-02"
}
I choose as
startDate: 2022-03-15T16:16:00.000Z
endDate:2022-03-18T16:12:23.103Z
What I expect
So now I should count the day between the startDate and EndDate, and the result is 4 days. But I my array there is value "*-03-17", so I should not considering a day.
How can I do?
I have a datepicker:
<DatePicker
modal
open={showStartDate}
date={startDate}
title={null}
confirmText="Ok"
cancelText="Cancel"
mode="date"
onConfirm={(date) => {
setShowStartDate(false)
onChangeStart( date)
}}
onCancel={() => setShowStartDate(false)}
/>
<DatePicker
modal
open={showEndDate}
date={endDate}
title={null}
confirmText="Ok"
cancelText="Cancel"
mode="date"
onConfirm={(date) => {
setShowEndDate(false)
onChangeStart( date)
}}
onCancel={() => setShowEndDate(false)}
/>
So my
onChangeStart = (selectedDate) => {
checkDate(selectedDate, endDate)
}
checkDate = (start, end) => {
const holidayArray = travelInfo.time.holidayArray
//
}

When you iterate over 'holidayArray' you need to check if the holiday falls between the selected date, if it does just decrement the total difference between the dates by '1' (assuming you are counting Sundays' as well).
For the case where the dates have *, you need to find the 'years' that can fall between the dates and use them to replace the * to form the appropriate date.
First Step (Find year (or years if the selection is too large, just in case))
(assuming endDate > startDate)
const findYears = (startDate, endDate) => {
let yearsArray = [];
let endYear = parseInt(endDate?.split("-")[0]);
let startYear= parseInt(startDate?.split("-")[0]);
yearsArray.push(endYear);
while(endYear !== startYear){
endYear--;
yearsArray.push(endYear);
}
return yearsArray;
}
Now complete your checkDate function
checkDate = (start, end) => {
const years = findYears(start, end)
const holidayArray = travelInfo.time.holidayArray;
var from = new Date(start);
var to = new Date(end);
var diff = 0;
holidayArray.forEach(holiday => {
if (holiday.match("\\*")) {
years.forEach(year => {
var check = new Date(holiday.replace("\\*", year));
if (check > from && check < to) {
diff--
}
})
} else {
var check = new Date(holiday);
if (check > from && check < to) {
diff--
}
}
})
const diffTime = Math.abs(start - end);
var diffDays = Math.round(diffTime / (1000 * 60 * 60 * 24));
return diffDays + diff
}

Related

Getting date range based on date and hour

I am trying to get individual dates ("2022-10-10") and hours ("2022-10-10T09") between an interval in UTC. I could get the individual dates by the following -
function getDatesInRange(startDate, endDate) {
const date = new Date(startDate.getTime());
const dates = [];
while (date <= endDate) {
const day = new Date(date).toISOString().split(':')[0].split('T')[0];
dates.push(day);
date.setDate(date.getDate() + 1);
}
return dates;
}
console.log(getDatesInRange(new Date('2022-10-10T20:50:59.938Z'), new Date('2022-10-15T23:50:59.938Z')));
Hence, the above returns - ["2022-10-10", "2022-10-11", "2022-10-12", "2022-10-13", "2022-10-14", "2022-10-15"]
I also want to return the hours of the start and end date and the rest should be dates. So i want to get in return - ["2022-10-10T20", "2022-10-10T21", "2022-10-10T22", "2022-10-10T23" "2022-10-11", "2022-10-12", "2022-10-13", "2022-10-14", "2022-10-15T00", "2022-10-15T01"]
Here is what i have as of now -
function getHoursInRange(startDate, endDate) {
let startDatePlusOne = new Date(startDate);
startDatePlusOne.setDate(startDatePlusOne.getDate() + 1);
let endDateMinusOne = new Date(endDate);
endDateMinusOne.setDate(endDateMinusOne.getDate() - 1);
const date = new Date(startDate.getTime());
console.log("Start date :", date);
let dates = getDatesInRange(startDatePlusOne, endDateMinusOne);
console.log("Only days : ", dates);
startDatePlusOne.setHours(0);
while (date < startDatePlusOne) {
const day = new Date(date).toISOString().split(':')[0];
dates.push(day);
date.setHours(date.getHours() + 1);
}
endDateMinusOne.setHours(23);
const edate = endDateMinusOne.getTime();
while (edate < endDate) {
const day = new Date(edate).toISOString().split(':')[0];
dates.push(day);
date.setHours(date.getHours() + 1);
}
return dates
}
For this use case, i am getting the days back excluding the start and end dates. But for getting each hour of start and end date it gets stuck somehow. Somehow i feel there is a better way to do this. Any ideas ?
You can do it a simpler way by incrementing the timestamp by 30 minutes at a time, and keeping a note of all non-duplicate hour strings and date strings:
function getDatesInRange(startDate, endDate) {
let h = new Set(), d = new Set(), t = [];
for(let i=startDate.getTime(); i<endDate.getTime(); i+=1000*1800) t.push(i);
[...t, endDate.getTime()].forEach(i=>{
let s = new Date(i).toISOString();
[[s.split(':')[0], h], [s.split('T')[0], d]].forEach(([s,r])=>r.add(s));
});
let firstDate = [...d.values()][0], lastDate = [...d.values()].pop();
return d.size===1 ? [...h.values()] : [
...[...h.values()].filter(v=>v.startsWith(firstDate)),
...[...d.values()].filter(v=>v!==firstDate && v!==lastDate),
...[...h.values()].filter(v=>v.startsWith(lastDate))];
}
console.log(getDatesInRange(
new Date('2022-10-10T20:50:59.938Z'), new Date('2022-10-15T23:50:59.938Z')));
dateRange constructs an array of Date objects corresponding to the supplied range, inclusive.
dayToString takes a date and creates an array of strings, one for each hour of the day between the specified UTC hour range, inclusive.
dateRangeToStrings accepts an array of dates and constructs an array of strings according to the rules laid-out in the question.
const twoDigit = (n) => String(n).padStart(2, '0')
const toISODateString = (date) => `${date.getUTCFullYear()}-${twoDigit(date.getUTCMonth() + 1)}-${twoDigit(date.getUTCDate())}`
const dateRange = (start, end, curr = new Date(start)) => {
const dates = []
while (curr <= end) {
dates.push(new Date(Date.UTC(curr.getUTCFullYear(), curr.getUTCMonth(), curr.getUTCDate())))
curr.setUTCDate(curr.getUTCDate() + 1)
}
return dates
}
const dayToString = (date, startUTCHour = 0, endUTCHour = 23) =>
Object.keys([...Array(24)])
.slice(startUTCHour, endUTCHour + 1)
.map((h)=>`${toISODateString(date)}T${twoDigit(h)}`)
const dateRangeToStrings = (arr, startUTCHour, endUTCHour) => {
const beginning = dayToString(arr[0], startUTCHour)
const middle = arr.slice(1, -1).map(toISODateString)
const end = dayToString(arr[arr.length - 1], 0, endUTCHour)
return beginning.concat(middle, end)
}
const getDatesInRange = (start, end) =>
dateRangeToStrings(dateRange(start, end),
start.getUTCHours(),
end.getUTCHours())
console.log(getDatesInRange(new Date('2022-10-10T20:50:59.938Z'),
new Date('2022-10-15T23:50:59.938Z')))

How to get the full dates between two dates using javascript / react?

My requirement is to get the number of days between two dates.
For example, the start date is 02/20/2020 to 01/03/2020 I would like to display the results like
Feb 20 Thursday, Feb 21 Friday,....01 Mar Monday.
I went through this scenario in StackOverflow but I didn't get the expected solution for the same.
Could anyone please guide in achieving this scenario using javascript or react?
You may calculate the difference between dates, than make up desired array of dates casted to date string of necessary format:
const d1 = new Date('02/20/2020'),
d2 = new Date('03/01/2020'),
diff = (d2-d1)/864e5,
dateFormat = {weekday:'long',month:'short',day:'numeric'},
dates = Array.from(
{length: diff+1},
(_,i) => {
const date = new Date()
date.setDate(d1.getDate()+i)
const [weekdayStr, dateStr] = date.toLocaleDateString('en-US',dateFormat).split(', ')
return `${dateStr} ${weekdayStr}`
}
)
console.log(dates)
.as-console-wrapper {min-height:100%;}
Or, as long as we're having fun here :) following is React implementation:
const { render } = ReactDOM,
{ useState } = React
const DatePicker = ({min,max,onPick,role}) => (
<input
type="date"
onChange={onPick}
{...{min,max}}
/>
)
const ListOfDates = ({startDate,endDate}) => {
const d1 = new Date(startDate),
d2 = new Date(endDate),
diff = (d2-d1)/864e5,
dateFormat = {weekday:'long',month:'short',day:'numeric'},
dates = Array.from(
{length: diff+1},
(_,i) => {
const date = new Date()
date.setDate(d1.getDate()+i)
const [weekdayStr, dateStr] = date.toLocaleDateString('en-US',dateFormat).split(', ')
return `${dateStr} ${weekdayStr}`
}
)
return (
<ul>
{dates.map((date,key) => <li {...{key}}>{date}</li>)}
</ul>
)
}
const App = () => {
const [start, setStart] = useState(''),
[end, setEnd] = useState(''),
onPickStart = ({target:{value}}) => setStart(value),
onPickEnd = ({target:{value}}) => setEnd(value)
return (
<div>
<DatePicker max={end} onPick={onPickStart} />
<DatePicker min={start} onPick={onPickEnd} />
<ListOfDates startDate={start} endDate={end} />
</div>
)
}
render (
<App />,
document.getElementById('root')
)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.12.0/umd/react.production.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.11.0/umd/react-dom.production.min.js"></script><div id="root"></div>
...and jQuery one:
$(document).ready(() => {
$('.datepick').on('change', function(){
$(this).attr('id') == 'startDate' ?
$('#endDate').attr('min', $(this).val()) :
$('#startDate').attr('max', $(this).val())
if($('#startDate').length && $('#endDate').length) {
const d1 = new Date($('#startDate').val()),
d2 = new Date($('#endDate').val()),
diff = (d2-d1)/864e5,
dateFormat = {weekday:'long',month:'short',day:'numeric'},
dates = Array.from(
{length: diff+1},
(_,i) => {
const date = new Date()
date.setDate(d1.getDate()+i)
const [weekdayStr, dateStr] = date.toLocaleDateString('en-US',dateFormat).split(', ')
return `${dateStr} ${weekdayStr}`
}
),
dateListItems = dates.map(d => `<li>${d}</li>`)
$('#dateList').html(dateListItems)
}
})
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label>Start Date: <input id="startDate" type="date" class="datepick"></input></label>
<label>End Date: <input id="endDate" type="date" class="datepick"></input></label>
<ul id="dateList"></ul>
you can use momentjs to get the result:
//let moment = require("moment");
let date = [];
let startDate = "02/20/2020";
let endDate = "01/03/2020";
while ( moment(startDate, "MM/DD/YYYY").valueOf() <= moment(endDate, "DD/MM/YYYY").valueOf()) {
date.push(moment(startDate, "MM/DD/YYYY").format("MMM DD dddd"));
startDate = moment(startDate, "MM/DD/YYYY").add(1, "days").format("MM/DD/YYYY");
}
console.log(date);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
You can start by first creating two date objects, one for the start date and another for the end date. Then, find out how many days are in between these dates. Finally, you can loop through this number and get the current date plus the current index in the loop and print that.
As a React component:
const App = () => {
const [dates, setDates] = React.useState([]);
React.useEffect(() => {
const start = new Date('02/20/2020');
const end = new Date('03/01/2020');
const daysBetween = (end.getTime() - start.getTime()) / (1000 * 3600 * 24);
const arr = [];
for (let i = 0; i <= daysBetween; i++) {
const temp = new Date();
temp.setDate(start.getDate() + i)
arr.push(temp);
}
setDates(arr);
}, []);
return (
<ul>
{dates.map(date => (
<li key={date}>
{date.toLocaleDateString(
"en-US",
{month: "short", day: "2-digit", weekday: "long"}
)}
</li>
))}
</ul>
);
}
ReactDOM.render(<App />, document.getElementById("app"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.12.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.12.0/umd/react-dom.production.min.js"></script>
<div id="app"></div>
From the date given, find the difference in days then based on the no of days, make a loop and log each increment day in between using toLocaleString()..
const startDate = "02/20/2020";
const endDate = "03/01/2020";
const diffTime = Math.abs(new Date(endDate) - new Date(startDate));
const diffDays = 0|diffTime/864e5;
for(let i = 0; i <= diffDays; i++){
const newdate = new Date(new Date(startDate).getTime()+(i*864e5));
console.log(newdate.toLocaleString('en-us', { day:'2-digit', month: 'short', weekday:'long'}))
}
Another method to get the difference between two dates in JavaScript:
const d1 = new Date("06/30/2019");
const d2 = new Date("07/30/2019");
// To calculate the time difference of two dates
const timeDiff = d2.getTime() - d1.getTime();
// To calculate the no. of days between two dates
const days = timeDiff / (1000 * 3600 * 24);
//to list the days
while (days !== 0) {
let date = new Date(d2)
date.setDate(date.getDate() - days)
console.log(date)
days--
}

get last 7 days when user picks up a date

I have a datetimepicker where the user picks up a date, and my requirement is I need 7 days difference between his selected date.
For eg,
if user has selected 2017-03-01 so i need last 7 days from 2017-03-01 and NOT the current date
All answers i checked here were based on days difference from today.
Can anyone help me out here ?
$("#dateTimePickerIdWhereUserSelectsHisDate").val() - (7 * 24 * 60 * 60 * 1000);
this was on one of the answers but didn't work.
How can I achieve this ?
Try This
SelectDateTime will give you selected date
604800000 is 7 days in miliseconds
prevDate will give you last 7 days Date
$("#startDate").on("dp.change", function(e) {
if (e.oldDate != null) {
if (e.date.format('D') != e.oldDate.format('D')) {
var selectDateTime = e.date["_d"].getTime();
var prevDateTImeMili = selectDateTime - 604800000;
var prevDate = msToDateTime(prevDateTImeMili)
$('#startDate').data("DateTimePicker").hide();
}
}
});
msToDateTime is a function which converts milliseconds to DateTime
function msToDateTime(s) {
Number.prototype.padLeft = function(base,chr){
var len = (String(base || 10).length - String(this).length)+1;
return len > 0? new Array(len).join(chr || '0')+this : this;
}
if(s != null){
s = new Date(s);
// var d = new Date(s);
// var d = new Date(s.getTime()+s.getTimezoneOffset()*60*1000+timeConversionToMilliseconds(sessionStorage.getItem("accounttimezone").split('+')[1]+':00'))
var d = new Date(s.getTime()+(s.getTimezoneOffset()*60*1000)+ (330 *60*1000));
dformat = [ d.getFullYear(),
(d.getMonth()+1).padLeft(),
d.getDate().padLeft()].join('-')+
' ' +
[ d.getHours().padLeft(),
d.getMinutes().padLeft(),
d.getSeconds().padLeft()].join(':');
return dformat;
}else{
return " ";
}
}
function getNDaysBefore(dateString, numberOfDaysBefore) {
let startingDate = new Date(dateString).getTime();
let datesArray = [],
daysCounter = 0,
day = 1000 * 60 * 60 * 24;
while (daysCounter < numberOfDaysBefore + 1) {
let newDateBeforeStaring = startingDate - day * daysCounter;
datesArray.push(new Date(newDateBeforeStaring));
daysCounter++;
}
return datesArray;
}
var dateString = "2016-03-01";
alert(getNDaysBefore(dateString,7));
With that kind of a function you can get any N days before the given date as an array of Date objects

How to exclude weekends between two dates using Moment.js

I am trying to exclude weekends in my JavaScript code. I use moment.js and having difficulty choosing the right variable for 'days'.
So far I have thought that I need to exclude day 6 (saturday) and day 0 (sunday) by changing the weekday variable to count from day 1 to day 5 only. But not sure how it changes.
My jsfiddle is shown here: FIDDLE
HTML:
<div id="myContent">
<input type="radio" value="types" class="syncTypes" name="syncTypes"> <td><label for="xshipping.xshipping1">Free Shipping: (<span id="fsv1" value="5">5</span> to <span id="fsv2" value="10">10</span> working days)</label> </td><br>
<div id="contacts" style="display:none;border:1px #666 solid;padding:3px;top:15px;position:relative;margin-bottom:25px;">
Contacts
</div>
<input type="radio" value="groups" class="syncTypes" name="syncTypes"> <td><label for="xshipping.xshipping2">Express Shipping: (<span id="esv1" value="3">3</span> to <span id="esv2" value="4">4</span> working days)</label> </td>
<div id="groups" style="display:none;border:1px #666 solid;padding:3px;top:15px;position:relative">
Groups
</div>
</div>
JavaScript:
var a = 5; //Free shipping between a
var b = 10;//and b
var c = 3;//Express shipping between c
var d = 4;//and d
var now = moment();
var f = "Your item will be delivered between " + now.add("days",a).format("Do MMMM") + " and " + now.add("days",b).format("Do MMMM");
var g = "Your item will be delivered between " + now.add("days".c).format("Do MMMM") + " and " + now.add("days",d).format("Do MMMM");
var h = document.getElementById('contacts');
h.innerHTML = g
var i = document.getElementById('groups');
i.innerHTML = f
$(function() {
$types = $('.syncTypes');
$contacts = $('#contacts');
$groups = $('#groups');
$types.change(function() {
$this = $(this).val();
if ($this == "types") {
$groups.slideUp(300);
$contacts.delay(200).slideDown(300);
}
else if ($this == "groups") {
$contacts.slideUp(300);
$groups.delay(200).slideDown(300);
}
});
});
Here you go!
function addWeekdays(date, days) {
date = moment(date); // use a clone
while (days > 0) {
date = date.add(1, 'days');
// decrease "days" only if it's a weekday.
if (date.isoWeekday() !== 6 && date.isoWeekday() !== 7) {
days -= 1;
}
}
return date;
}
You call it like this
var date = addWeekdays(moment(), 5);
I used .isoWeekday instead of .weekday because it doesn't depend on the locale (.weekday(0) can be either Monday or Sunday).
Don't subtract weekdays, i.e addWeekdays(moment(), -3) otherwise this simple function will loop forever!
Updated JSFiddle http://jsfiddle.net/Xt2e6/39/ (using different momentjs cdn)
Those iteration looped solutions would not fit my needs.
They were too slow for large numbers.
So I made my own version:
https://github.com/leonardosantos/momentjs-business
Hope you find it useful.
https://github.com/andruhon/moment-weekday-calc plugin for momentJS might be helpful for similar tasks
It does not solves the exact problem, but it is able to calculate specific weekdays in the range.
Usage:
moment().isoWeekdayCalc({
rangeStart: '1 Apr 2015',
rangeEnd: '31 Mar 2016',
weekdays: [1,2,3,4,5], //weekdays Mon to Fri
exclusions: ['6 Apr 2015','7 Apr 2015'] //public holidays
}) //returns 260 (260 workdays excluding two public holidays)
If you want a pure JavaScript version (not relying on Moment.js) try this...
function addWeekdays(date, days) {
date.setDate(date.getDate());
var counter = 0;
if(days > 0 ){
while (counter < days) {
date.setDate(date.getDate() + 1 ); // Add a day to get the date tomorrow
var check = date.getDay(); // turns the date into a number (0 to 6)
if (check == 0 || check == 6) {
// Do nothing it's the weekend (0=Sun & 6=Sat)
}
else{
counter++; // It's a weekday so increase the counter
}
}
}
return date;
}
You call it like this...
var date = addWeekdays(new Date(), 3);
This function checks each next day to see if it falls on a Saturday (day 6) or Sunday (day 0). If true, the counter is not increased yet the date is increased.
This script is fine for small date increments like a month or less.
I would suggest adding a function to the moment prototype.
Something like this maybe? (untested)
nextWeekday : function () {
var day = this.clone(this);
day = day.add('days', 1);
while(day.weekday() == 0 || day.weekday() == 6){
day = day.add("days", 1);
}
return day;
},
nthWeekday : function (n) {
var day = this.clone(this);
for (var i=0;i<n;i++) {
day = day.nextWeekday();
}
return day;
},
And when you're done and written some tests, send in a pull request for bonus points.
d1 and d2 are moment dates passed as an argument to calculateBusinessDays
calculateBusinessDays(d1, d2) {
const days = d2.diff(d1, "days") + 1;
let newDay: any = d1.toDate(),
workingDays: number = 0,
sundays: number = 0,
saturdays: number = 0;
for (let i = 0; i < days; i++) {
const day = newDay.getDay();
newDay = d1.add(1, "days").toDate();
const isWeekend = ((day % 6) === 0);
if (!isWeekend) {
workingDays++;
}
else {
if (day === 6) saturdays++;
if (day === 0) sundays++;
}
}
console.log("Total Days:", days, "workingDays", workingDays, "saturdays", saturdays, "sundays", sundays);
return workingDays;
}
If you want a version of #acorio's code sample which is performant (using #Isantos's optimisation) and can deal with negative numbers use this:
moment.fn.addWorkdays = function (days) {
// Getting negative / positive increment
var increment = days / Math.abs(days);
// Looping weeks for each full 5 workdays
var date = this.clone().add(Math.floor(Math.abs(days) / 5) * 7 * increment, 'days');
// Account for starting on Saturdays and Sundays
if(date.isoWeekday() === 6) { date.add(-increment, 'days'); }
else if(date.isoWeekday() === 7) { date.add(-2 * increment, 'days'); }
// Adding / removing remaining days in a short loop, jumping over weekends
var remaining = days % 5;
while(remaining != 0) {
date.add(increment, 'days');
if(date.isoWeekday() !== 6 && date.isoWeekday() !== 7)
remaining -= increment;
}
return date;
};
See Fiddle here: http://jsfiddle.net/dain/5xrr79h0/
Edit: now fixed issue adding 5 days to a day initially on a weekend
I know this question was posted long ago, but in case somebody bump on this, here is optimized solution using moment.js:
function getBusinessDays(startDate, endDate){
var startDateMoment = moment(startDate);
var endDateMoment = moment(endDate)
var days = Math.round(startDateMoment.diff(endDateMoment, 'days') - startDateMoment .diff(endDateMoment, 'days') / 7 * 2);
if (endDateMoment.day() === 6) {
days--;
}
if (startDateMoment.day() === 7) {
days--;
}
return days;
}
const calcBusinessDays = (d1, d2) => {
// Calc all days used including last day ( the +1 )
const days = d2.diff(d1, 'days') + 1;
console.log('Days:', days);
// how many full weekends occured in this time span
const weekends = Math.floor( days / 7 );
console.log('Full Weekends:', weekends);
// Subtract all the weekend days
let businessDays = days - ( weekends * 2);
// Special case for weeks less than 7
if( weekends === 0 ){
const cur = d1.clone();
for( let i =0; i < days; i++ ){
if( cur.day() === 0 || cur.day() === 6 ){
businessDays--;
}
cur.add(1, 'days')
}
} else {
// If the last day is a saturday we need to account for it
if (d2.day() === 6 ) {
console.log('Extra weekend day (Saturday)');
businessDays--;
}
// If the first day is a sunday we need to account for it
if (d1.day() === 0) {
console.log('Extra weekend day (Sunday)');
businessDays--;
}
}
console.log('Business days:', businessDays);
return businessDays;
}
This can be done without looping between all dates in between.
// get nb of weekend days
var startDateMonday = startDate.clone().startOf('isoWeek');
var endDateMonday = endDate.clone().startOf('isoWeek');
var nbWeekEndDays = 2 * endDateMonday.diff(startDateMonday, 'days') / 7;
var isoDayStart = startDate.isoWeekday();
if (isoDayStart > 5) // starts during the weekend
{
nbWeekEndDays -= (8 - isoDayStart);
}
var isoDayEnd = endDate.isoWeekday();
if (isoDayEnd > 5) // ends during the weekend
{
nbWeekEndDays += (8 - isoDayEnd);
}
// if we want to also exlcude holidays
var startOfStartDate = startDate.clone().startOf('day');
var nbHolidays = holidays.filter(h => {
return h.isSameOrAfter(startOfStartDate) && h.isSameOrBefore(endDate);
}).length;
var duration = moment.duration(endDate.diff(startDate));
duration = duration.subtract({ days: nbWeekEndDays + nbHolidays });
var nbWorkingDays = Math.floor(duration.asDays()); // get only nb of complete days
I am iterating from start date to end date and only counting days which are weekdays.
const calculateBusinessDays = (start_date, end_date) => {
const d1 = start_date.clone();
let num_days = 0;
while(end_date.diff(d1.add(1, 'days')) > 0) {
if ([0, 6].includes(d1.day())) {
// Don't count the days
} else {
num_days++;
}
}
return num_days;
}

Check if date is less than 1 hour ago?

Is there a way to check if a date is less than 1 hour ago like this?
// old date
var olddate = new Date("February 9, 2012, 12:15");
// current date
var currentdate = new Date();
if (olddate >= currentdate - 1 hour) {
alert("newer than 1 hour");
else {
alert("older than 1 hour");
}
Also, different question - is there a way to add hours to a date like this?
var olddate = new Date("February 9, 2012, 12:15") + 15 HOURS; // output: February 10, 2012, 3:15
Define
var ONE_HOUR = 60 * 60 * 1000; /* ms */
then you can do
((new Date) - myDate) < ONE_HOUR
To get one hour from a date, try
new Date(myDate.getTime() + ONE_HOUR)
Using some ES6 syntax:
const lessThanOneHourAgo = (date) => {
const HOUR = 1000 * 60 * 60;
const anHourAgo = Date.now() - HOUR;
return date > anHourAgo;
}
Using the Moment library:
const lessThanOneHourAgo = (date) => {
return moment(date).isAfter(moment().subtract(1, 'hours'));
}
Shorthand syntax with Moment:
const lessThanOneHourAgo = (date) => moment(date).isAfter(moment().subtract(1, 'hours'));
the moment library can really help express this. The trick is to take the date, add time, and see if it's before or after now:
lastSeenAgoLabel: function() {
var d = this.lastLogin();
if (! moment(d).isValid()) return 'danger'; // danger if not a date.
if (moment(d).add(10, 'minutes').isBefore(/*now*/)) return 'danger'; // danger if older than 10 mins
if (moment(d).add(5, 'minutes').isBefore(/*now*/)) return 'warning'; // warning if older than 5mins
return 'success'; // Looks good!
},
Using moment will be much easier in this case, You could try this:
let hours = moment().diff(moment(yourDateString), 'hours');
It will give you integer value like 1,2,5,0etc so you can easily use condition check like:
if(hours < 1) {
Also, one more thing is you can get more accurate result of the time difference (in decimals like 1.2,1.5,0.7etc) to get this kind of result use this syntax:
let hours = moment().diff(moment(yourDateString), 'hours', true);
Let me know if you have any further query
//for adding hours to a date
Date.prototype.addHours= function(hrs){
this.setHours(this.getHours()+hrs);
return this;
}
Call function like this:
//test alert(new Date().addHours(4));
You can do it as follows:
First find difference of two dates i-e in milliseconds
Convert milliseconds into minutes
If minutes are less than 60, then it means date is within hour else not within hour.
var date = new Date("2020-07-12 11:30:10");
var now = new Date();
var diffInMS = now - date;
var msInHour = Math.floor(diffInMS/1000/60);
if (msInHour < 60) {
console.log('Within hour');
} else {
console.log('Not within the hour');
}
Plain JavaScript solution with in 12 days and 12 days ago option
const timeAgo = ( inputDate ) => {
const date = ( inputDate instanceof Date) ? inputDate : new Date(inputDate);
const FORMATTER = new Intl.RelativeTimeFormat('en');
const RANGES = {
years : 3600 * 24 * 365,
months : 3600 * 24 * 30,
weeks : 3600 * 24 * 7,
days : 3600 * 24,
hours : 3600,
minutes : 60,
seconds : 1
};
const secondsElapsed = (date.getTime() - Date.now()) / 1000;
for (let key in RANGES) {
if ( RANGES[key] < Math.abs(secondsElapsed) ) {
const delta = secondsElapsed / RANGES[key];
return FORMATTER.format(Math.round(delta), key);
}
}
}
// OUTPUTS
console.log( timeAgo('2040-12-24') )
console.log( timeAgo('6 Sept, 2012') );
console.log( timeAgo('2022-05-27T17:45:01+0000') );
let d = new Date()
console.log( "Date will change: ", timeAgo( d.setHours(24,0,0,0) ) );
// d.setDate( d.getDate() - 0 );
d.setHours(-24,0,0,0); // (H,M,S,MS) | 24 hours format
console.log("Day started: " , timeAgo( d ) );
//try this:
// to compare two date's:
<Script Language=Javascript>
function CompareDates()
{
var str1 = document.getElementById("Fromdate").value;
var str2 = document.getElementById("Todate").value;
var dt1 = parseInt(str1.substring(0,2),10);
var mon1 = parseInt(str1.substring(3,5),10);
var yr1 = parseInt(str1.substring(6,10),10);
var dt2 = parseInt(str2.substring(0,2),10);
var mon2 = parseInt(str2.substring(3,5),10);
var yr2 = parseInt(str2.substring(6,10),10);
var date1 = new Date(yr1, mon1, dt1);
var date2 = new Date(yr2, mon2, dt2);
if(date2 < date1)
{
alert("To date cannot be greater than from date");
return false;
}
else
{
alert("Submitting ...");
}
}
</Script>
Hope it will work 4 u...

Categories