-2

Here am trying to change date which am showing in div, here i have previous and next buttons & when i will click on Next button then the my current date should be got changed there i need a date of next day, previous date by clicking on previous button/div like bellow snapp
enter image description here

Here if i will cleck right side (>) text then the date should got changed as 9/9/2016,and when i will clicked on (<) text the date should got changed as 7/9/2016 & same thing should happen with month if we will reached to the eand of month means here suppose the date is 30/9/2016 and i will click on Next(< text) then the date should got displayed like 1/10/2016 and if am at 1/9/2016 and am clicking on previous(< text) then date should got displayed like 31/8/2016,can any one help me with this ?I tried something as like bellow but there is issue with date format

$("#prevDate").unbind('click').click(function () {
        var actualDate = new Date(document.getElementById("Date").innerHTML);        
        var newDate = new Date(actualDate.getFullYear(), actualDate.getMonth(), actualDate.getDate()-1);
        var converdate=newDate.toString().split(" ");
        var dayName=converdate[0];
        var month=converdate[1];
        var dayNumber=converdate[2];
        var year=converdate[3];
        var newdte= dayName+","+month+","+dayNumber+","+year;

        document.getElementById("Date").innerHTML=newdte;
        document.getElementById("result").innerHTML=newdte;      
    });

    $("#nextDate").unbind('click').click(function () {       
        var actualDate = new Date(document.getElementById("Date").innerHTML);        
        var newDate = new Date(actualDate.getFullYear(), actualDate.getMonth(), actualDate.getDate()+1);
        var converdate=newDate.toString().split(" ");
        var dayName=converdate[0];
        var month=converdate[1];
        var dayNumber=converdate[2];
        var year=converdate[3];
        var newdte= dayName+","+month+","+dayNumber+","+year;

        document.getElementById("Date").innerHTML=newdte;
        document.getElementById("result").innerHTML=newdte;

    });

here am passing date in dd/mm/yyyy format and it considered it as mm/dd/yyyy format this is the problem am facing

Madhav
  • 559
  • 2
  • 11
  • 34
  • 1
    I'm sure someone could help you with this. But they probably won't. You should show what you have tried and come to SO with any problems you're having. Don't just ask "Can someone please write some code for me?" – Bob Brinks Sep 08 '16 at 09:38
  • Please create a jsFiddle and show whatever you've tried, then if there is some issue we are always there to help you. – Indranil Mondal Sep 08 '16 at 09:40
  • Possible duplicate of [How to add/subtract dates with javascript?](http://stackoverflow.com/questions/10931288/how-to-add-subtract-dates-with-javascript) – Manish Sep 08 '16 at 09:40

1 Answers1

1

Resolved the issue

var months = ["","Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
    $("#prevDate").unbind('click').click(function () {

            var originalDate=document.getElementById("Date").innerHTML;
            var convertOriginalDate=originalDate.toString().split("/");
            var d=convertOriginalDate[0];
            var m=convertOriginalDate[1];
            var y=convertOriginalDate[2];
            var convertedOriginalDate= m+"/"+d+"/"+y;

            var actualDate = new Date(convertedOriginalDate);        
            var newDate = new Date(actualDate.getFullYear(), actualDate.getMonth(), actualDate.getDate()-1);
            var converdate=newDate.toString().split(" ");
            var dayName=converdate[0];
            var month=converdate[1];
            var dayNumber=converdate[2];
            var year=converdate[3];
            var newdte= dayNumber+"/"+months.indexOf(month)+"/"+year//dayName+","+month+","+dayNumber+","+year;

            document.getElementById("Date").innerHTML=newdte;
            document.getElementById("result").innerHTML=newdte;
        });

        $("#nextDate").unbind('click').click(function () {             
            var originalDate=document.getElementById("Date").innerHTML;
            var convertOriginalDate=originalDate.toString().split("/");
            var d=convertOriginalDate[0];
            var m=convertOriginalDate[1];
            var y=convertOriginalDate[2];
            var convertedOriginalDate= m+"/"+d+"/"+y;

            var actualDate = new Date(convertedOriginalDate);        
            var newDate = new Date(actualDate.getFullYear(), actualDate.getMonth(), actualDate.getDate()+1);
            var converdate=newDate.toString().split(" ");
            var dayName=converdate[0];
            var month=converdate[1];
            var dayNumber=converdate[2];
            var year=converdate[3];
            var newdte= dayNumber+"/"+months.indexOf(month)+"/"+year//dayName+","+month+","+dayNumber+","+year;

            document.getElementById("Date").innerHTML=newdte;
            document.getElementById("result").innerHTML=newdte;
        });
Madhav
  • 559
  • 2
  • 11
  • 34