formats.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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('datetimepicker').picker.remove();
  8. }
  9. });
  10. test('d: Day of month, no leading zero.', function(){
  11. this.input
  12. .val('2012-03-05')
  13. .datetimepicker({format: 'yyyy-mm-d'})
  14. .datetimepicker('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. .datetimepicker({format: 'yyyy-mm-dd'})
  21. .datetimepicker('setValue');
  22. equal(this.input.val().split('-')[2], '05');
  23. });
  24. test('m: Month, no leading zero.', function(){
  25. this.input
  26. .val('2012-03-05')
  27. .datetimepicker({format: 'yyyy-m-dd'})
  28. .datetimepicker('setValue');
  29. equal(this.input.val().split('-')[1], '3');
  30. });
  31. test('mm: Month, leading zero.', function(){
  32. this.input
  33. .val('2012-3-5')
  34. .datetimepicker({format: 'yyyy-mm-dd'})
  35. .datetimepicker('setValue');
  36. equal(this.input.val().split('-')[1], '03');
  37. });
  38. test('M: Month shortname.', function(){
  39. this.input
  40. .val('2012-Mar-05')
  41. .datetimepicker({format: 'yyyy-M-dd'})
  42. .datetimepicker('setValue');
  43. equal(this.input.val().split('-')[1], 'Mar');
  44. });
  45. test('MM: Month full name.', function(){
  46. this.input
  47. .val('2012-March-5')
  48. .datetimepicker({format: 'yyyy-MM-dd'})
  49. .datetimepicker('setValue');
  50. equal(this.input.val().split('-')[1], 'March');
  51. });
  52. test('yy: Year, two-digit.', function(){
  53. this.input
  54. .val('2012-03-05')
  55. .datetimepicker({format: 'yy-mm-dd'})
  56. .datetimepicker('setValue');
  57. equal(this.input.val().split('-')[0], '12');
  58. });
  59. test('yyyy: Year, four-digit.', function(){
  60. this.input
  61. .val('2012-03-5')
  62. .datetimepicker({format: 'yyyy-mm-dd'})
  63. .datetimepicker('setValue');
  64. equal(this.input.val().split('-')[0], '2012');
  65. });
  66. test('H: 12 hour when language has meridiems', function(){
  67. this.input
  68. .val('2012-March-5 16:00:00')
  69. .datetimepicker({format: 'yyyy-mm-dd H:ii p'})
  70. .datetimepicker('setValue');
  71. ok(this.input.val().match(/4:00 pm/));
  72. });
  73. test('H: 24 hour when language has no meridiems', function(){
  74. $.fn.datetimepicker.dates['pt-BR'] = {
  75. days: ["Domingo", "Segunda", "Terça", "Quarta", "Quinta", "Sexta", "Sábado", "Domingo"],
  76. daysShort: ["Dom", "Seg", "Ter", "Qua", "Qui", "Sex", "Sáb", "Dom"],
  77. daysMin: ["Do", "Se", "Te", "Qu", "Qu", "Se", "Sa", "Do"],
  78. months: ["Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"],
  79. monthsShort: ["Jan", "Fev", "Mar", "Abr", "Mai", "Jun", "Jul", "Ago", "Set", "Out", "Nov", "Dez"],
  80. today: "Hoje",
  81. suffix: [],
  82. meridiem: []
  83. };
  84. this.input
  85. .val('2012-March-5 16:00:00')
  86. .datetimepicker({format: 'yyyy-mm-dd H:ii p', language: 'pt-BR'})
  87. .datetimepicker('setValue');
  88. ok(this.input.val().match(/16:00/));
  89. });
  90. test('dd-mm-yyyy: Regression: Prevent potential month overflow in small-to-large formats (Mar 31, 2012 -> Mar 01, 2012)', function(){
  91. this.input
  92. .val('31-03-2012')
  93. .datetimepicker({format: 'dd-mm-yyyy'})
  94. .datetimepicker('setValue');
  95. equal(this.input.val(), '31-03-2012');
  96. });
  97. test('dd-mm-yyyy: Leap day', function(){
  98. this.input
  99. .val('29-02-2012')
  100. .datetimepicker({format: 'dd-mm-yyyy'})
  101. .datetimepicker('setValue');
  102. equal(this.input.val(), '29-02-2012');
  103. });
  104. test('yyyy-mm-dd: Alternative format', function(){
  105. this.input
  106. .val('2012-02-12')
  107. .datetimepicker({format: 'yyyy-mm-dd'})
  108. .datetimepicker('setValue');
  109. equal(this.input.val(), '2012-02-12');
  110. });
  111. test('yyyy-MM-dd: Regression: Infinite loop when numbers used for month', function(){
  112. this.input
  113. .val('2012-02-12')
  114. .datetimepicker({format: 'yyyy-MM-dd'})
  115. .datetimepicker('setValue');
  116. equal(this.input.val(), '2012-February-12');
  117. });
  118. test('+1d: Tomorrow', patch_date(function(Date){
  119. Date.now = UTCDate(2012, 2, 15);
  120. this.input
  121. .val('+1d')
  122. .datetimepicker({format: 'dd-mm-yyyy'})
  123. .datetimepicker('setValue');
  124. equal(this.input.val(), '16-03-2012');
  125. }));
  126. test('-1d: Yesterday', patch_date(function(Date){
  127. Date.now = UTCDate(2012, 2, 15);
  128. this.input
  129. .val('-1d')
  130. .datetimepicker({format: 'dd-mm-yyyy'})
  131. .datetimepicker('setValue');
  132. equal(this.input.val(), '14-03-2012');
  133. }));
  134. test('+1w: Next week', patch_date(function(Date){
  135. Date.now = UTCDate(2012, 2, 15);
  136. this.input
  137. .val('+1w')
  138. .datetimepicker({format: 'dd-mm-yyyy'})
  139. .datetimepicker('setValue');
  140. equal(this.input.val(), '22-03-2012');
  141. }));
  142. test('-1w: Last week', patch_date(function(Date){
  143. Date.now = UTCDate(2012, 2, 15);
  144. this.input
  145. .val('-1w')
  146. .datetimepicker({format: 'dd-mm-yyyy'})
  147. .datetimepicker('setValue');
  148. equal(this.input.val(), '08-03-2012');
  149. }));
  150. test('+1m: Next month', patch_date(function(Date){
  151. Date.now = UTCDate(2012, 2, 15);
  152. this.input
  153. .val('+1m')
  154. .datetimepicker({format: 'dd-mm-yyyy'})
  155. .datetimepicker('setValue');
  156. equal(this.input.val(), '15-04-2012');
  157. }));
  158. test('-1m: Last month', patch_date(function(Date){
  159. Date.now = UTCDate(2012, 2, 15);
  160. this.input
  161. .val('-1m')
  162. .datetimepicker({format: 'dd-mm-yyyy'})
  163. .datetimepicker('setValue');
  164. equal(this.input.val(), '15-02-2012');
  165. }));
  166. test('+1y: Next year', patch_date(function(Date){
  167. Date.now = UTCDate(2012, 2, 15);
  168. this.input
  169. .val('+1y')
  170. .datetimepicker({format: 'dd-mm-yyyy'})
  171. .datetimepicker('setValue');
  172. equal(this.input.val(), '15-03-2013');
  173. }));
  174. test('-1y: Last year', patch_date(function(Date){
  175. Date.now = UTCDate(2012, 2, 15);
  176. this.input
  177. .val('-1y')
  178. .datetimepicker({format: 'dd-mm-yyyy'})
  179. .datetimepicker('setValue');
  180. equal(this.input.val(), '15-03-2011');
  181. }));
  182. test('-1y +2m: Multiformat', patch_date(function(Date){
  183. Date.now = UTCDate(2012, 2, 15);
  184. this.input
  185. .val('-1y +2m')
  186. .datetimepicker({format: 'dd-mm-yyyy'})
  187. .datetimepicker('setValue');
  188. equal(this.input.val(), '15-05-2011');
  189. }));
  190. test('Regression: End-of-month bug', patch_date(function(Date){
  191. Date.now = UTCDate(2012, 4, 31);
  192. this.input
  193. .val('29-02-2012')
  194. .datetimepicker({format: 'dd-mm-yyyy'})
  195. .datetimepicker('setValue');
  196. equal(this.input.val(), '29-02-2012');
  197. }));
  198. test('Invalid formats are force-parsed into a valid date on tab', patch_date(function(Date){
  199. Date.now = UTCDate(2012, 4, 31);
  200. this.input
  201. .val('44-44-4444')
  202. .datetimepicker({format: 'yyyy-MM-dd'})
  203. .focus();
  204. this.input.trigger({
  205. type: 'keydown',
  206. keyCode: 9
  207. });
  208. equal(this.input.val(), '56-September-30');
  209. }));
  210. test('Untrimmed datetime value', patch_date(function(Date){
  211. this.input
  212. .val('2012-03-05 ')
  213. .datetimepicker({format: 'yyyy-mm-dd hh:ii'})
  214. .datetimepicker('setValue');
  215. equal(this.input.val(), '2012-03-05 00:00');
  216. }));
  217. test('With timezone option', patch_date(function(Date){
  218. this.input
  219. .val('2012-03-05')
  220. .datetimepicker({format: 'yyyy-mm-dd hh:ii P Z'})
  221. .datetimepicker('setValue');
  222. equal(this.input.val(), '2012-03-05 00:00 AM UTC');
  223. }));