Add 5 days to the current date using JavaScript [duplicate] - javascript

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

Related

How do you code where the system will immediately compute the estimated next date? [duplicate]

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

Getting the date of the nth day after a given date [duplicate]

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!

Javascript For Display Previous Day From Given Date [duplicate]

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

Get timestamp of current hour in Javascript [duplicate]

This question already has answers here:
How do I get a timestamp in JavaScript?
(43 answers)
Closed 7 years ago.
Is it possible for me to get the UTC time of current hour in Javascript?
I've tried the following:
var mins = new Date().getMinutes();
var hours = new Date().getHours();
var hourStamp = new Date().setHours(hours, 0,0,0);
var dates = new Date(hourStamp);
Here dates gives me the Unix Timestamp of the current hour. Is there a better/faster way to do this?
EDIT:
For example: I want the Timestamp for the current hour. So if it's 00:16 am on 12/04/2015, I want the timestamp of 12/04/2015 00:00. And, my method works. My question pertains to if there's a better method of doing the same.
var UTCHour = new Date().getUTCHours();
Like this?
The one works well:
var UTCTime= (new Date()).toUTCString();

Handling different timezone to get data from db in javascript [duplicate]

This question already has answers here:
How to initialize a JavaScript Date to a particular time zone
(20 answers)
Closed 9 years ago.
I have pcap import and read facility in my project. I have handling timezone in my project. Imported pcap is not read/display if I changed my timezone from GMT to other Asia/Kolkota.
How can I handle timezone issue in javascript. I am storing the value into the database is 2013-05-07 00:04:23.435751-06.
it should be handled in all timezone. thanks in advance
I wish you are looking for this beautiful documentation. Thanks.
Update
try this:
var str= '2013-05-07 00:04:23.435751-06';
var n = str.slice( -3 );
var time = str.replace(" ","T");
time = time.slice(0, -3);
alert(calcTime(time, n));
function calcTime(time, offset) {
// create Date object for current location
d = new Date(Date.parse(time));
// convert to msec
// add local time zone offset
// get UTC time in msec
utc = d.getTime() + (d.getTimezoneOffset() * 60000);
// create new Date object for different city
// using supplied offset
nd = new Date(utc + (3600000*offset));
// return time as a string
return "The Time is " + nd.toLocaleString();
}

Categories