Calculate departure day based on arrival date and duration of program - javascript

Learning examples in comments of E&P Plugin for WordPress
I found a few bugs myself, but still can't make the code work.
Case: Choose Arrival date (date-picker) and program duration from select field (5|7|14|30|60) in days. Generate Departure date
This code from plugin owner don't word at all
jQuery(document).ready(function(){
jQuery('[data-title="Departure"]').on('change',checkDates);
jQuery('[data-title="Program"]').on('change',checkDates);
});
function checkDates(){
var nbDays = jQuery('[data-title="Program"] option:selected').attr('data-price');
var startDate = moment(jQuery('[data-title="Arrival"]').datetimepicker('getDate'));
var endDate = startDate.subtract(nbDays,'days');
jQuery('[data-title="Departure"]').datetimepicker('setDate',endDate.toDate());
}
From first view I find that he use subtract instead of add
I using https://momentjs.com/docs/ and Format date and Subtract days using Moment.js
var startdate = moment();
startdate = startdate.subtract(1, "days");
is equal
var startdate = moment().subtract(1, "days");
So this is mine version:
jQuery(document).ready(function(){
jQuery('[data-title="Arrival"]').on('change',checkDates);
jQuery('[data-title="Program"]').on('change',checkDates);
});
function checkDates(){
var nbDays = jQuery('[data-title="Program"] option:selected').attr('data-price');
var startDate = moment(jQuery('[data-title="Arrival"]').datetimepicker('getDate'));
var endDate = moment().add(nbDays,'days');
jQuery('[data-title="Departure"]').datetimepicker('setDate',endDate.toDate());
}
Still not recalculate departure date on.click and count days not properly

I think you get a wrong end-date because you are doing moment().add(...) which gets the current date at the script execution (equelevant of Date.now()).
Try to use the startDate variable and using moment prototype add on that instead:
var endDate = startDate.add(nbDays,'days');
This will make moment add additional days to the startDate instead of the current date.
End result:
function checkDates(){
var nbDays = jQuery('[data-title="Program"] option:selected').attr('data-price');
var startDate = moment(jQuery('[data-title="Arrival"]').datetimepicker('getDate'));
var endDate = startDate.add(nbDays,'days');
jQuery('[data-title="Departure"]').datetimepicker('setDate',endDate.toDate());
}

Related

Time changes after toLocaleTimeString()

I'm using a google sheets script, which on the click of a button will add values to two fields.
The first will contain the date, the second the time.
For this, I use this piece of code:
var timestamp = new Date();
var date = Utilities.formatDate(timestamp, "GMT+1", "dd/MM/yyyy");
var time = timestamp.toLocaleTimeString('nl-BE');
Now, the issue is that the time is off by 6 hours.
The timestamp value does contain the correct time, the date variable gets the correct date, but the time seems to differ 6 hours after the 'toLocaleTimeString() function.
Use Utilities.formatDate() for time as well, like this:
const timezone = SpreadsheetApp.getActive().getSpreadsheetTimeZone(); // or 'GMT+1'
const timestamp = new Date();
const dateString = Utilities.formatDate(timestamp, timezone, 'dd/MM/yyyy');
const timeString = Utilities.formatDate(timestamp, timezone, 'HH:mm:ss');
console.log(`date and time in ${timezone}: ${dateString} ${timeString}`);

Formatting dates when getting days between 2 dates with JQuery/Javascript

I am trying to calculate the days between 2 dates and it is working as far as I can tell but I keep getting stupidly high numbers which clearly isn't right, I have a feeling this is the way my dates are set out. my dates are set out as dd/mm/yyyy and this is the code I am using:
var diff = new Date(end_date - start_date);
var days = diff/1000/60/60/24;
console.log("diff=>"+days);
This is the question I used to get the answer:
JavaScript date difference Days
When it writes to the console this is the result I get:
diff=>17301.95833332176
I have had a play with the code, although I have not used HTML, i set the vars statically below.
var end_date = new Date("May 25, 2017");
var start_date = new Date("May 23, 2017");
var diff = new Date(end_date - start_date);
var days = diff/1000/60/60/24;
console.log("diff=>"+days);
I have also checked it with a 3 value date format
var end_date = new Date(2017,4,25);
var start_date = new Date(2017,4,23);
var diff = new Date(end_date - start_date);
var days = diff/1000/60/60/24;
console.log("diff=>"+days);
I manage to get an output of 2. Which is what i expected. The code you supplied looks ok to me. Maybe look at the HTML to check that the values being passed are in the correct format.
Jquery datepicker may be of help to you here.
You could also use moment: https://momentjs.com
var moment = require('moment');
var start_moment = moment(start_date);
var end_moment = moment(end_date);
var days = start_moment.diff(end_moment, 'days');
console.log("diff=>" + days);
You can also get weeks, months etc. with this method
Easy solution, is to use countBtw
var { date } = require('aleppo')
//..
date.countBtw('all', date1, date2)

