formats.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. module('Formats', {
  2. setup: function(){
  3. this.input = $('<input type="text">').appendTo('#qunit-fixture');
  4. this.date = UTCDate(2012, 2, 15, 0, 0, 0, 0); // March 15, 2012
  5. },
  6. teardown: function(){
  7. this.input.data('datepicker').picker.remove();
  8. }
  9. });
  10. test('d: Day of month, no leading zero.', function(){
  11. this.input
  12. .val('2012-03-05')
  13. .datepicker({format: 'yyyy-mm-d'})
  14. .datepicker('setValue');
  15. equal(this.input.val().split('-')[2], '5');
  16. });
  17. test('dd: Day of month, leading zero.', function(){
  18. this.input
  19. .val('2012-03-5')
  20. .datepicker({format: 'yyyy-mm-dd'})
  21. .datepicker('setValue');
  22. equal(this.input.val().split('-')[2], '05');
  23. });
  24. test('D: Day of week, short.', function(){
  25. this.input
  26. .val('2012-03-05')
  27. .datepicker({format: 'yyyy-mm-dd-D'})
  28. .datepicker('setValue');
  29. equal(this.input.val().split('-')[3], 'Mon');
  30. });
  31. test('DD: Day of week, long.', function(){
  32. this.input
  33. .val('2012-03-05')
  34. .datepicker({format: 'yyyy-mm-dd-DD'})
  35. .datepicker('setValue');
  36. equal(this.input.val().split('-')[3], 'Monday');
  37. });
  38. test('m: Month, no leading zero.', function(){
  39. this.input
  40. .val('2012-03-05')
  41. .datepicker({format: 'yyyy-m-dd'})
  42. .datepicker('setValue');
  43. equal(this.input.val().split('-')[1], '3');
  44. });
  45. test('mm: Month, leading zero.', function(){
  46. this.input
  47. .val('2012-3-5')
  48. .datepicker({format: 'yyyy-mm-dd'})
  49. .datepicker('setValue');
  50. equal(this.input.val().split('-')[1], '03');
  51. });
  52. test('M: Month shortname.', function(){
  53. this.input
  54. .val('2012-Mar-05')
  55. .datepicker({format: 'yyyy-M-dd'})
  56. .datepicker('setValue');
  57. equal(this.input.val().split('-')[1], 'Mar');
  58. });
  59. test('M: Month shortname case insensitive.', function(){
  60. this.input
  61. .val('2012-MAR-05')
  62. .datepicker({format: 'yyyy-M-dd'})
  63. .datepicker('setValue');
  64. equal(this.input.val().split('-')[1], 'Mar');
  65. });
  66. test('MM: Month full name.', function(){
  67. this.input
  68. .val('2012-March-5')
  69. .datepicker({format: 'yyyy-MM-dd'})
  70. .datepicker('setValue');
  71. equal(this.input.val().split('-')[1], 'March');
  72. });
  73. test('M: Month fullname case insensitive.', function(){
  74. this.input
  75. .val('2012-MARCH-05')
  76. .datepicker({format: 'yyyy-MM-dd'})
  77. .datepicker('setValue');
  78. equal(this.input.val().split('-')[1], 'March');
  79. });
  80. test('yy: Year, two-digit.', function(){
  81. this.input
  82. .val('2012-03-05')
  83. .datepicker({format: 'yy-mm-dd'})
  84. .datepicker('setValue');
  85. equal(this.input.val().split('-')[0], '12');
  86. });
  87. test('yyyy: Year, four-digit.', function(){
  88. this.input
  89. .val('2012-03-5')
  90. .datepicker({format: 'yyyy-mm-dd'})
  91. .datepicker('setValue');
  92. equal(this.input.val().split('-')[0], '2012');
  93. });
  94. test('dd-mm-yyyy: Regression: Prevent potential month overflow in small-to-large formats (Mar 31, 2012 -> Mar 01, 2012)', function(){
  95. this.input
  96. .val('31-03-2012')
  97. .datepicker({format: 'dd-mm-yyyy'})
  98. .datepicker('setValue');
  99. equal(this.input.val(), '31-03-2012');
  100. });
  101. test('dd-mm-yyyy: Leap day', function(){
  102. this.input
  103. .val('29-02-2012')
  104. .datepicker({format: 'dd-mm-yyyy'})
  105. .datepicker('setValue');
  106. equal(this.input.val(), '29-02-2012');
  107. });
  108. test('yyyy-mm-dd: Alternative format', function(){
  109. this.input
  110. .val('2012-02-12')
  111. .datepicker({format: 'yyyy-mm-dd'})
  112. .datepicker('setValue');
  113. equal(this.input.val(), '2012-02-12');
  114. });
  115. test('yyyy-MM-dd: Regression: Infinite loop when numbers used for month', function(){
  116. this.input
  117. .val('2012-02-12')
  118. .datepicker({format: 'yyyy-MM-dd'})
  119. .datepicker('setValue');
  120. equal(this.input.val(), '2012-February-12');
  121. });
  122. test('+1d: Tomorrow', patch_date(function(Date){
  123. Date.now = UTCDate(2012, 2, 15);
  124. this.input
  125. .val('+1d')
  126. .datepicker({format: 'dd-mm-yyyy'})
  127. .datepicker('setValue');
  128. equal(this.input.val(), '16-03-2012');
  129. }));
  130. test('tomorrow (alias for +1d): Tomorrow', patch_date(function(Date){
  131. Date.now = UTCDate(2012, 2, 15);
  132. this.input
  133. .val('tomorrow')
  134. .datepicker({format: 'dd-mm-yyyy'})
  135. .datepicker('setValue');
  136. equal(this.input.val(), '16-03-2012');
  137. }));
  138. test('-1d: Yesterday', patch_date(function(Date){
  139. Date.now = UTCDate(2012, 2, 15);
  140. this.input
  141. .val('-1d')
  142. .datepicker({format: 'dd-mm-yyyy'})
  143. .datepicker('setValue');
  144. equal(this.input.val(), '14-03-2012');
  145. }));
  146. test('yesterday (alias for -1d): Yesterday', patch_date(function(Date){
  147. Date.now = UTCDate(2012, 2, 15);
  148. this.input
  149. .val('yesterday')
  150. .datepicker({format: 'dd-mm-yyyy'})
  151. .datepicker('setValue');
  152. equal(this.input.val(), '14-03-2012');
  153. }));
  154. test('+1w: Next week', patch_date(function(Date){
  155. Date.now = UTCDate(2012, 2, 15);
  156. this.input
  157. .val('+1w')
  158. .datepicker({format: 'dd-mm-yyyy'})
  159. .datepicker('setValue');
  160. equal(this.input.val(), '22-03-2012');
  161. }));
  162. test('-1w: Last week', patch_date(function(Date){
  163. Date.now = UTCDate(2012, 2, 15);
  164. this.input
  165. .val('-1w')
  166. .datepicker({format: 'dd-mm-yyyy'})
  167. .datepicker('setValue');
  168. equal(this.input.val(), '08-03-2012');
  169. }));
  170. test('+1m: Next month', patch_date(function(Date){
  171. Date.now = UTCDate(2012, 2, 15);
  172. this.input
  173. .val('+1m')
  174. .datepicker({format: 'dd-mm-yyyy'})
  175. .datepicker('setValue');
  176. equal(this.input.val(), '15-04-2012');
  177. }));
  178. test('-1m: Last month', patch_date(function(Date){
  179. Date.now = UTCDate(2012, 2, 15);
  180. this.input
  181. .val('-1m')
  182. .datepicker({format: 'dd-mm-yyyy'})
  183. .datepicker('setValue');
  184. equal(this.input.val(), '15-02-2012');
  185. }));
  186. test('+1y: Next year', patch_date(function(Date){
  187. Date.now = UTCDate(2012, 2, 15);
  188. this.input
  189. .val('+1y')
  190. .datepicker({format: 'dd-mm-yyyy'})
  191. .datepicker('setValue');
  192. equal(this.input.val(), '15-03-2013');
  193. }));
  194. test('-1y: Last year', patch_date(function(Date){
  195. Date.now = UTCDate(2012, 2, 15);
  196. this.input
  197. .val('-1y')
  198. .datepicker({format: 'dd-mm-yyyy'})
  199. .datepicker('setValue');
  200. equal(this.input.val(), '15-03-2011');
  201. }));
  202. test('-1y +2m: Multiformat', patch_date(function(Date){
  203. Date.now = UTCDate(2012, 2, 15);
  204. this.input
  205. .val('-1y +2m')
  206. .datepicker({format: 'dd-mm-yyyy'})
  207. .datepicker('setValue');
  208. equal(this.input.val(), '15-05-2011');
  209. }));
  210. test('Regression: End-of-month bug', patch_date(function(Date){
  211. Date.now = UTCDate(2012, 4, 31);
  212. this.input
  213. .val('29-02-2012')
  214. .datepicker({format: 'dd-mm-yyyy'})
  215. .datepicker('setValue');
  216. equal(this.input.val(), '29-02-2012');
  217. }));
  218. test('Invalid formats are force-parsed into a valid date on tab', patch_date(function(Date){
  219. Date.now = UTCDate(2012, 4, 31);
  220. this.input
  221. .val('44-44-4444')
  222. .datepicker({format: 'yyyy-MM-dd'})
  223. .focus();
  224. this.input.trigger({
  225. type: 'keydown',
  226. keyCode: 9
  227. });
  228. equal(this.input.val(), '56-September-30');
  229. }));
  230. test('Trailing separators', patch_date(function(Date){
  231. Date.now = UTCDate(2012, 4, 31);
  232. this.input
  233. .val('29.02.2012.')
  234. .datepicker({format: 'dd.mm.yyyy.'})
  235. .datepicker('setValue');
  236. equal(this.input.val(), '29.02.2012.');
  237. }));
  238. test('Assume nearby year - last century', patch_date(function(Date){
  239. Date.now = UTCDate(2012, 4, 31);
  240. this.input
  241. .val('02/14/91')
  242. .datepicker({format: 'mm/dd/yyyy', assumeNearbyYear: true})
  243. .datepicker('setValue');
  244. equal(this.input.val(), '02/14/1991');
  245. }));
  246. test('Assume nearby year - this century (- 1 year)', patch_date(function(Date){
  247. Date.now = UTCDate(2012, 4, 31);
  248. this.input
  249. .val('02/14/01')
  250. .datepicker({format: 'mm/dd/yyyy', assumeNearbyYear: true})
  251. .datepicker('setValue');
  252. equal(this.input.val(), '02/14/2001');
  253. }));
  254. test('Assume nearby year - this century (+ 7 years)', patch_date(function(Date){
  255. Date.now = UTCDate(2012, 4, 31);
  256. this.input
  257. .val('02/14/19')
  258. .datepicker({format: 'mm/dd/yyyy', assumeNearbyYear: true})
  259. .datepicker('setValue');
  260. equal(this.input.val(), '02/14/2019');
  261. }));
  262. test('Assume nearby year - this century (+ 13 years)', patch_date(function(Date){
  263. Date.now = UTCDate(2012, 4, 31);
  264. this.input
  265. .val('02/14/23')
  266. .datepicker({format: 'mm/dd/yyyy', assumeNearbyYear: true})
  267. .datepicker('setValue');
  268. equal(this.input.val(), '02/14/1923');
  269. }));
  270. test('Assume nearby year - this century (+ 13 years, threshold = 30)', patch_date(function(Date){
  271. Date.now = UTCDate(2012, 4, 31);
  272. this.input
  273. .val('02/14/23')
  274. .datepicker({format: 'mm/dd/yyyy', assumeNearbyYear: 30})
  275. .datepicker('setValue');
  276. equal(this.input.val(), '02/14/2023');
  277. }));