How to get a date that now date minus condition value? [duplicate] - javascript

This question already has answers here:
Find out the previous year date from current date in javascript
(12 answers)
Add A Year To Today's Date
(9 answers)
Closed 1 year ago.
I have a date picker, i want to user cannot pick a date but date now minus this date less than 18 year(i don't want the user less than 18 years old).but i don't know how to do that.
i described my idea by this code below.
const nowDate = new Date()
const valueCondition = 18
const dateWishes = nowDate - valueCondition
how can i do that?
have a nice day, everyone!

Use getFullYear()
const nowDate = new Date();
const valueCondition = 18;
const dateWishes = nowDate.getFullYear() - valueCondition; // 2021 - 18
console.log(dateWishes); // 2003

Related

How to deal with the invalid date in javascript? [duplicate]

This question already has answers here:
What are valid Date Time Strings in JavaScript?
(2 answers)
Why does Date.parse give incorrect results?
(11 answers)
Closed 3 months ago.
I am trying to convert string in to date object in but I am getting two different result for two date. Help me to solve this issue and let me know is there any more convenient way to doing this.
<script>
let date1 = '02-11-2022 17:32';
let dateObj1 = new Date(date1);
console.log(dateObj1); // Fri Feb 11 2022 17:32:00 GMT+0530 (India Standard Time)
let date2 = "30-11-2022 17:32";
let dateObj2 = new Date(date2);
console.log(dateObj2); // Invalid Date
</script>

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

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

Output age from year vaule and then increase by 1 every year javascript [duplicate]

This question already has answers here:
Calculate age given the birth date in the format YYYYMMDD
(45 answers)
Closed 8 years ago.
I have a custom field vaule in WordPress. This field is for the year of birth,
for example 1980. I would like to convert this year to display the age of (in the example) 33.
Furthermore, I would like to increase this number once every year. So next year, the age till be 34.
Is this possible to do with javascript?
Best regards,
Fredrik
This way:
function getAge(yearOfBirth) {
return (new Date()).getFullYear() - yearOfBirth;
}
So just get the current date with JS, and substract the base year from it:
var base_year = 1980; //get this from DB
var today = new Date();
var current_year = today.getFullYear(); //2014
alert (current_year - base_year); // = 34

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