Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a date object (six months prior) from another date object

How can I create a date object which is less than n number of months from another date object? I am looking for something like DateAdd().

Example:

var objCurrentDate = new Date();

Now using objCurrentDate, how can I create a Date object having a date which is six months older than today's date / objCurrentDate?

like image 788
Shyju Avatar asked Oct 30 '09 06:10

Shyju


People also ask

How do I only get date from date object?

ToString() − One more way to get the date from DateTime is using ToString() extension method. The advantage of using ToString() extension method is that we can specify the format of the date that we want to fetch. DateTime. Date − will also remove the time from the DateTime and provides us the Date only.


3 Answers

You can implement very easily an "addMonths" function:

function addMonths(date, months) {
  date.setMonth(date.getMonth() + months);
  return date;
}


addMonths(new Date(), -6); // six months before now
// Thu Apr 30 2009 01:22:46 GMT-0600 

addMonths(new Date(), -12); // a year before now
// Thu Oct 30 2008 01:20:22 GMT-0600

EDIT: As reported by @Brien, there were several problems with the above approach. It wasn't handling correctly the dates where, for example, the original day in the input date is higher than the number of days in the target month.

Another thing I disliked is that the function was mutating the input Date object.

Here's a better implementation handling the edge cases of the end of months and this one doesn't cause any side-effects in the input date supplied:

const getDaysInMonth = (year, month) => new Date(year, month, 0).getDate()

const addMonths = (input, months) => {
  const date = new Date(input)
  date.setDate(1)
  date.setMonth(date.getMonth() + months)
  date.setDate(Math.min(input.getDate(), getDaysInMonth(date.getFullYear(), date.getMonth()+1)))
  return date
}

console.log(addMonths(new Date('2020-01-31T00:00:00'), -6))
// "2019-07-31T06:00:00.000Z"

console.log(addMonths(new Date('2020-01-31T00:00:00'), 1))
// "2020-02-29T06:00:00.000Z"

console.log(addMonths(new Date('2020-05-31T00:00:00'), -6))
// "2019-11-30T06:00:00.000Z"

console.log(addMonths(new Date('2020-02-29T00:00:00'), -12))
// "2019-02-28T06:00:00.000Z"
like image 192
Christian C. Salvadó Avatar answered Oct 21 '22 19:10

Christian C. Salvadó


var oldDate:Date = new Date();
/*
 Check and adjust the date -
 At the least, make sure that the getDate() returns a 
 valid date for the calculated month and year.
 If it's not valid, change the date as per your needs.
 You might want to reset it to 1st day of the month/last day of the month
 or change the month and set it to 1st day of next month or whatever.
*/
if(oldDate.getMonth() < n)
    oldDate.setFullYear(oldDate.getFullYear() - 1);
oldDate.setMonth((oldDate.getMonth() + n) % 12);
like image 22
Amarghosh Avatar answered Oct 21 '22 19:10

Amarghosh


Create date object and pass the value of n, where n is number(add/sub) of month.

  var dateObj = new Date();
  var requiredDate= dateObj.setMonth(dateObj.getMonth() - n);
like image 23
Ankit Avatar answered Oct 21 '22 17:10

Ankit