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

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!

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

How to get the whole date time in a single line not separated [duplicate]

This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 4 years ago.
I need to get the date time in years,month,day,hours,minute,seconds,milliseconds in this format
201802281007475001
right now i managed to return this:
2018418112252159
This is my code:
var date = new Date();
blockid = JSON.stringify(date.getFullYear()) + JSON.stringify(date.getMonth()+1) + JSON.stringify(date.getDate()) + JSON.stringify(date.getHours()) + JSON.stringify(date.getMinutes()) + JSON.stringify(date.getSeconds()) + JSON.stringify(date.getMilliseconds())
I also need to put a zero in front of the month and the day in case it is minor than 10.
One simple way to this is to call toISOString on the date object and replace the non-required characters with blank:
var date = new Date();
console.log(date.toISOString().replace(/\D/g, ''));

Date manipulation in React js [duplicate]

This question already has answers here:
getMonth in javascript gives previous month
(6 answers)
Closed 5 years ago.
I am trying like this:
var db = new Date('2017-12-03T13:32:45.000Z');
console.log(db.getMonth());
to get the month from given date but it gives the previous month. Any suggestion...?
This is the expected behaviour, it's zero-based so January = 0, February = 1 etc. If you want the current month, you need:
let db = new Date('2017-12-03T13:32:45.000Z');
console.log(db.getMonth() + 1);
Reference: MDN

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

AngularJS how to add days / months / years to a date [duplicate]

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

Categories