-4

I want to get dates 2 years prior to today in JavaScript not using any library.

Ms.KV
  • 214
  • 2
  • 10
  • 3
    Not exactly a duplicate, but exchanging `+` with `-` should not be a problem: [How to determine one year from now in Javascript](https://stackoverflow.com/questions/8609261/how-to-determine-one-year-from-now-in-javascript) – t.niese Aug 23 '18 at 08:17
  • 1
    This does not seem to be a quality question. Shows lack of research. – pixlboy Aug 23 '18 at 08:17
  • 1
    And regarding the comment `[...]I want all the days in between them.[...]`: [Javascript - get array of dates between 2 dates](https://stackoverflow.com/questions/4413590/javascript-get-array-of-dates-between-2-dates) – t.niese Aug 23 '18 at 08:19
  • 2
    Nearly all Date related questions have already been asked and answered multiple times on StackOverflow. – t.niese Aug 23 '18 at 08:20
  • If you guys know then please provide me the guidance not provide other already have questions and all. – Ms.KV Aug 23 '18 at 08:21
  • 2
    @Ms.KV I did. The answers in the first link provides the way to get the date two years back. And the second link has a function to get all dates between those two dates. So what else do you need. In the current from the question does not contain a problem that has not been already answered here, so it is a duplicate. If you think those answers won't work with your problem, then explain why. – t.niese Aug 23 '18 at 08:28
  • Thanks for your time @t.niese, Suppose let say 18 yrs. is minimum than it should display date 18yrs prior to today or default date can be todays date.This is i am working on and need help. – Ms.KV Aug 23 '18 at 08:56
  • @Ms.KV this still does not explain what part of your problem is not covered by those two answers. – t.niese Aug 23 '18 at 09:14

1 Answers1

0

Use date.getFullYear() on the Date() object, to get the current year and subtract that by 2

var date = new Date();
date.setFullYear( date.getFullYear() - 2 );
console.log(date);
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62