Lekhraj Sharma
Lekhraj Sharma

Reputation: 31

How to calculated 3 months date based on given date JavaScript

function change()    {
    var date    =     new Date();
    var dd = date.getDate("startDate");
    var mm = date.getMonth("startDate")+1;
    var yyyy = date.getFullYear("startDate");
    var myDate=yyyy+'-'+mm+'-'+dd;
    
    var tmps = new Date(myDate).getTime();
    var newdate = (90*3600*24*1000)+tmps;
    var t=new Date(newdate);
    var dd= t.getDate();
    var mm=t.getMonth()+1;
    var yyyy=t.getFullYear();
    var tdate=yyyy+'-'+mm+'-'+dd;
    // alert(document.getElementById("endDate").value=tdate);
    return tdate;
}
document.write(change());

My problem is that the tdate value actually gives a YYYY-M-DD value, instead of a YYYY-DD-MM.

Upvotes: 1

Views: 99

Answers (3)

Mamdouh Saeed
Mamdouh Saeed

Reputation: 2324

function change(myDate){
    var tmps = new Date(myDate).getTime();
    var newdate = (90*3600*24*1000)+tmps;
    var t=new Date(newdate);
    var dd= t.getDate() <10 ? "0"+t.getDate() : t.getDate();
    var mm=t.getMonth()+1 <10 ? "0"+(+t.getMonth()+1) : t.getMonth()+1;
    var yyyy=t.getFullYear();
    var tdate=yyyy+'-'+dd+'-'+mm; 
    alert(tdate);
document.getElementById("tdate").value = tdate;
}
change("2016-7-1"); // ==> 2016-29-09
//change(new Date("2016-8-1"));// ==> 2016-29-30
//change(new Date("2016/08/01"));// ==> 2016-29-10
//change(new Date(2016,(8-1),1));// ==> 2016-29-10
<input type='text' id="tdate" value=''/>

Upvotes: 0

RobG
RobG

Reputation: 147413

In your code you have:

var tdate = yyyy + '-' + mm + '-' + dd;

so you should not be surprised to get yyyy-mm-dd. ;-) Change it to:

var tdate = yyyy + '-' + dd + '-' + mm;

If you wish to get a date that is 3 months from now, you need to consider what happens when you add 3 months to say 31 December. In older versions of ECMAScript, 30 November 2015 + 3 months gave 1 March 2016. Newer versions will not allow the date to overrun the month, so the result will be 29 February.

E.g. in Firefox 49:

var d = new Date(2015,10,30);
d.setMonth(d.getMonth()+3);
console.log(d); // 1 Mar 2016 or 29 Feb 2016 depending on implementation

Since you can't tell which version the host is compliant with, you will need to manually test whether the date has rolled over or not:

function addMonths(date, months) {
  var startMonth = date.getMonth();
  date.setMonth(date.getMonth() + months);
  // If the new month isn't correct, set to last day of previous month
  if ((startMonth + +months)%12 != date.getMonth()) {
    date.setDate(0);
  }
  return date
}

var d = new Date(2015,10,31);
addMonths(d, 3);
console.log(d)

In your code, the Date get methods do not take any arguments, so any that are supplied are ignored, e.g.

date.getDate("startDate");

should be

date.getDate();

and so on. Also:

var date = new Date();

will create a new Date based on the host timezone offset, however when you then create a string based on its YYYY-MM-DD values and do:

 var myDate=yyyy+'-'+mm+'-'+dd;

It is treated as a UTC date. For users west of Greenwich, the date will appear to be one day earlier.

If you wish to copy a date, simply provide it as the sole argument to the Date constructor:

var tmps = new Date(date);

Months should be added as indicated above. There are not exactly 90 days in every 3 month period, and there are not exactly 24 hours in every day in places where daylight saving is observed. So the combination of factors means that from time to time your algorithm for adding 3 months will seem to be randomly incorrect.

Lastly, a format of YYYY-DD-MM is certain to confuse the vast majority of people read it. Dates are largely misunderstood, so the best strategy is to write them in an unambiguous form with the month name used instead of the number. Or if you wish to use an only numeric form, the stick to well known standards like ISO 8601 YYYY-MM-DD.

Upvotes: 0

ThisClark
ThisClark

Reputation: 14823

There's a time library for JavaScript called Moment. Your problem is trivial with this simple syntax:

var m = moment().add(3,'months').format('YYYY-DD-MM');
alert('Three months from now in YYYY-DD-MM format is:\n' + m);
<script src="http://momentjs.com/downloads/moment.min.js"></script>

The moment() constructor creates the current instant of time and has methods to manipulate and format it without all the details of time management algorithms that require debugging and testing.

Upvotes: 1

Related Questions