Date selection for week-to-date - javascript

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();
}

Related

How do I add 1 to a date in a query string with an onClick function?

I am trying to have previous day, current day, and next day buttons so for example, if I press the next button, it will take today's date, add one to today's date and show tomorrows information on the page.
My click handler looks like:
const nextHandler = () => {
let resDate = new Date();
let year = resDate.getFullYear();
let day = new Date().getDate();
let month = resDate.getMonth() + 1;
if (month.toString().length < 2 || day.toString().length < 2) {
month = ('0' + month).slice(-2);
day = ('0' + day).slice(-2);
}
day = parseInt(day) + 1;
let newDate = `${year}-${month}-${day}`;
// newDate --> 2021-04-11
history.push(`/dashboard?date=${newDate}`);
};
When I click my next button I get taken to: http://localhost:5000/reservations?date=2021-04-12 exactly as I would like. However, I am only able to add to the day once. How am I continuously able to update this query string?
You're only ever starting with new Date(); on your second line so it'll only ever increment once. You'll have to read from the querystring a value to put in new Date(VALUE); if it's set so that it continues to remember. Here's a stackoverflow answer from something like that: How can I get query string values in JavaScript?
You're code may look like:
const urlParams = new URLSearchParams(window.location.search);
const dateParam = urlParams.get('date');
let resDate = dateParam ? new Date(dateParam) : new Date();
It's nextHandler is using today's date to increment rather than the date of the query string.
On the first click, nextHandler today's date to increment. But, the next click should start from the date in the query string.
I hope that solve your problem.

Calculate departure day based on arrival date and duration of program

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());
}

Working with TimeStamps and Moment.js

I have a script that updates my database every day at 9:30am. I’m using Ember.js for my application and I need to write a function that checks if a timestamp is past 9:30am or not. The feature should always be showing data, so if it is before 9:30am it should show the previous day’s data and if it's after then it should show the current day's data. So essentially the function would return a correct timestamp depending on what time of day it is. I’ve tried this using moment.js but cannot figure it out. Any help would be great.
payload.forEach(function(value) {
// console.log("value: ", value );
var nineThirty = ' 09:30:00';
// Create date from input value
var inputDate = moment( value.updated_at ).format('MM/DD/YYYY');
var date = moment(inputDate + nineThirty);
console.log("input Date: ", date );
// yesterday
var yesterdayDate = moment().subtract(1, 'days');
var YD = moment( yesterdayDate ).format('MM/DD/YYYY');
var yesterday = moment(YD + nineThirty );
console.log("here: ", yesterday );
// Get today's date
var todaysDate = moment().format('MM/DD/YYYY');
var today_date = moment(todaysDate + nineThirty);
//... than something
});
use isBefore
this will check if now if 9:30 is before the current time
if true date = today's date else the date is yesterdays date
If(moment().set({hour: 9, minute: 30}).isBefore(moment())){
date = moment().format('MM/DD/YYYY');
}
else{
date= moment().subtract(1, 'days').format('MM/DD/YYYY');
}

Does this code work fine? I'd like to get the timestamp of the following date

I'm building a chrome extension now.
I'd like to set an alarm to be fired at 23:59:59 everyday, so I can reset some saved settings at the end of day.
I wrote this code below but, I'm not used to using Date Object.
And I'm worried if this code works fine and don't mess up everything.
Especially, I'm worried about the code for setting up an alarm again for the next day.
var day = now.getDate() + 1;
To get the date of the following day, I just add 1 to the returned value of "now.getDate()".
But, I wonder if the returned value of "now.getDate()" is the end of the month and because of that, adding 1 ends up getting the date which doesn't exist.
Please take a look at my code and tell me if this works fine or not.
Thank you in advance!!
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth();
var day = now.getDate();
var timestamp = Number(new Date(year, month, day, 23, 59, 59, 0));
//set an alarm for today at 23:59:59.
chrome.alarms.create('resetSpentTime', {
when: timestamp
});
// when alarm fires, do the following.
chrome.alarms.onAlarm.addListener(function() {
//clear some saved settings at the end of day.
//After that, set an alarm again for tomorrow at 23:59:59.
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth();
var day = now.getDate() + 1;
var timestamp = Number(new Date(year, month, day, 23, 59, 59, 0));
//set an alarm for tomorrow at 23:59:59.
chrome.alarms.create('resetSpentTime', {
when: timestamp
});
})
I'll suggest use date.js Link for date.js filefor all date operations in Javascript in which you can add days or parse dates easily. See the demo on given site.
You can try this. Nextday as well as end of month is auto handeled.
var today = new Date();
var tomorrow = new Date();
tomorrow.setDate(today.getDate()+1);
Source: Add days to JavaScript 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