date_format-test.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. 'use strict';
  2. require('should');
  3. var dateFormat = require('../lib');
  4. function createFixedDate() {
  5. return new Date(2010, 0, 11, 14, 31, 30, 5);
  6. }
  7. describe('date_format', function() {
  8. var date = createFixedDate();
  9. it('should default to now when a date is not provided', function() {
  10. dateFormat.asString(dateFormat.DATETIME_FORMAT).should.not.be.empty();
  11. });
  12. it('should be usable directly without calling asString', function() {
  13. dateFormat(dateFormat.DATETIME_FORMAT, date).should.eql('11 01 2010 14:31:30.005');
  14. });
  15. it('should format a date as string using a pattern', function() {
  16. dateFormat.asString(dateFormat.DATETIME_FORMAT, date).should.eql('11 01 2010 14:31:30.005');
  17. });
  18. it('should default to the ISO8601 format', function() {
  19. dateFormat.asString(date).should.eql('2010-01-11T14:31:30.005');
  20. });
  21. it('should provide a ISO8601 with timezone offset format', function() {
  22. var tzDate = createFixedDate();
  23. tzDate.setMinutes(tzDate.getMinutes() - tzDate.getTimezoneOffset() - 660);
  24. tzDate.getTimezoneOffset = function () {
  25. return -660;
  26. };
  27. // when tz offset is in the pattern, the date should be in UTC
  28. dateFormat.asString(dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT, tzDate)
  29. .should.eql('2010-01-11T03:31:30.005+1100');
  30. tzDate = createFixedDate();
  31. tzDate.setMinutes((tzDate.getMinutes() - tzDate.getTimezoneOffset()) + 120);
  32. tzDate.getTimezoneOffset = function () {
  33. return 120;
  34. };
  35. dateFormat.asString(dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT, tzDate)
  36. .should.eql('2010-01-11T16:31:30.005-0200');
  37. });
  38. it('should provide a just-the-time format', function() {
  39. dateFormat.asString(dateFormat.ABSOLUTETIME_FORMAT, date).should.eql('14:31:30.005');
  40. });
  41. it('should provide a custom format', function() {
  42. var customDate = createFixedDate();
  43. customDate.setMinutes((customDate.getMinutes() - customDate.getTimezoneOffset()) + 120);
  44. customDate.getTimezoneOffset = function () {
  45. return 120;
  46. };
  47. dateFormat.asString('O.SSS.ss.mm.hh.dd.MM.yy', customDate).should.eql('-0200.005.30.31.16.11.01.10');
  48. });
  49. });