????6. }??

????7.

????8. "test 8 day difference should result in '1 week ago'": function () {

????9. assertEquals("1 week ago"?? dateUtil.differenceInWords(this.date8DaysAgo));

????10. }??

????11.

????12. "test should display difference with days?? hours?? minutes and seconds": function () {

????13. assertEquals("3 days?? 2 hours?? 16 minutes and 10 seconds ago"??

????14. dateUtil.differenceInWords(this.date3DaysAgo));

????15. }

????16. });

??????????????????????????DOM???????????????Ч?????????????????????????????????????jQuery???????????????????????滻??

????????????????????

??????????????????????????????????????????????“????????????????????д??Щ???????????????????????????????”??????

???????????????????????????????????????????????????????????в???????????????????????????????????????????????????????????????Twitter?????????????棬????????????????????differenceInWords ???????????????DOM????jQuery?????????·??(Now?? if we later decide to implement e.g. aTwitter feed on our pages?? we can use the differenceInWords functiondirectly with the timestamp rather than going the clumsy route via a DOMelement and the jQuery plugin.)??????????????????????????????????????п????????????????????????????????????????п?????????????????С?????????????—???????????—????????????????ζ??????????????

??????д?????????????????

?????????????е???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????д?????

????????????????(TDD)???????????????????ЩС??????????????????????????????????????????????????????????д???????TDD????????????????????????????????

??????????????????????????????jQuery?????????????????????????????????????????????????TDD????????????????????????????????????????????????

????[javascript] view plaincopy1. "test should accept date to compare to": function () {

????2. var compareTo = new Date(2010?? 1?? 3);

????3. var date = new Date(compareTo.getTime() - 24 * 60 * 60 * 1000);

????4.

????5. assertEquals("24 hours ago"?? dateUtil.differenceInWords(date?? compareTo));

????6. }

??????????????÷???????????????????????????????????????????????24С?????????????????"24 hours ago"?????иò???????????????????????????ò??????????????ò???ú??????????????????????????и?亯??????е??????????????????????????

????[javascript] view plaincopy1. dateUtil.differenceInWords = function (date?? compareTo) {

????2. compareTo = compareTo || new Date();

????3. var diff = compareTo - date;

????4.

????5. // ...

????6. };

???????е???????????????μ?????????????????????

???????????????????????????????????????????????????????????????????????????????????????????

????[javascript] view plaincopy1. "test should humanize differences into the future": function () {

????2. var compareTo = new Date();

????3. var date = new Date(compareTo.getTime() + 24 * 60 * 60 * 1000);

????4.

????5. assertEquals("in 24 hours"?? dateUtil.differenceInWords(date?? compareTo));

????6. }

????????????????????Щ???????????????????????????????(????)???????????(????????????????????????????????????????????÷?????????????????)??????????????????????????????????????????????????????????澯???????????????????

????[javascript] view plaincopy1. dateUtil.differenceInWords = function (date?? compareTo) {

????2. compareTo = compareTo || new Date();

????3. var diff = compareTo - date;

????4. var future = diff < 0;

????5. diff = Math.abs(diff);

????6. var humanized;

????7.

????8. if (diff > units.month) {

????9. humanized = "more than a month";

????10. } else if (diff > units.week) {

????11. humanized = format(Math.floor(diff / units.week)?? "week");

????12. } else {

????13. var pieces = []?? num?? consider = ["day"?? "hour"?? "minute"?? "second"]?? measure;

????14.

????15. for (var i = 0?? l = consider.length; i < l; ++i) {

????16. measure = units[consider[i]];

????17.

????18. if (diff > measure) {

????19. num = Math.floor(diff / measure);

????20. diff = diff - (num * measure);