Abd ELL
Abd ELL

Reputation: 177

How to add one day to the current date

i want to add one day to the current date heres my code :

 var dt = $filter('date')(new Date(), "yyyy-MM-dd");
 alert(dt);
 dt.setDate(dt.getDate() + 1);
 alert("date plus one day is : "+dt);

thats gave me an error:

TypeError: dt.getDate is not a function

can anybody help please??

Upvotes: 4

Views: 21810

Answers (6)

Bhuwan
Bhuwan

Reputation: 177

Use Maths

var currenttimestamp = new Date().getTime();
var currentdate=new Date().getDate();
var onedayaftertimestamp=currenttimestamp+(86400000);//1 day=86400000 ms;
var ondayafterdate=new Date(onedayaftertimestamp).getDate();

console.log("Current date:"+currentdate+"\n");
console.log("Ondayafterdate:"+ondayafterdate);

Upvotes: 2

Endzeit
Endzeit

Reputation: 5474

You can't increase the date on the filter itself.

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

var dt = $filter('date')(tomorrow, "yyyy-MM-dd");
alert(dt);

Upvotes: 0

Nishant123
Nishant123

Reputation: 1966

Change

var dt = $filter('date')(new Date(), "yyyy-MM-dd");

To

var dt = new Date($filter('date')(new Date(), "yyyy-MM-dd"));

DEMO

Upvotes: 2

Sachila Ranawaka
Sachila Ranawaka

Reputation: 41387

Add one day to date before the filter.

 var sdate = new Date();
 sdate.setDate(sdate.getDate() + 1);
 var dt = $filter('date')(sdate, "yyyy-MM-dd");

angular.module("app",[])
.controller("ctrl",function($scope,$filter){
  
  var sdate = new Date();
  alert(sdate);
  sdate.setDate(sdate.getDate() + 1);
 var dt = $filter('date')(sdate, "yyyy-MM-dd"); 
 alert("date plus one day is : "+dt);
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
 
</div>

Upvotes: 6

Gaurav Ojha
Gaurav Ojha

Reputation: 1177

If you are open to using Moment.js you can use -

moment().add(1, 'days')

Upvotes: 2

Ashwin Golani
Ashwin Golani

Reputation: 411

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

Upvotes: 0

Related Questions