1

I have to calculate three date form tommorow. for example todayes date is 1-12-2016 so next three date will be 2-12-2016 , 3-12-2016 , 4-12-2016

i am able to do it in this situation but my problem is if my date is 30 or 31 i am get the date as 32, 33, 34 but i have to get 1, 2, 3

here is my code

 this.today = new Date();
    this.tommorowDate =this.today.setDate(this.today.getDate() + 1)
    console.log(this.tommorowDate)
    this.dd = this.today.getDate();
    this.mm = this.today.getMonth()+1;
    this.yyyy = this.today.getFullYear();
    this.currentDate = this.dd+'/'+this.mm+'/'+this.yyyy;

 for(var i = 1; i <= 3; i++){
       var incrementdate = this.dd+i;
        this.datepickerArray.push(this.yyyy+'-'+this.mm+'-'+incrementdate)

    }

my this.datepickerArray as [31-1-2017,32-1-2017,33-1-2017].

but i have to get like this [31-1-2017,1-2-2017,2-2-2017].

I am able to get tomorrows date but using that how can i get my array update with next months.

Poul Kruijt
  • 69,713
  • 12
  • 145
  • 149
Mohan Gopi
  • 7,606
  • 17
  • 66
  • 117

4 Answers4

1

You can use Moment js as well it's a great library for Parse, validate, manipulate, and display dates in JavaScript.. For current scenario please find below snippet using moment js

startdate = new Date();
var new_date = moment(startdate).add('days', 1);
DisplayDate(new_date);
new_date = moment(startdate).add('days', 2);
DisplayDate(new_date);
new_date = moment(startdate).add('days', 3);
DisplayDate(new_date);

function DisplayDate(param)
{
var day = param.format('DD');
var month = param.format('MM');
var year = param.format('YYYY');
alert(day + '.' + month + '.' + year);
}
<script src="http://davis-design.de/marktadresse/layout/js/moment.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Curiousdev
  • 5,668
  • 3
  • 24
  • 38
  • why using moment.js when the question is not tagged with moment? – brk Jan 30 '17 at 10:46
  • @user2181397 you are absolutely right but i don't think there is any thumb rule like if there is no tag in questions we can't put answer related to that and moment is very powerful library over manipulation on dates so this is just for future references – Curiousdev Jan 30 '17 at 10:55
1

Try this

<!DOCTYPE html>
<html>

<head>
    <script>
        var date = new Date();
        console.log("Todays date" + date);
        date = new Date(new Date().setDate(new Date().getDate() + 1));
        console.log("Tommorows date" + date);
        date = new Date(new Date().setDate(new Date().getDate() + 2));
        console.log("Day after tommorows date" + date);
        date = new Date(new Date().setDate(new Date().getDate() - 1));
        console.log("Yesterdays date" + date);
    </script>
</head>

<body>
</body>

</html>
Nitheesh
  • 19,238
  • 3
  • 22
  • 49
0

This is not the best way to do , but hope it will help you

var tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);


var dayAfterTomm = new Date();
dayAfterTomm.setDate(dayAfterTomm.getDate() + 2);

var afterOverMorrow = new Date();
afterOverMorrow.setDate(afterOverMorrow.getDate() + 3);

document.write('<p>'+tomorrow+'</p>')
document.write('<p>'+dayAfterTomm+'</p>')
document.write('<p>'+afterOverMorrow+'</p>')

Instead of creating the new variable you can pass the value like 1,2,3 through a loop to get the next dates

DEMO

brk
  • 48,835
  • 10
  • 56
  • 78
0

try this https://jsfiddle.net/z5a07w7m/

this.today = new Date();
this.datepickerArray = [];

 for(var i = 1; i <= 3; i++){
this.today.setDate(this.today.getDate() + 1);
this.datepickerArray.push(this.today.getFullYear()+'-'+(this.today.getMonth()+1)+'-'+this.today.getDate())

    }

    console.log(this.datepickerArray);
Aman B
  • 2,276
  • 1
  • 18
  • 26