Get the date between 2 dates using node.js

In node.js I need to get the date within the date range of particular gap.
Consider two dates:
startDate:2016-07-10T00:00:00.000Z,
payByDate:"13"(13th of every month);
endDate:2016-10-08T00:00:00.000Z
I need an array of dates of monthly or weekly gap between these 2 dates.
My result Should be:(Monthly gap)
[2016-07-13T00:00:00.000Z,
2016-08-13T00:00:00.000Z,
2016-09-13T00:00:00.000Z]
EDIT:
My startdate is 2016-07-10T00:00:00.000Z , so i can calculate the noOfmonths using endDate-startdate, but the array entry should be by payByDate.
I am using MOMENT.JS, but its returning only noOfMonths not the dates. Please share your ideas. Thanks in advance.
Now is it possible to calculate the calculate the Above array.Please note that "payByDate" is string.
you can use getMonth() function to get the month of your date. then keep adding one to it till you reach the end date.
var arrayDates = [];
arrayDates.push(startDate);
var tempDate = new Date(new Date(startDate).setMonth(startDate.getMonth()+1));
while(tempDate < endDate)
{
arrayDates.push(tempDate);
var tempDate = new Date(new Date(tempDate).setMonth(tempDate.getMonth()+1));
}
Abouve logic will work for monthly gap. to get array of weekly gap, use getDate() function and keep adding 7 to it till you reach the end Date
Try using the manipulate method in moment.js to add a month to the starting date until you reach the second date.
Consider this code. Assuming that the first date and the last date are already moments when they are passed into this function, you can use the diff and manipulate methods to create an array of dates by month in between the two different dates:
EDIT:
function monthsBetween(payByDate, startDate, lastDate) {
var months = [];
//finds nearest date to the start date that is on the payByDate
var currentDate = startDate.date(parseInt(payByDate));
//checks if the date is after the startDate and adds a month if it is
if(!currentDate.isSameOrAfter(startDate)){
currentDate = currentDate.add(1, 'months');
}
while (currentDate.isSameOrBefore(lastDate)) {
months.push(currentDate);
currentDate = currentDate.add(1, 'months');
}
return months;
}

Date selection for week-to-date

I have a comboBox DateType with options Today, Yesterday, Week-to-date and Manual Entry.
I also have 2 textboxes - one for Start Date and one for End Date.
By default, the DateType will be set to Today's date.
For eg.: Let's say, current date is 01/11/2011 (Tues)
When DateType : Today
Start Date = 01/11/2011
End Date = 01/11/2011
When DateType : Week-to-date (Note: Each week will be `Sun to Sat`)
Start Date = 01/09/2012 (Sun)
End Date = 01/11/2012 (Current Date)
Few Variables:
var startDate = new Date(document.getElementsByName('Start Date').value);
var endDate = new Date(document.getElementsByName('End Date').value);
I am new to javascript, so could anyone help me frame a function that can modify the Start Date and End Date based on the DateType selected(as above).
I need the condition only for Week-to-date
The rest would be similar,I guess, so I can frame the remaining conditions.
Thanks in advance!
In response to the onchange event of the select control you can set a date range something like:
function setDateRange()
{
var periodSelection = dateseln.period.options[dateseln.period.selectedIndex].value;
var start;
switch (periodSelection)
{
case "Today":
start = new Date();
break;
case "WeekToDate":
start = new Date();
start.setDate(start.getDate() - start.getDay());
break;
}
dateseln.startDate.value = start;
dateseln.endDate.value = new Date();
}

Selecting dates - javascript

I have a comboBox DateType with options Today, Yesterday, Week-to-date
I also have 2 textboxes - one for Start Date and one for End Date.
By default, the DateType will be set to Today's date.
For eg.: Let's say, current date is 01/11/2011 (Tues)
When DateType : Today
Start Date = 01/11/2011
End Date = 01/11/2011
When DateType : Week-to-date (Note: Each week will be `Sun to Sat`)
Start Date = 01/09/2012 (Sun)
End Date = 01/11/2012 (Current Date)
Few Variables:
var startDate = new Date(document.getElementsByName('Start Date').value);
var endDate = new Date(document.getElementsByName('End Date').value);
I am new to javascript, so could anyone help me frame a function that can modify the Start Date and End Date based on the DateType selected(as above).
I need the condition only for Week-to-date
The rest would be similar,I guess, so I can frame the remaining conditions.
Thanks in advance!
Download Date JS (http://code.google.com/p/datejs/downloads/list)
Use following code
currentDate = new Date().toString('M/d/yyyy');
lastSunday = Date.today().add(-8).sunday().toString('M/d/yyyy');
document.write("currentDate :"+currentDate+"<br/>");
document.write("lastSunday :"+lastSunday+"<br/>");

Categories

Resources