Created
June 27, 2012 17:37
-
-
Save jedschneider/3005604 to your computer and use it in GitHub Desktop.
advance a date object and deliver obis-common style
This file contains hidden or 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
| # 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