Skip to content

Instantly share code, notes, and snippets.

@jedschneider
Created June 27, 2012 17:37
Show Gist options
  • Select an option

  • Save jedschneider/3005604 to your computer and use it in GitHub Desktop.

Select an option

Save jedschneider/3005604 to your computer and use it in GitHub Desktop.
advance a date object and deliver obis-common style
# advances date x days and formats as if it is coming from
# obis-common date validated attributes YYYY-MM-DD
# clones the passed in date such to avoid mutating the original.
advance_date: (date, days=0)->
assert date instanceof Date, "date needs to be a Date object"
assert typeof days == 'number', "days need to be an Integer"
d = new Date(date)
d.setDate(d.getDate() + days)
cast_by_two = (v)->
if v < 10 then "0#{v}" else v
"#{d.getFullYear()}-#{cast_by_two(d.getMonth() + 1)}-#{cast_by_two(d.getDate())}"
# random date between Jan 1, 2011 and the current day
# formatted like obis-common would return from an obis-common date attribute
# (not timestamp)
date: ->
cast_by_two = (v)->
if v < 10 then "0#{v}" else v
randomDate = (start, end)->
new Date start.getTime() + Math.random() * (end.getTime() - start.getTime())
d = randomDate(new Date(2011,0,1), new Date())
"#{d.getFullYear()}-#{cast_by_two(d.getMonth() + 1)}-#{cast_by_two(d.getDate())}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment