How to calculate time for this and next month in javascript? - javascript

I'm working on a program where I get dates like this:
2016-08-31T00:00:00
so my goal is to do 3 comparisons:
1.- Need to show "Due" if appoinment has already happended.
2.- Need to show "Due next Month" if appoinment is due next month.
3.- Need to show "Due this month" if appoinment is due this month.
So far I'm able to show message "Due" by doing this:
var someTime = "2016-08-31T00:00:00";
if(new Date(someTime).getTime() < new Date()){
console.log("Due");
}
So how can I get the "Due next Month" and "Due this month" calculations working? Thanks a lot in advance!

Why not something like this:
function appointment(srcDate) {
console.log('');
console.log(srcDate);
var today = new Date();
var todayNextMonth = today.getMonth() + 1;
todayNextMonth = todayNextMonth > 11 ? 0 : todayNextMonth;
if (srcDate < today) {
console.log("Due");
} else if (srcDate.getMonth() === today.getMonth()) {
console.log("Due this month");
} else if (srcDate.getMonth() === todayNextMonth) {
console.log("Due next month");
}
}
appointment(new Date("2016-08-31T00:00:00"));
appointment(new Date("2018-05-29T00:00:00"));
appointment(new Date("2018-06-15T00:00:00"));
See jsfiddle
Use a function, easier to test.
Keep in mind that getMonth() is zero-indexed, so januari equals 0, not 1 etc.
Not sure why you use getTime() to compare with a Date object, you can omit the getTime(). But keep in mind that if an appointment date is 5 minutes in the future, it will show 'Due this month'. You'll have to add in extra logic to show 'Due today' if you require that.

You can get the current month by doing:
const today = new Date();
const month = today.getMonth();
Technically then, you can add 1 to get the next month: const nextMonth = month + 1;
But understand that doing it like that can run into issues if the date is something like Jan 31 (which then the above will give you March as the next month).
See this question and answers for more information on that: Javascript Date: next month
Alternatively, if you're doing a lot of work with dates, you can use a library like moment.js.

Related

Dates stuck in month loops

The dates go to one month either side and then get stuck in loops. Starting in June, it will go fine to end of July, or start of May, but then loop back to the end/start of those months instead of going further.
globalDate is a React state defined const [globalDate, setGlobalDate] = useState(new Date());
Code Snippet:
//decreasing
const newDate = new Date();
newDate.setDate(globalDate.getDate() - 1);
if (globalDate.getMonth() !== newDate.getMonth()) {
newDate.setMonth(globalDate.getMonth());
}
if (globalDate.getDate() <= 1) {
newDate.setMonth(globalDate.getMonth() - 1);
newDate.setDate(daysInMonth[newDate.getMonth()]);
}
setGlobalDate(newDate);
//increasing
const newDate = new Date();
newDate.setDate(globalDate.getDate() + 1);
if (globalDate.getMonth() !== newDate.getMonth()) {
newDate.setMonth(globalDate.getMonth());
}
if (globalDate.getDate() >= daysInMonth[globalDate.getMonth()]) {
newDate.setMonth(globalDate.getMonth() + 1);
newDate.setDate(1);
}
setGlobalDate(newDate);
Full page source : https://github.com/Westsi/thynkr/blob/master/frontend/web/js/src/Planner.js
The problem in the first code block ("decreasing") occurs when newDate.setMonth() is executed when newDate has a date that is the last day of the month, and the previous month has fewer days. So for instance, it happens when newDate is 31 May at the moment this call to setMonth is made. That call will adjust the date to 31 April, but that date automatically translates to 1 May as April only has 30 days, and so you get stuck in the month of May.
To avoid this kind of problems, just start with globalDate immediately and subtract or add one day. That's all. The overflowing into a next/previous month is something that JavaScript already deals with automatically. So instead of trying to do this yourself (and run into trouble), let JavaScript do this for you:
Decreasing logic:
const newDate = new Date(globalDate); // starting point!
newDate.setDate(globalDate.getDate() - 1); // month overflow happens automatically!
setGlobalDate(newDate); // That's it!
Increasing logic:
const newDate = new Date(globalDate); // starting point!
newDate.setDate(globalDate.getDate() + 1); // month overflow happens automatically!
setGlobalDate(newDate); // That's it!

