How to subtract hours within object in Javascript - javascript

I need to calculate work hours between two Date. I set up two date.
var startDate = new Date("2/25/2017 20:30");
var endDate = new Date ("3/28/2017 20:58");
I set up days like objects in which i have work start and work end.
var monday = {"start":"09:00", "end":"17:00"};
var tuesday = {"start":"09:00", "end":"17:00"};
var wednesday = {"start":"09:00", "end":"17:00"};
var thursday = {"start":"09:00", "end":"17:00"};
var friday = {"start":"08:00", "end":"13:00"};
var saturday = {"start":"08:00", "end":"11:00"};
var sunday = {"start":"08:00", "end":"09:00"};
Then i put all objects in array:
var workDays = [monday, tuesday, wednesday, thursday, friday, saturday, sunday];
I don't know how to subtract hours in each day. For example, in monday: 17:00 - 09:00 = 8 hours.
I tried this:
var mondayStart = monday.start;
var mondayEnd= monday.end;
In console i get right start and end time of monday object, but i have problem with subtraction.
var mondayDifference = mondayEnd - mondayStart;
console.log(mondayDifference);
But, in console i get: NaN (not a number).
I'll be thankfull for any hints or help.

If you are given the real case not only POC, parseInt will be enough to get the difference
var mondayDifference = parseInt(monday.end) - parseInt(monday.start);
// "17:00" will be 17 ..so on

Related

Google script add row with date only on certain dates

I am trying to make a Google Sheet that automatically adds new rows with a weekday date in the first columns, two days before the respective date (Mondays get added on Saturday, Tuesdays on Sunday, etc.). For this I have written the following function:
function addRowsWithDate() {
var ss = SpreadsheetApp.getActiveSheet();
var Today = new Date();
Today.setDate(Today.getDate()+0); // I am using this to experiment with different dates
var newDate = new Date();
var dayOfWeek = Today.getDay();
if (dayOfWeek<=3 || dayOfWeek==6){ // On Saturday through Wednesday
newDate.setDate(Today.getDate()+2);
ss.insertRowsBefore(1, 1); // Add new rows
ss.getRange(1, 1).setValue(newDate); // Set date to the day after tomorrow
}
}
This works as expected if I leave the +0 as it is, or comment out that line, but once I start changing that value to experiment with what will happen when I run this code on different dates I returns dates in other months. For example, offsetting today to +3 (as of 31 September) adds a row with 5 September. Now that I am typing this I see the issue is possibly with getDate() returning the day of the month instead of the week like I read here. Is that correct? And if so, is there another function that returns the day of the week in Google Sheets scripts?
EDIT: it seems to be some wrapping issue. When I set +3 it returns 5 September instead of 5 October and if I set -30 I get 3 October again.
Instead of creating a new Date() for newDate, define newDate as new Date(Today) and then add 2 days to it inside the if loop.
Solution:
function addRowsWithDate() {
var ss = SpreadsheetApp.getActiveSheet();
var Today = new Date();
Today.setDate(Today.getDate()+3); // I am using this to experiment with different dates
var newDate = new Date(Today);
var dayOfWeek = Today.getDay();
if (dayOfWeek<=3 || dayOfWeek==6){ // On Saturday through Wednesday
newDate.setDate(newDate.getDate()+2);
ss.insertRowsBefore(1, 1); // Add new rows
ss.getRange(1, 1).setValue(newDate); // Set date to the day after tomorrow
ss.getRange(1, 2).setValue(Today);
}
}

Javascript moment - timezone, difference between dates in different time zones

I have:
var now = moment.format(); //get current time
var days = 5; //days I need to subtract from time(then)
var then = '05/02/2016 12:00 am';
Now I need to get difference between now and then substract(-) 5 days but in +0000 so GMT +0.
so now must be in user localtime and then must be at +0000 GMT.
How I can get difference between this dates in days, hours, minutes, seconds?
I try:
var now = moment().format();
var then = moment('05/02/2016 12:00 am').utcOffset(+0000).format();
then = moment(then).subtract(5,'days');
d = moment.utc(moment(now).diff(moment(then))).format("DD HH:mm:ss");
but I get result- which is wrong...
"27 18:48:55"
The problem is that you're trying to use a time difference as a time. You need to use moment.duration() with the return value of the diff. You should also call then.diff(now) to get a positive difference. There were also some unnecessary calls to .format() and moment() that I removed.
var now = moment();
var then = moment('05/02/2016 12:00 am').utcOffset(+0000).subtract(5, 'days');
var duration = moment.duration(then.diff(now));
console.log(duration.days(), duration.hours(), duration.minutes(), duration.seconds());
logs
4 3 15 46

How to use moment to find first monday 18 month ago

I am writing some javascript and came across moment library.I would like to use the moment to get the first Monday of 18 Month ago. How would I do it?
I know 18 months ago it is
moment().subtract(18, 'months');
But how would I know the first Monday of that time.
Thanks ahead of time!
You can do this in one line.
moment().subtract(18,'months').startOf('month').add(6 - moment().day("Monday").day() ,'days').startOf('week').day(1);
Find the moment 18 months ago, get the start date of that month, add 6 days since in worst case the first of that month is a tuesday(results in a date which is in the week that includes the first monday) and then get the start of that week.
Voila, all in one line.
1st edit: corrected the calculation to be locale independent and always return a Monday and not beginning of week since that is locale dependent.
2nd edit: locales makes it a bit more complicated. Adding 6 days to a Monday with a locale where the week ends on Saturday will move into another week thus we need to adapt the algorithm to take the Monday week number into consideration, i.e. subtract moment().day("Monday").day() which gives the locale dependent week day number of a Monday(0 or 1). Changed in the code above.
cleaner and more reusable IMHO ;)
var monday = moment().day('Monday').weekday();
var searchFirstMonday = moment().subtract(18, 'months').startOf('month');
while (searchFirstMonday.weekday() !== monday){
searchFirstMonday.add(1, 'day');
}
If you go back 18 months, get the first day of that month, then the Monday of that week, you'll get close:
moment().subtract(18, 'months').startOf('month').day(1)
To make sure the Monday is in the right month, you need to break that down a bit:
var month = moment().subtract(18, 'months');
var firstMonday = month.startOf('month').day(1);
if (firstMonday.month() != month.month()) {
firstMonday = firstMonday.add(7, 'days');
}
If anyone wants to do the same without any libraries.
Not sure if this is the best way to do it :P
var now = new Date();
//Get year and month
var month = now.getMonth();
var year = now.getFullYear();
//Months to go back
var monthsIntoPast = 18;
//Day of week we're looking for
var firstWeekDay= 2;
//Loop for one week
for(var x = 0; x < 7; x++) {
//Go back 18 months to first day + x of the month
var past = new Date(year, month - monthsIntoPast, 2 + x);
//First monday
if(past.getDay() == firstWeekDay) {
//First monday 18 months ago
alert(past.toUTCString())
break;
}
}
You use the following code to get what you need:
// Subtract 18 months from the current date, then get the first day of the month
var date = moment().subtract(18, 'months').startOf('month');
// Get current month
var month = date.month();
// Get monday of the first week of the month
var firstMonday = moment(date).weekday(0);
// Check if the first day of the month is not Monday
// and first monday of first week is in the desired month
if( date.weekday() != 0 && firstMonday.month() != month ){
// Add 1 week if needed
firstMonday.add(1, 'weeks');
}
The example above consideres Monday as the first day of the week. If Monday is not the first day of the week (day with index 0), you can get the correct index using moment.weekdays()
var moment = require('moment');
date = moment().subtract(18, 'months').startOf('month');
First we get the the current date and subtract 18 months. Then we find the start of the month, which as of today is
_d: Tue Jul 01 2014 00:00:00 GMT-0800 (AKDT),
So now we need to adjust for the next Monday. We get the current day of the week with date.day() which will give us a number between 0-7 with 0 being last Sunday and 7 being next Sunday. In this case, the first is on a Tuesday.
>date.day();
2
So we take the offset of the day of the week and subtract it from 8. Why 8 you say? Because if you count every number including 0 and 7 there are 8 numbers. (Think arrays)
//since it's not monday we add the offset
date = date.add(8-date.day(), 'days');
In your code you'd want to check if the First day returned actually IS a Monday so as to not get the second Monday of the month. So the whole thing might look like
date = moment().subtract(18, 'months').startOf('month');
if(date.day() > 1){ //check if it's Monday
//since it's not monday we add the offset
date = date.add(8-date.day(), 'days');
}
returns
_d: Mon Jul 07 2014 00:00:00 GMT-0800 (AKDT),

Javascript add days to a existing calendar object off by one month

I'm trying to read a date from a Calendar object and add a specific amount of days to it (i.e. 7)
Here is the code that I have:
var daysFromBeginDate = parseInt($("#policyDuration").val()) * 7
alert(daysFromBeginDate)
var beginDate = new Date("2015-04-24");
alert(beginDate)
var endDate = new Date("2015-05-08");
alert(beginDate.getDate() + daysFromBeginDate)
endDate.setDate(new Date(beginDate.getDate() + daysFromBeginDate));
alert(endDate.toString())
and I am getting Sun May 31 2015 17:00:000 GMT as my answer. It should be that with one less month, where is the extra month getting added?
JavaScript using the below call, I found out that the month argument counts starting from zero.
new Date(2015, 3, 1); // that's the 1st April 2015!
And what is causing the issue is the below code snippet in your code:-
endDate.getMonth() + 1
That is possibly the reason for your issue..
EDIT:
if the below code
var endDate = new Date("2015-05-08");
is changed to
var endDate = new Date();
you will get correct output..
it is because setDate sets the day of the month and April has only 30 days so it is rolled over and you get it as May and you get 31 because 24+7 is 31..

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