This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to add number of days to today's date?
I'm confused, I found so many different approaches, and which of them is actually correct?
So what is the correct way to add day(s) to a given Date?
date.setTime( date.getTime() + days * 86400000 );
Note : Use it if calculating / adding days from current date.
Be aware: this answer has issues (see comments)
var myDate = new Date();
myDate.setDate(myDate.getDate() + AddDaysHere);
It should be like
var newDate = new Date(date.setTime( date.getTime() + days * 86400000 ));
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:
Compare two dates with JavaScript
(43 answers)
Closed 2 years ago.
In database my date is stored in D/M/YYYY format eq:(7/1/2021)
var todayDate = (
new Date().getDate() +
"/" +
(new Date().getMonth() + 1) +
"/" +
new Date().getFullYear()
);
console.log(todayDate);
shows current date as 14/1/2021
when i compare to know which date is greater it shows false
console.log(todayDate > date);// date stored in database(7/1/2021)
can anyone please solve this and thanks in advance
you can't compare date strings using ">" operator. if its a string it's comparing them alphabetically.
Instead use date.getTime() to compare.
also, just use toLocalDateString() for date stings, instead of trying to re-invent the wheel yourself.
var todayDate = (new Date().toLocalDateString());
console.log(todayDate);
This question already has answers here:
How to add hours to a Date object?
(20 answers)
How do I subtract minutes from a date in JavaScript?
(9 answers)
want to substract 5.30 hours from a Date javascript
(1 answer)
Closed 2 years ago.
Could someone help me with this. I need to finish something for a schoolproject which we are displaying tonight and I have to subtract 2 hours from this code. Javascript is totally not my thing.
<span id="datetime"></span>
<script>
var dt = new Date();
document.getElementById("datetime").innerHTML = dt.toLocaleString();
</script>
I have no idea how to change this. Thanks in advance.
Regards,
Justin
Try
var dt = new Date();
dt.setHours(dt.getHours() - 2);
document.getElementById("datetime").innerHTML = dt.toLocaleString();
Have a look here https://codepen.io/vyspiansky/pen/abNBqrx
You can use moment.js.
moment(dt).subtract(2, 'hours').format();
This question already has answers here:
Incrementing a date in JavaScript
(19 answers)
How to add days to Date?
(56 answers)
Closed 2 years ago.
I am looking to find the date for, say, the 100th day after any given day. Is there a way to do that via javascript?
I was hoping it could be as simple as below, but it isn't quite that simple.
var givenDay = new Date(01/01/2020);
var hundredthDay = new Date(givenDay + 100);
console.log(hundredthDay)
You can try using .setDate() and .getDate() combination.
The setDate() method sets the day of the Date object relative to the beginning of the currently set month.
The getDate() method returns the day of the month for the specified date according to local time.
Adding the required days to .getDate() as the following:
const givenDay = new Date('01/01/2020');
console.log(givenDay);
const result = new Date(givenDay.setDate(givenDay.getDate() + 1 + 100));
console.log(result);
I hope this helps!
This question already has answers here:
How to add number of days to today's date? [duplicate]
(16 answers)
Closed 9 years ago.
I am trying to add 5 days to today's date using JavaScript. I am also trying to add this function into a button so that the result comes in an alert box when I click it, not as soon as I open the page.
I am new to JavaScript and trying very hard to learn.
Any help?
Thanks!
Declare a Date variable (it will be set to current date/time):
var dt = new Date();
Add 5 days:
dt.setDate(dt.getDate() + 5);
Put all the above in your click handler function. Something like:
document.getElementById('dateBtn').onclick = function () {
var dt = new Date();
dt.setDate(dt.getDate() + 5);
alert(dt);
};
FIDDLE EXAMPLE
var date = new Date(); // Get current Date
date.setDate(date.getDate()+5); // add 5 days to the current date
For more information see Date.
Might be overkill but moment.js could be useful to you.
JavaScript stores dates and times in milliseconds. So, add 5 days worth:
var fiveDaysLater = new Date(0,0,0,0,0,0,Date.now() + 5 * 24 * 60 * 60 * 1000);
Date.now() returns a value in milliseconds.
The date constructor (new Date) then makes a new Date object (hence the keyword new) using this value, plus the five days of milliseconds, and initializes the variable fiveDaysLater.
create a Date instance five days:
var fiveDaysLater = new Date( AnyYourDate.getTime() );
fiveDaysLater.setDate(fiveDaysLater.getDate() + 5);