date_format-test.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. dateFormat.asString(dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT, tzDate)
  28. .should.eql('2010-01-11T14:31:30.005+1100');
  29. tzDate = createFixedDate();
  30. tzDate.setMinutes((tzDate.getMinutes() - tzDate.getTimezoneOffset()) + 120);
  31. tzDate.getTimezoneOffset = function () {
  32. return 120;
  33. };
  34. dateFormat.asString(dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT, tzDate)
  35. .should.eql('2010-01-11T14:31:30.005-0200');
  36. });
  37. it('should provide a just-the-time format', function() {
  38. dateFormat.asString(dateFormat.ABSOLUTETIME_FORMAT, date).should.eql('14:31:30.005');
  39. });
  40. it('should provide a custom format', function() {
  41. var customDate = createFixedDate();
  42. customDate.setMinutes((customDate.getMinutes() - customDate.getTimezoneOffset()) + 120);
  43. customDate.getTimezoneOffset = function () {
  44. return 120;
  45. };
  46. dateFormat.asString('O.SSS.ss.mm.hh.dd.MM.yy', customDate).should.eql('-0200.005.30.31.14.11.01.10');
  47. });
  48. });