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/>");
Related
Using this code I am getting the dates excluding the start dates
var startdate = new Date("");
var enddate = new Date("");
while (startdate < enddate) {
startdate.setDate(startdate.getDate() + 1);
dates.push(new Date(startdate).format("mm/dd/yyyy"));
}
You should replace new Date(startdate) with new Date(startdate.getTime()) to avoid JS error. However, you cannot simply format JS Date object with format(), as this function does not exist, you have to extract year, month, day by yourself. If you want to do it, beware the month value is started from zero.
Please consider using a library like Moment.js instead, it is a lot handy to manipulate date, time and duration.
You can't simply format javascript date using format function, which doesn't exists. You need to split date, month, year and join. Here it may help-
var startdate = new Date();
var enddate = new Date("2019, 10, 23");
while (startdate < enddate) {
startdate.setDate(startdate.getDate() + 1);
dates.push(startdate.getMonth()+1+"/"+startdate.getDate()+"/"+startdate.getFullYear());
}
You have to increase moth by 1 startdate.getMonth()+1 because month starts from 0 in JS.
Consider using Moment.js which has lots of facility.
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());
}
How do I compare today's time and date with user selected date. If user selects today's date or tomorrow's date and time is 2pm or more than 2pm then I should show alert saying delivery time over.
I have tried something like this
$scope.checkDateTime=function(){
angular.forEach($scope.orders.start_date,function(s){
console.log(s);
var curTime = moment().format('YYYY-MM-DD HH:mm:ss');
var orderTime = s+' 14:00:00';
console.log(moment(orderTime).diff(curTime,'seconds'));
if(moment(orderTime).diff(curTime,'seconds')>86400) {
console.log('hooray!')
}
})
}
I have orders.start_date is a input field inside ng-repeat so I am using forEach loop. I just want to check if selected date is today's or tomorrow's date. Then I have to check time, and if it's greater than 2pm then I should not allow. Otherwise I can allow.
I am not sure when the acceptable order period starts (since checking if the day is today or tomorrow means that everything from 00:00 to 14:00 is a fair game) , but here is the is one way to do it based on your code:
$scope.checkDateTime = function(){
angular.forEach($scope.orders.start_date, function(s){
console.log(s);
var selectedTime = moment(s);
// create boundaries for time ranges
var today_end = moment("14:00","HH:mm"); // today 14:00
var today_start = moment().subtract(1,'day').endOf('day'); // end of yesterday (since we need to include 00:00 of today)
var tomorrow_end = moment("14:00","HH:mm").add(1,'day'); // tomorrow 14:00
var tomorrow_start = moment().endOf('day'); // end of today (since we need to include 00:00 of tomorrow)
// check if time in questions fits any of the ranges
if( ( selectedTime.isBetween(today_start, today_end) ||
( selectedTime.isBetween(tomorrow_start, tomorrow_end) )
console.log('hooray!')
}
})
}
Note, that isBetween(t1, t2) does not include t1 and t2 into the accepted range.
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;
}
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();
}