Skip to content

Instantly share code, notes, and snippets.

@timrwood
Created October 12, 2011 02:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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
@caillou
Copy link

caillou commented Dec 2, 2011

Good specification.

We started to implement it, using the following function name: relativeDate().

You can follow progression here: https://github.com/caillou/moment/commits/relative-date

We'll finish it this weekend.

@timrwood
Copy link
Author

timrwood commented Dec 2, 2011

I cleaned up the spec a little bit, mostly conforming to the code you are working on.

One thing that should probably be added is relativeDate.else instead of hard-coding 'L' as the format for everything else. This way, if someone wants to change the way other dates are presented, they won't have to hack the library, they can just make their own lang file with their preferences.

@timrwood
Copy link
Author

timrwood commented Dec 2, 2011

Another thing along those lines, what about changing the formatting strings to be true formatting strings, instead of moment.fn.from like replacement patterns. Heres an example:

moment.relativeDate.tomorrow = 'To\\morrow \\at H:MMa';
moment().add('days', 1).closest(); // 'Tomorrow at 2:30pm

Those escape characters are pretty ugly though, so maybe I should also introduce an escape opening and closing bracket notation like this:

moment.relativeDate.tomorrow = '[Tomorrow at] H:MMa';

Doing this would also future proof against added formatting tokens.

@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