Moment js getting next date given specified week day

I seem to have a bit of a problem getting the previous Monday given a particular date. I'm trying to use Moment js for the task. Obviously, I can do it by hand, but found it curious that I couldn't get it to work using the example in the moment.js documentation on their website: http://momentjs.com/docs/#/get-set/day/.
I was trying something like:
moment([2013, 08, 15, 15, 20]).day(-1).format('ddd, MMM DD')
which results in the 'two days ago' date, that being September 13 instead of the expected September 9th.
Does anybody have a clue here? Thanks.
Here is how it works:
moment().day(1) // this monday
moment().day(-6) // last monday, think of it as this monday - 7 days = 1 - 7 = -6
Same applies in other direction:
moment().day(8) // next monday, or this monday + 7 days = 1 + 7 = 8
Your code moment().day(-1) can be explained as this Sunday - 1 day = 0 - 1 = -1
or this Saturday - 7 days = 6 - 7 = -1
The accepted answer only works if you already know whether the day in question is in this week or next week. What if you don't know? You simply need the next available Thursday following some arbitrary date?
First, you want to know if the day in question is smaller or bigger than the day you want. If it's bigger, you want to use the next week. If it's smaller, you can use the same week's Monday or Thursday.
const dayINeed = 4; // for Thursday
if (moment().isoWeekday() <= dayINeed) {
return moment().isoWeekday(dayINeed);
} else...
If we're past the day we want already (if for instance, our Moment is a Friday, and we want the next available Thursday), then you want a solution that will give you "the Thursday of the week following our moment", regardless of what day our moment is, without any imperative adding/subtracting. In a nutshell, you want to first go into the next week, using moment().add(1, 'weeks'). Once you're in the following week, you can select any day of that week you want, using moment().day(1).
Together, this will give you the next available day that meets your requirements, regardless of where your initial moment sits in its week:
const dayINeed = 4; // for Thursday
// if we haven't yet passed the day of the week that I need:
if (moment().isoWeekday() <= dayINeed) {
// then just give me this week's instance of that day
return moment().isoWeekday(dayINeed);
} else {
// otherwise, give me next week's instance of that day
return moment().add(1, 'weeks').isoWeekday(dayINeed);
}
See also: https://stackoverflow.com/a/27305748/800457
function nextWeekday (day, weekday) {
const current = day.day()
const days = (7 + weekday - current) % 7
return day.clone().add(days, 'd')
}
// example: get next Friday starting from 7 Oct 2019
nextWeekday(moment('2019-10-07'), 5)) // 2019-10-11
I think the point is that using day() or isoWeekday() you get a date in the current week, no matter which day of the week is today. As a consequence, the date you get can be past, or still to come.
Example:
if today is Wednesday, moment().isoWeekday(5).format() would return the date of the upcoming Friday.
While
moment().isoWeekday(1).format() would return the previous Monday.
So when you say you want the date of, let's say, "last Tuesday", this date could belong to the current week or to the previous week, depending on which day is today.
A possible function to get the date of the last dayOfTheWeek is
function getDateOfPreviousDay(dayOfTheWeek) {
currentDayOfTheWeek = moment().isoWeekday();
if ( currentDayOfTheWeek >= dayOfTheWeek ) {
return moment().isoWeekday(dayOfTheWeek).format(); // a date in the current week
}
else {
return moment().add(-1,'weeks').isoWeekday(dayOfTheWeek).format(); // a date in the previous week
}
}
const upcomingDay = (dayIndex, format = "DD MMMM YYYY") => {
if (
Number(moment().format("D")) >= Number(moment().day(dayIndex).format("D"))
) {
return moment()
.day(7 + dayIndex)
.format(format);
}
return moment().day(dayIndex).format(format);
};

How to efficiently calculate consecutive dates given an original date

