OLD | NEW |
(Empty) | |
| 1 humanized_time_span |
| 2 =========== |
| 3 |
| 4 humanized_time_span returns the difference between two Dates as a nice, human st
ring for use in the browser. |
| 5 |
| 6 |
| 7 usage |
| 8 ----- |
| 9 |
| 10 Include the script (it's native JS so doesn't require anything else) and you're
ready to go. |
| 11 |
| 12 ###Syntax |
| 13 humanized_time_span(date, ref_date, date_formats, time_units) |
| 14 |
| 15 Only date is required. |
| 16 |
| 17 ###Examples |
| 18 |
| 19 humanized_time_span("2010/09/10 10:00:00") => "3 days ago" (using now as a r
eference) |
| 20 |
| 21 humanized_time_span("2010/09/10 10:00:00", "2010/09/10 12:00:00") => "2 hour
s ago" |
| 22 |
| 23 ###Customisation |
| 24 |
| 25 Custom date formats can be set as follows: |
| 26 |
| 27 var custom_date_formats = { |
| 28 past: [ |
| 29 { ceiling: 60, text: "less than a minute ago" }, |
| 30 { ceiling: 86400, text: "$hours hours, $minutes minutes and $seconds sec
onds ago" }, |
| 31 { ceiling: null, text: "$years years ago" } |
| 32 ], |
| 33 future: [ |
| 34 { ceiling: 60, text: "in less than a minute" }, |
| 35 { ceiling: 86400, text: "in $hours hours, $minutes minutes and $seconds
seconds time" }, |
| 36 { ceiling: null, text: "in $years years" } |
| 37 ] |
| 38 } |
| 39 |
| 40 humanized_time_span("2010/09/10 10:00:00", "2010/09/10 10:00:05", custom_dat
e_formats) |
| 41 => "less than a minute ago" |
| 42 humanized_time_span("2010/09/10 10:00:00", "2010/09/10 17:01:25", custom_dat
e_formats) |
| 43 => "5 hours, 1 minute and 25 seconds ago" |
| 44 humanized_time_span("2010/09/10 10:00:00", "2012/09/10 10:00:00", custom_dat
e_formats) |
| 45 => "in 2 years" |
| 46 |
| 47 Here the date format's ceiling is in seconds. Formats are walked through until o
ne is reached where the ceiling is more than the difference between the two time
s or is null. |
| 48 |
| 49 Please note that the last date format provided must not have a ceiling. |
| 50 |
| 51 Variables in the format text should be prefixed with a $. eg. $xxx where xxx is
the name of the time unit. The text should always be written in the plural (eg.
"$years years ago") and the text will be automatically de-pluralized if, say in
this case, there is only 1 year. |
| 52 |
| 53 |
| 54 For those who live by different time rules, time_units can also be customised: |
| 55 |
| 56 var custom_date_formats = [ |
| 57 { ceiling: null, text: "$moggles moggles, $tocks tocks and $ticks ticks ag
o" }, |
| 58 ] |
| 59 |
| 60 var custom_time_units = [ |
| 61 [20, 'moggles'], |
| 62 [10, 'tocks'], |
| 63 [1, 'ticks'] |
| 64 ] |
| 65 time_ago("2010/01/01 00:00:00", "2010/01/01 00:00:53", custom_date_formats,
custom_time_units) |
| 66 => "2 moggles, 1 tock and 3 ticks ago" |
| 67 |
| 68 Custom date formats will have to be used when using custom time_units. |
| 69 |
| 70 the default time units are: |
| 71 |
| 72 [31556926, 'years'], |
| 73 [2629744, 'months'], |
| 74 [86400, 'days'], |
| 75 [3600, 'hours'], |
| 76 [60, 'minutes'], |
| 77 [1, 'seconds'] |
| 78 |
OLD | NEW |