Skip to content

Instantly share code, notes, and snippets.

@timrwood
Created October 12, 2011 02:41
Show Gist options
  • Save timrwood/1280098 to your computer and use it in GitHub Desktop.
Save timrwood/1280098 to your computer and use it in GitHub Desktop.
moment.closest requirements

Functionality

Show the date with the smallest order of magnitude of difference from now.

Names

closest

calendar

magnitude

relativeDate

The same day

moment.relativeDate.today = "Today at %t";
moment().closest() // Today at 1pm
moment().add('hours', 1).closest() // Today at 2pm

The day after

moment.relativeDate.tomorrow = "Tomorrow at %t";
moment().subtract('days', 1).closest() // Tomorrow at 1pm

The day before

moment.relativeDate.yesterday = "Yesterday at %t";
moment().subtract('days', 1).closest() // Yesterday at 1pm

Up to 7 days before/after

moment.relativeDate.last = "Last %d at %t";
moment.relativeDate.yesterday = "Next %d at %t";
moment().subtract('days', 7).closest() // Last Sunday at 1pm
moment().subtract('days', 6).closest() // Last Saturday at 1pm
moment().add('days', 6).closest() // Next Saturday at 1pm
moment().add('days', 7).closest() // Next Sunday at 1pm

Everything else

moment.relativeDate.else = "L"; // a formatting string
moment().add('weeks', 2).closest() // 7/10/1986
@timrwood
Copy link
Author

timrwood commented Dec 2, 2011

Also, I just saw this a couple days ago, maybe we could use something like this?

https://github.com/xaviershay/kronic/blob/e049961ec678f7f469a2de14a3ce4a03bd762515/lib/js/kronic.js#L134

@caillou
Copy link

caillou commented Dec 8, 2011

@timwood: Thanks for the link. Unfortunatly this code does not really work. If you choose to display s.th. that is tomorrow, yet in less than 24h, it'll display "Today".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment