Skip to content

Instantly share code, notes, and snippets.

@jforaker
Created August 13, 2014 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jforaker/d6f38632926055dcf631 to your computer and use it in GitHub Desktop.
Save jforaker/d6f38632926055dcf631 to your computer and use it in GitHub Desktop.
Parse due dates with moment.js
dues: function (expiresdate){
var now = moment(),
due = moment(expiresdate).format("YYYY-MM-DD"),
todayMoment = moment().format("YYYY-MM-DD"),
dueFormatted = moment(expiresdate).format("MMMM Do YYYY"),
tmrw, yest,
dueInfo = {
color: '',
text: ''
};
tmrw = moment(now.add('days', 1)).format("MMMM Do YYYY");
yest = moment(now.subtract('days', 2)).format("MMMM Do YYYY");
if(todayMoment == due){
dueInfo.text = 'Due today';
dueInfo.color = 'blue';
}else{
if(moment(due).isBefore(todayMoment)){
dueInfo.color = 'red';
if(dueFormatted === yest){
dueInfo.text = "Due yesterday" + " " + dueFormatted;
}else{
dueInfo.text = "Due " + dueFormatted;
}
}else{
dueInfo.color = 'blue';
if(dueFormatted == tmrw){
dueInfo.text = "Due tomorrow: " + dueFormatted;
}else{
dueInfo.text = "Due on " + dueFormatted;
}
}
}
return dueInfo;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment