Last active
December 25, 2015 21:09
-
-
Save gwhitelaw/7040178 to your computer and use it in GitHub Desktop.
Allow sugarjs to parse multiple friendly dates in the format 1 month 1 day ago
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sugar' | |
{ compact } = require 'underscore' | |
parseFriendlyDate = (friendlyDate) -> | |
date = new Date().getTime() | |
dateOffset = date | |
dateSections = compact((friendlyDate.trim()).split(/(\d+\s\w+)\s/)) | |
dateEnding = dateSections.splice(-1) | |
dateSections.forEach((section) -> dateOffset -= (date - Date.create("#{section} #{dateEnding}").getTime())) | |
new Date(dateOffset) | |
console.log(parseFriendlyDate('1 day ago')) | |
console.log(parseFriendlyDate('1 day 1 month ago')) | |
console.log(parseFriendlyDate('1 year 1 month 1 day ago')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment