{"id":15643346,"url":"https://github.com/tdd/moments-away","last_synced_at":"2025-07-29T12:32:24.132Z","repository":{"id":11650358,"uuid":"14157167","full_name":"tdd/moments-away","owner":"tdd","description":"Moment.js durations made easier, ActiveSupport-style.","archived":false,"fork":false,"pushed_at":"2013-11-08T19:17:18.000Z","size":824,"stargazers_count":58,"open_issues_count":0,"forks_count":2,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-07-03T07:51:29.830Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://tdd.github.io/moments-away/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tdd.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-11-05T23:21:25.000Z","updated_at":"2018-10-04T09:53:27.000Z","dependencies_parsed_at":"2022-09-11T10:41:21.784Z","dependency_job_id":null,"html_url":"https://github.com/tdd/moments-away","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/tdd/moments-away","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tdd%2Fmoments-away","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tdd%2Fmoments-away/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tdd%2Fmoments-away/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tdd%2Fmoments-away/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tdd","download_url":"https://codeload.github.com/tdd/moments-away/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tdd%2Fmoments-away/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267686342,"owners_count":24127716,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-29T02:00:12.549Z","response_time":2574,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-03T12:00:20.105Z","updated_at":"2025-07-29T12:32:24.097Z","avatar_url":"https://github.com/tdd.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"moments-away\n============\n\n[![Build Status](https://travis-ci.org/tdd/moments-away.png?branch=master)](https://travis-ci.org/tdd/moments-away)\n[![Coverage Status](https://coveralls.io/repos/tdd/moments-away/badge.png?branch=master)](https://coveralls.io/r/tdd/moments-away?branch=master)\n[![Code Climate](https://codeclimate.com/github/tdd/moments-away.png)](https://codeclimate.com/github/tdd/moments-away)\n[![Dependency Status](https://gemnasium.com/tdd/moments-away.png)](https://gemnasium.com/tdd/moments-away)\n\nMoment.js durations made easier, ActiveSupport-style.\n\nWut?\n====\n\n[Moment.js](http://momentjs.com/) is a great JS library for manipulating dates and times in just about every way possible.  If you do this kind of stuff, you should absolutely use Moment.\n\nIt provides a system of *durations* (e.g. “1 month”) that allow reliable offsets between dates.  For instance, a month is a contextual notion: this could end up being anywhere from 28 to 31 days, not to mention the leap second thing we sometimes get at the end of the year.\n\nHowever, I favor another reading style when manipulating such things, mostly because I'm used to the ActiveSupport way of doing things (in Ruby/Rails).  In Moment, you'd so something like :\n\n```js\n// The Moment way\n\n// Based on a specific date:\nvar d = new Date(2013, 10, 6);\nmoment(d).add(3, 'months').toDate() // \"3 months after d\"\nmoment(d).subtract(1, 'year').toDate() // \"1 year before d\"\n\n// Based on now:\nmoment().add(1, 'month').toDate()  // \"1 month from now\"\nmoment().subtract(2, 'weeks').toDate()  // \"2 weeks ago\"\n```\n\nThis is functionally great, but I like how ActiveSupport reads better, so I created this little library that builds on Moment (which you should have available, then) and adds some stuff.  Mirroring that exact Ruby syntax in JS is fairly convoluted and bulky memory-wise, so I went with something slightly different, but here it is.\n\n```js\n// The Moments-Away way\n\n// Based on a specific date:\nvar d = new Date(2013, 10, 6);\n'3 months'.after(d)\n'1 year'.before(d)\n\n// Based on now:\n'1 month'.fromNow()\n'2 weeks'.ago()\n\n// Based on Numbers instead of strings:\n(3).months().after(d)\n(1).year().before(d)\n\nvar om = 1, ow = 2;\nom.months().fromNow()\now.weeks().ago()\n```\n\nInstallation\n============\n\nIf you're using this in Node or installing through npm, just go with it:\n\n```\nnpm install --save moments-away\n```\n\n[Bower](http://bower.io/) more your cup of tea?  Here you go:\n\n```\nbower install moments-away\n```\n\nPerhaps you favor TJ's [Component](http://component.io/)?  Not a problem:\n\n```\ncomponent install tdd/moments-away\n```\n\nOtherwise you can just pick up the [library source file](https://raw.github.com/tdd/moments-away/master/lib/index.js) (only 1.7KB uncompressed).\n\nZOMG! :scream: You're extending native prototypes!\n==================================================\n\nYes, I am.  This is necessary to achieve the level of expressiveness I was shooting for, and this does work across browsers.\n\nIf that looks too irksome for you, feel free to fork this and provide an alternative that uses, say, a global wrapper function over `String`s or `Number`s that returns the appropriate objects.\n\nThe API\n=======\n\n`String`- or `Number`-based\n---------------------------\n\nDepending on what you're working with, it'll be smoother to use either a `String`- or `Number`-based API.\n\nYou’ll usually work with strings for one-off duration specifications.  Loading the library automagically equips `String` with four new methods:\n\n* `before(date)` returns a `Date` the precedes the `date` argument by the duration defined in the string.\n* `after(date)` returns a `Date` the follows the `date` argument by the duration defined in the string.\n* `ago()` returns a past `Date` with a distance defined in the string.\n* `fromNow()` returns a future `Date` with a distance defined in the string.\n\nThe string should start with a number, followed possibly by whitespace, followed by one of the duration units recognized by `moment.duration` (this is every `Date` sub-unit from `milliseconds` to `years`, singular or plural, along with a few abbreviated forms, such as `ms`, `s` or `y`, for instance).  Surrounding whitespace is ignored.\n\nIf you have an existing number (variable, argument, etc.) that defines your base duration, using the `Number`-based API will feel smoother:\n\n```js\nfunction setAuthCookie(sessionId) {\n  // var expiry = (AUTH_EXPIRY_MONTHS + ' months').fromNow(); // Ugh\n  var expiry = AUTH_EXPIRY_MONTHS.months().fromNow();         // Better\n  document.cookie = \"authId=\" + sessionId + \"; expires=\" + expiry;\n}\n```\n\nLoading the library automagically equips `Number` with new methods for every duration unit supported by `moment.duration`, using the long forms, singular or plural:\n\n* `milliseconds()`, `millisecond()`\n* …\n* `years()`, `year()`\n\nThe abbreviated forms are not used: less readable, more prone to naming conflicts.\n\nThese do not return numbers or strings, but actual Moment durations (which you could then use in regular Moment API calls), which remain contextual and will work at any later time, including on long-lived web pages.\n\nIn order to make the API homogeneous, we also extend Moment durations with the same four methods as on `String`: `before(date)`, `after(date)`, `ago()` and `fromNow()`.\n\nContributing\n============\n\nYou can report any issues or feature requests [on GitHub](https://github.com/tdd/moments-away/issues).\n\nFeel free to fork the repo, contribute and submit pull requests.  These must always come with updated tests.\n\nLicence\n=======\n\nThis work is MIT licence.  Read the licence [here](https://github.com/tdd/moments-away/blob/master/LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftdd%2Fmoments-away","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftdd%2Fmoments-away","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftdd%2Fmoments-away/lists"}