This question already has answers here:
How to add number of days to today's date? [duplicate]
(16 answers)
Closed 8 years ago.
I'm trying to figure out the proper way to get the javascript Date object for 8 days in the future from today.
Why? because I want to subtract a given Date from today's date and if it is less than or equal to 8 days in the future then do something.
So, I figured out how to parse the given date with new Date('2014-11-21T00:00:00.000-05:00') and get the date object from that and then I can subtract it from today like so: new Date('2014-12-25T00:00:00.000-05:00') - new Date() and then I have to compare that to the date object for 8 days in the future - today's date object.
Here's how I thought about doing the date object for the date 8 days in the future: I created a new Date with all measurements of time the same as today except I added 8 to the day. This works, except what if today is less than 8 days from the end of the month, then how do I get it to overflow into the next month. For example the 27th + 8 ( of october ) should be november 4th and not october's 35th.
var date = new Date();
date.setDate((date.getDate() + 8));
//date.setDate((date.getDate() - 8)); example of subtracting
This is how you add or subtract days from a date. This will automatically account for month to month role over.
Related
This question already has answers here:
How to add days to Date?
(56 answers)
Add 30 days to a Current date - JS [duplicate]
(3 answers)
Closed 1 year ago.
For example, I have a project where the vaccination date for AstraZeneca is today (August 15, 2021) and the next dosage should be the next 4 weeks? Also, I'm going to be using Firestore to store these dates which would be used for monthly reports.
Is this the correct way to store the current date?
console.log(new Date().toLocaleString().split(",")[0]);
Output:
8/15/2021
The expected output for the estimated 2nd dose of vaccination:
9/5/2021
Also, how can I code it in a way that the system will immediately computer the estimated date for the 2nd dose of vaccine? For sure, I know adding the days to the current date won't work since there are different months with either 30 or 31 days and also both the month and year won't get updated.
can you please try using days instead.
Date.prototype.addDays = function (days) {
const date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
};
// Add 7 Days
const date = new Date('2020-12-02');
console.log(date.addDays(7));
// 2020-12-09T00:00:00.000Z
This question already has answers here:
Moment JS - how to subtract 7 days from current date?
(6 answers)
Format date and Subtract days using Moment.js
(7 answers)
Closed 4 years ago.
I get the second week of 2018 like this using moment.js
var date =moment([2018]).isoWeek(2).startOf('isoWeek').format('DD-MM-YYYY');
I need to get the date previous to the this date; i.e. the date previous to
08-01-2018
Use moment substract method:
.subtract(1, 'days');
This question already has answers here:
Add one day to date in javascript
(11 answers)
Closed 4 years ago.
I have From-Date, To-Date, and Total-Days...if i have a From-Date and Total-Days then i have to calculate To-Date automatically
I Used The Following method to get the To-Date day
var FromDate = $("#FromDate").val();
var TotalDays = $("#txtDays").val();
FromDate.setDate(parseInt(FromDate.getDate()) + parseInt(NoOfDays));
var dd = FromDate.getDate()-1;
This Will Not Work For Day 1 of every month.....Since It Returns 0
How To Handle This Situation or help me to solve this in another way....Thanx In Advance
try this
var d = new Date('08-20-2018');
d.setDate(d.getDate() - 1);
alert(d.getDate());
Date object is smart enough to know what to do if you set any of the "components" (month, day, hour, minute, second, millisecond) outside of "normal" range - it does the maths for you
If you only need to know the previous date, you can always subtract the 24hours from the epoch time and then convert it back to date object and get the date like:
new Date(new Date('08-01-2018').getTime() - 24*3600000).getDate()
new Date('08-01-2018').getTime() will give you the epoch time of the date you want previous date from
24*3600000 is subtracting the 24 milliseconds from the epoch
After subtracting the value you get the previous day epoch and using the new Date() constructor you are getting the Date object again
On this new date object you can call getDate() method to get the correct result of 31.
I have given date to Date constructor in MM-DD-YYYY format
This question already has answers here:
Why does the month argument range from 0 to 11 in JavaScript's Date constructor?
(10 answers)
Closed 5 years ago.
I'm trying to get the day of the week from a manually set date.
var year = 2017;
var month = 10;
var d = new Date(year, month, 1);
var n = d.getDay();
console.log(n);
The above outputs 3, however, the correct day of the 1st of October is Sunday (ie. day 6 in JS terms). What am i doing wrong?
In javascript months start from 0. So your date is not really 1st of October, it is 1st of November, which is Wednesday.
In JavaScript, dates are denoted by the numbers 0-11 (like an array). So October would be 9. Your program is getting the 1st of November, which is a Wednesday.
This question already has answers here:
How to add number of days to today's date? [duplicate]
(16 answers)
Closed 8 years ago.
We are using AngularJS to set filters.
Basically we have a start date and end date and frequency. When the frequency is set to week, we simply want to add 1 week to the start date, when the frequency is set to daily, we want to add 1 day to the start date.
Basically something like :-
var date = new date();
date.addDays(2);
date.addMonths(2);
date.addYears(2);
I would consider using moment.js for all of your JS date related needs then you can do:
var date = moment();
date.add(2, 'days');
date.add(2, 'months');
date.add(2, 'years');
// or all of the above with:
date.add({years: 2, months: 2, days: 2});
And if you need a regular JS date object at the end, check out this post