Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Allow sugarjs to parse multiple friendly dates in the format 1 month 1 day ago
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
You can’t perform that action at this time.