date-parse-test.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. var vows = require('vows');
  2. var assert = require('assert');
  3. require('../lib/date-utils.js');
  4. vows.describe('Date Parse').addBatch({
  5. 'can instantiate milliseconds': {
  6. topic: function () { return new Date(123456789); },
  7. 'returns a valid date object': function (date) {
  8. assert.ok(date);
  9. },
  10. 'returns a correct value': function (date) {
  11. assert.equal(date.valueOf(), 123456789);
  12. }
  13. },
  14. 'can instantiate string': {
  15. topic: function () { return new Date('Jan 1, 2011 01:01:01 GMT'); },
  16. 'returns a valid date object': function (date) {
  17. assert.ok(date);
  18. },
  19. 'returns a correct value': function (date) {
  20. assert.equal(date.valueOf(), 1293843661000);
  21. }
  22. },
  23. 'can instantiate arguments': {
  24. topic: function () { return new Date(2011, 1, 1, 1, 1, 1, 0); },
  25. 'returns a valid date object': function (date) {
  26. assert.ok(date);
  27. }
  28. },
  29. 'can parse normal date': {
  30. topic: function () { return Date.parse('Jan 1, 2011 01:01:01 GMT'); },
  31. 'returns a correct value': function (milli) {
  32. assert.equal(milli, 1293843661000);
  33. }
  34. },
  35. 'can parse ISO-8601': {
  36. topic: function () { return Date.parse('2011-01-01T01:01:01Z'); },
  37. 'returns a correct value': function (milli) {
  38. assert.equal(milli, 1293843661000);
  39. }
  40. },
  41. 'can parse custom format': {
  42. topic: function () {
  43. return Date.parse('20/6/2011 8:30', 'd/M/y H:m');
  44. },
  45. 'returns a correct value': function (milli) {
  46. var against = new Date(2011, 5, 20, 8, 30, 0);
  47. assert.equal(milli, against.valueOf());
  48. }
  49. },
  50. 'parse custom format with full month name': {
  51. topic: function () {
  52. return Date.parse('June 20, 2011 08:30:00', 'MMM d, y H:m:s');
  53. },
  54. 'returns a correct value': function (milli) {
  55. var against = new Date(2011, 5, 20, 8, 30, 0);
  56. assert.equal(milli, against.valueOf());
  57. }
  58. },
  59. 'parse custom format with abbr month name': {
  60. topic: function () {
  61. return Date.parse('Jun 20, 2011 08:30:00', 'MMM d, y H:m:s');
  62. },
  63. 'returns a correct value': function (milli) {
  64. var against = new Date(2011, 5, 20, 8, 30, 0);
  65. assert.equal(milli, against.valueOf());
  66. }
  67. },
  68. 'parse custom format with 12 hr clock': {
  69. topic: function () {
  70. return Date.parse('June 20, 2011 08:30:00AM', 'MMM d, y h:m:sa');
  71. },
  72. 'returns a correct value': function (milli) {
  73. var against = new Date(2011, 5, 20, 8, 30, 0);
  74. assert.equal(milli, against.valueOf());
  75. }
  76. },
  77. 'parse mysql date format': {
  78. topic: function () {
  79. return Date.parse('2011-06-20 08:30:00', 'y-M-d H:m:s');
  80. },
  81. 'returns a correct value': function (milli) {
  82. var against = new Date(2011, 5, 20, 8, 30, 0);
  83. assert.equal(milli, against.valueOf());
  84. }
  85. },
  86. 'parse us date format w/o time': {
  87. topic: function () {
  88. return Date.parse('6/20/2011', 'M/d/y');
  89. },
  90. 'returns a correct value': function (milli) {
  91. var against = new Date(2011, 5, 20);
  92. assert.equal(milli, against.valueOf());
  93. }
  94. },
  95. 'parse us date format with time': {
  96. topic: function () {
  97. return Date.parse('6/20/2011 00:00:01', 'M/d/y H:m:s');
  98. },
  99. 'returns a correct value': function (milli) {
  100. var against = new Date(2011, 5, 20, 0, 0, 1);
  101. assert.equal(milli, against.valueOf());
  102. }
  103. },
  104. 'parse uk date format w/o time': {
  105. topic: function () {
  106. return Date.parse('20/6/2011', 'd/M/y');
  107. },
  108. 'returns a correct value': function (milli) {
  109. var against = new Date(2011, 5, 20);
  110. assert.equal(milli, against.valueOf());
  111. }
  112. },
  113. 'parse uk date format with time': {
  114. topic: function () {
  115. return Date.parse('20/6/2011 00:00:01', 'd/M/y H:m:s');
  116. },
  117. 'returns a correct value': function (milli) {
  118. var against = new Date(2011, 5, 20, 0, 0, 1);
  119. assert.equal(milli, against.valueOf());
  120. }
  121. }
  122. }).export(module);