Reputation: 23
I want to show the difference between the date I enter and the current one. For this I am doing that:
$("#news").append(
"<h5>"+moment(["2018-03-27 12:18:41"]).fromNow()+"</h5>"
);
However, momentjs prints: '4 months ago' and it is yesterday date
Maybe you know why?
Upvotes: 0
Views: 1213
Reputation: 23859
The date you mentioned in the question is a date of March, which is definitely not yesterday. Moreover, you need to pass the date string itself to the moment()
constructor; don't wrap it in an array:
moment("2018-03-27 12:18:41").fromNow()
MomentJS uses the arrayed format for special cases, where you need to pass year, month, and day as separate integers. Additionally, as @charlietfl mentioned, passing the date format will ensure correct date parsing:
moment("2018-03-27 12:18:41", "YYYY-MM-DD HH:mm:ss").fromNow()
Upvotes: 3