3

I am trying to get the date last week starting from now.

console.log(moment().day(-7).format('DD.MM.YYYY')); 

It should show me 06.01.2017 but I am getting always 01.01.2017. I don't understand why. According to API this should work.

// when Monday is the first day of the week

moment().weekday(-7); // last Monday

http://momentjs.com/docs/

Why do I get a wrong date and how to get the right date?

Community
  • 1
  • 1
Meteor Newbie
  • 646
  • 2
  • 10
  • 23

2 Answers2

6

You should use instead:

moment().subtract(7, 'days').format('DD.MM.YYYY')
José Antonio Postigo
  • 2,674
  • 24
  • 17
2

You can use

moment().add(Number, String);
moment().add(Duration);
moment().add(Object);

console.log(moment().add(-7, 'days').format('DD.MM.YYYY'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.js"></script>
Weedoze
  • 13,683
  • 1
  • 33
  • 63
  • 1
    Wow. You beat me with 45 seconds with an exactly the same answer. :) I removed mine. – smoksnes Jan 13 '17 at 13:36
  • Hehe.. Faster.. Thanks for deleting yours – Weedoze Jan 13 '17 at 13:38
  • Thank you for an ultra fast answer! Do you know why I can't use my example? I got it from the api too. PS: I can't click accept right now, because need to wait 10 Minutes according to stackoverflow message. – Meteor Newbie Jan 13 '17 at 13:42
  • @MeteorNewbie Your method can be used to set the day of the week, with Sunday as 0 and Saturday as 6. No problem, dont forget to accept it when you can – Weedoze Jan 13 '17 at 13:46