date_format-test.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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.getTimezoneOffset = function () {
  24. return -660;
  25. };
  26. // when tz offset is in the pattern, the date should be in local time
  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.getTimezoneOffset = function () {
  31. return 120;
  32. };
  33. dateFormat.asString(dateFormat.ISO8601_WITH_TZ_OFFSET_FORMAT, tzDate)
  34. .should.eql('2010-01-11T14:31:30.005-0200');
  35. });
  36. it('should provide a just-the-time format', function() {
  37. dateFormat.asString(dateFormat.ABSOLUTETIME_FORMAT, date).should.eql('14:31:30.005');
  38. });
  39. it('should provide a custom format', function() {
  40. var customDate = createFixedDate();
  41. customDate.getTimezoneOffset = function () {
  42. return 120;
  43. };
  44. dateFormat.asString('O.SSS.ss.mm.hh.dd.MM.yy', customDate).should.eql('-0200.005.30.31.14.11.01.10');
  45. });
  46. });