Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Java Script date calculation not working correctly

I am calculating date in JavaScript based on date, frequency and period. When I am calculating date for one month then It is working perfectly but when I am trying to calculate date three monthly or six monthly then below code is not working fine.

CurrentDate value is 01-Feb-2023 and value of X is 3

When I am giving value of x = 1 then below code is giving 1-Mar-2023 which is correct but when I am giving value of x = 3 then it is giving 1-Feb-2024 instead of 1-May-2023.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

var x = 3;
CurrentDate = "01-Feb-2023"
var d = new Date(CurrentDate);
console.log(d);
d.setMonth(d.getMonth() + x);
console.log(d);
var lastDate = moment(d.toISOString().slice(0, 10)).format("DD-MMM-YYYY");
console.log(lastDate)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>

>Solution :

You have a timezone issue

Also you are wasting moment by not using it to add the month

Note that this date format makes moment complain: '01-Feb-2023'

var x = 3;
var currentDate = moment('01-Feb-2023'); // not a great value for moment
var lastDate = moment(currentDate).add(x, 'M').format("DD-MMM-YYYY");

console.log(lastDate)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>

Better: Strict mode is set by passing true as the third parameter to the moment function.

moment('02/01/2023', 'MM/DD/YYYY', true);

var x = 3;
var currentDate = moment('02/01/2023', 'MM/DD/YYYY', true); // better
var lastDate = moment(currentDate).add(x, 'M').format("DD-MMM-YYYY");

console.log(lastDate)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>

Without moment

const dateTimeFormat = new Intl.DateTimeFormat('en-US', { year: 'numeric', month: 'short', day: 'numeric' });

const addToDate = (dateString, offset) => {
  const currentDate = new Date(dateString); // using the Date.parse. Moment warned it would use this
  currentDate.setHours(15, 0, 0, 0) // normalise
  currentDate.setMonth(currentDate.getMonth() + offset); // add the month
  const [month, sp1, day, comma, year] = dateTimeFormat.formatToParts(currentDate);
  return `${day.value}-${month.value}-${year.value}`; // There is no DD-MMM-YYYY in INTL so we make our own
}

let x = 3;
const lastDate = addToDate("01-Feb-2023", x);
console.log("01-Feb-2023 plus",x,"months:",lastDate)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading