最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - angularjs - calculate a date plus one day - Stack Overflow

programmeradmin1浏览0评论

I need to get the date one day after another date. I do :

$scope.date2.setDate($scope.date1.getDate()+1);

if   
$scope.date1 = 2015-11-27  

then 
$scope.date2 = 2015-11-28

It s ok,
but when 

$scope.date1 = 2015-12-02

 then 
 $scope.date2 = 2015-11-28 (ie tomorrow)

I don't understand why...

If anyone knows..

I need to get the date one day after another date. I do :

$scope.date2.setDate($scope.date1.getDate()+1);

if   
$scope.date1 = 2015-11-27  

then 
$scope.date2 = 2015-11-28

It s ok,
but when 

$scope.date1 = 2015-12-02

 then 
 $scope.date2 = 2015-11-28 (ie tomorrow)

I don't understand why...

If anyone knows..

Share Improve this question edited Nov 27, 2015 at 10:42 ozil 7,1259 gold badges36 silver badges61 bronze badges asked Nov 27, 2015 at 10:23 user1260928user1260928 3,43910 gold badges67 silver badges111 bronze badges 8
  • 2 why don't you try javascript Date function and add days to it addDays(1) rather doing +1 – super cool Commented Nov 27, 2015 at 10:25
  • 1 also i will suggest you to create JsFiddle or Stack's inbuilt snippet creator, so people can see the actual problem and provide better solution – Virendra Yadav Commented Nov 27, 2015 at 10:27
  • I don't see any addDays in Javascript – user1260928 Commented Nov 27, 2015 at 10:28
  • you can use moment.js for date calculations – Kamal Kumar Commented Nov 27, 2015 at 10:29
  • 1 getDate() gets the date for today. If you increment it, it will be tomorrow. Everything works as it is supposed. Your logic inside the code is wrong. – cezar Commented Nov 27, 2015 at 10:31
 |  Show 3 more ments

2 Answers 2

Reset to default 3

try this instead efficient simple pure JS

var todayDate = new Date();    
console.log(new Date().setDate(todayDate.getDate()+1));

so you will have that same Date type object and hence you don't need to go with moment.js

Use moment.js for this momentjs

var startdate = "2015-12-02";
var new_date = moment(startdate, "YYYY-MM-DD").add('days', 1);
var day = new_date.format('DD');
var month = new_date.format('MM');
var year = new_date.format('YYYY');
alert(new_date);
alert(day + '.' + month + '.' + year);
发布评论

评论列表(0)

  1. 暂无评论