This is for a system that essentially allows you to set the first date for a given event, then to set the recurrence period.
Eg. I set a date for a week from now, 19/07/2012, so I know that I have to put the cat out with the milk. I also set it to be a weekly notification, so in future weeks I want to be notified of the same.
That original date sits in my database, which is fine for week 1, but in week 2 I need to return the date as the original plus 1 week.
On the face of it, that may seem straightforward, but I need to make sure I can account for leap years and different recurrence frequencies (fortnightly, monthly, yearly, whatever).
I'd like to keep this as a javascript implementation - because it's quicker and I feel probably would require less code than updating dates in the database. Maybe it's not achievable, any pointers would be excellent.
I think these may be a starting point:
Given a start date , how to calculate number of years till current date in javascript
Given a date, how can I efficiently calculate the next date in a given sequence (weekly, monthly, annually)?
Update, I've written the below to return the amount of time to add in each different case, from there I can just use the answer below:
var strDate = $(this).find('.next').text();
var frequency = $(this).find('.occurs').text();
var frmDate = getDateObject(strDate);
var toDate = new Date();
var days = parseInt(Math.floor((frmDate - toDate) / 86400000));
if(days < 0) {
// find out how many WHOLE 'frequencies' have passed
var weeks = Math.ceil(0 - (days / 7));
var months = Math.ceil(0 - (monthDiff(toDate,frmDate)));
var years = Math.ceil(months / 12);
//alert(days + '/' + weeks + '/' + fortnights + '/' + months + '/' + quarters + '/' + years);
if(frequency == 'Weekly') { frmDate.add(weeks).weeks(); }
if(frequency == 'Fortnightly') { frmDate.add(weeks*2).weeks(); }
if(frequency == 'Monthly') { frmDate.add(months).months(); }
if(frequency == 'Quarterly') { frmDate.add(months*3).months(); }
if(frequency == 'Annually') { frmDate.add(years).years(); }
var newdate = frmDate.toString("dd/MM/yyyy");
//alert(newdate);
$(this).find('.next').text(newdate);
}
Also, the SQL implementation for this would be using DATEADD:
http://sql-plsql.blogspot.com/2010/07/dateadd.html
You don't have to worry about special dates like leap year and so forth, because most Date functions take care of that.
Alternatively, you can use the getDate(), getMonth() as the other user suggested.
var today = new Date();
today.setDate(today.getDate() + numberOfDaysToAdd);
What I would do (probably not the best solution, I'm just coming up with it right now) is to start from the initial date and use a loop: while the date you are observing is less than the current date, increment the observed date by a week (fortnight, month, year etc.). If you land on the current date, the event happens. Otherwise it's for another day.
You can use things like date.setDate(date.getDate()+1); to increment the date by a day, the same +7 for a week, using set/getMonth and set/getFullYear for months and years respectively. If you give a value out of bounds, JS will wrap it (so March 32nd becomes April 1st)
Please check out the following code for some raw idea
var someDate = new Date();
for(var i = 0 ; i < 7 ; i++)
{
someDate.setDate(someDate.getDate() + 1);
console.log(someDate)
}
You can test the same in the below fiddle
Consecutive 7 days from current day

setDate() set the wrong date on 31st?

This is very weird I don't know what I'm doing wrong. I have a function to grab the date (i.e in this format: 06/24/2011), here's the function:
function checkDate(input){
var d = new Date();
var dspl = input.split("/");
if(dspl.length != 3)
return NaN;
d.setDate(dspl[1]);
d.setMonth(Number(dspl[0])-1);
if(dspl[2].length == 2)
d.setYear("20"+(dspl[2]+""));
else if(dspl[2].length == 4)
d.setYear(dspl[2]);
else
return NaN;
var dt = jsToMsDate(new Date(d));
return dt;
}
If I enter any date of the month, it would parse the date correctly, but if I enter 31st, i.e "01/31/2011", then it would turn into "01/01/2011". I'm not sure what to do and not really sure where the problem might be.
JavaScript's Date objects allow you to give invalid combinations of months and days; they automagically correct those for you (so for instance, if you set the day of the month to 31 when the month is June, it automatically makes it July 1st). That means if you set the fields individually, you can run into situations where that automagic correction gets in your way.
In your case, if you're going to set all three of those fields, you're better off using the form of the Date constructor that accepts them as arguments:
var dt = new Date(year, month, day);
(If you want hours, minutes, seconds, and milliseconds, you can add them as parameters as well.)
So looking at your code, an off-the-cuff update:
function checkDate(input){
var year, month, day, d, dt;
var dspl = input.split("/");
if(dspl.length != 3)
return NaN;
year = parseInt(dspl[2], 10);
month = parseInt(dspl[0], 10) - 1;
day = parseInt(dspl[1], 10);
if (isNaN(year) || isNaN(month) || isNaN(day)) {
return NaN;
}
if (year < 100) {
year += 2000;
}
d = new Date(year, month, day);
var dt = jsToMsDate(d);
return dt;
}
Some other notes on that update:
It's best to use parseInt to parse numbers from end users, and to always specify the radix (10 for decimal). (No, parseInt is not slower than Number or the unary + trick. People assume it is, but it isn't.)
No need to muck about with strings to add 2000 to years given with only two digits. But you can if you like. Note I weakened the validation there, allowing one-digit years for (say) 2001 and three-digit years for (say) 300 AD. So if you need it to be that strong, you'll need to readjust that.
No need to feed the date instance into new Date() again.
You need to set the month before setting the day (or as Marc B points out in his comment, use the Date(yearval, monthval, dayval) constructor).
When you create a Date object, it defaults to the current date. At the time of writing that's in June, so when you try to set the day to 31 it wraps.
...And because of similar behaviour in leap years, you should set the year before setting the month or day.
(It's a good job you developed this code in June rather than in July - the bug would have lurked undiscovered until September, and it would probably have been your users that found it rather than you. :-)
Right hierarchy is set year, then Month and at last add the Day.
This will return the exact date that you added.
function checkDate() {
//Wrong order- will return 1 May 2016
var start = new Date();
start.setDate(31);
start.setMonth(4);
start.setFullYear(2016);
alert(start)
//Right order - will return 31 May 2016
var end = new Date();
end.setFullYear(2016);
end.setMonth(4);
end.setDate(31);
alert(end)
}
<input type="button" value="Test" onclick="checkDate()" />
This is the right heirarchy to set date.
Why are you adding 1 the day position (position 1)? I think that is your problem.
d.setDate(dspl[1] + 1);

to display only weekdays weekdays

I have a input date picker which will pick the current system date. onclick of button. I need to add nine bussiness days to current day and display it into input value,
so that only weekdays are exclude when we add the days.
For example,
if you have today's date (01/31/2011) and i want to add 9 days, the
answer should be 10/3/06 because the(02/03/2011) weekend should not be counted.
Does anyone know how this can be done?
for this you need to crate one sql function like:
create function fn_IsWeekDay
(
#date datetime
)
returns bit
as
begin
declare #dtfirst int
declare #dtweek int
declare #iswkday bit
set #dtfirst = ##datefirst - 1
set #dtweek = datepart(weekday, #date) - 1
if (#dtfirst + #dtweek) % 7 not in (5, 6)
set #iswkday = 1 --business day
else
set #iswkday = 0 --weekend
return #iswkday
end
Short and sweet. Now you can simply do this:
if dbo.fn_IsWeekDay(#date) = 1
begin
--do some magic here ;
end
--or
implemented functionality
You probably will need something more advanced, but assuming that you just want to discard Saturday and Sunday you can do something like this (just basic calculation, hope I did not count wrong):
<script>
today = new Date();
endDate=today; //Init.
alert(today);
dayOfWeek=today.getDay();
//0: Sunday
if(dayOfWeek==0 || dayOfWeek==1)endDate.setDate(today.getDate() + 11);
else if(dayOfWeek==6)endDate.setDate(today.getDate() + 12);
else endDate.setDate(today.getDate() + 13);
alert(endDate);
</script>
EDIT: this assumes "woking days" Mon, Tue, Wed, Thu, Fri

Categories