bjui-util.date.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*!
  2. * B-JUI v1.2 (http://b-jui.com)
  3. * Git@OSC (http://git.oschina.net/xknaan/B-JUI)
  4. * Copyright 2014 K'naan (xknaan@163.com).
  5. * Licensed under Apache (http://www.apache.org/licenses/LICENSE-2.0)
  6. */
  7. /* ========================================================================
  8. * B-JUI: bjui-util.date.js v1.2
  9. * @author K'naan (xknaan@163.com)
  10. * -- Modified from dwz.util.date.js (author:ZhangHuihua@msn.com)
  11. * http://git.oschina.net/xknaan/B-JUI/blob/master/BJUI/js/bjui-util.date.js
  12. * ========================================================================
  13. * Copyright 2014 K'naan.
  14. * Licensed under Apache (http://www.apache.org/licenses/LICENSE-2.0)
  15. * ======================================================================== */
  16. +function ($) {
  17. 'use strict';
  18. var MONTH_NAMES = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')
  19. var DAY_NAMES = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat')
  20. function LZ(x) {
  21. return (x < 0 || x > 9 ? '' :'0') + x
  22. }
  23. /**
  24. * formatDate (date_object, format)
  25. * Returns a date in the output format specified.
  26. * The format string uses the same abbreviations as in parseDate()
  27. * @param {Object} date
  28. * @param {Object} format
  29. */
  30. function formatDate(date, format) {
  31. format = format + ''
  32. var result = ''
  33. var i_format = 0
  34. var c = ''
  35. var token = ''
  36. var y = date.getYear() + ''
  37. var M = date.getMonth() + 1
  38. var d = date.getDate()
  39. var E = date.getDay()
  40. var H = date.getHours()
  41. var m = date.getMinutes()
  42. var s = date.getSeconds()
  43. var yyyy, yy, MMM, MM, dd, hh, h, mm, ss, ampm, HH, H, KK, K, kk, k
  44. // Convert real date parts into formatted versions
  45. var value = {}
  46. if (y.length < 4) {
  47. y = '' + (y - 0 + 1900)
  48. }
  49. value['y'] = '' + y
  50. value['yyyy'] = y
  51. value['yy'] = y.substring(2, 4)
  52. value['M'] = M
  53. value['MM'] = LZ(M)
  54. value['MMM'] = MONTH_NAMES[M - 1]
  55. value['NNN'] = MONTH_NAMES[M + 11]
  56. value['d'] = d
  57. value['dd'] = LZ(d)
  58. value['E'] = DAY_NAMES[E + 7]
  59. value['EE'] = DAY_NAMES[E]
  60. value['H'] = H
  61. value['HH'] = LZ(H)
  62. if (H == 0) {
  63. value['h'] = 12
  64. } else if (H > 12) {
  65. value['h'] = H - 12
  66. } else {
  67. value['h'] = H
  68. }
  69. value['hh'] = LZ(value['h'])
  70. if (H > 11) {
  71. value['K'] = H - 12
  72. } else {
  73. value['K'] = H
  74. }
  75. value['k'] = H + 1
  76. value['KK'] = LZ(value['K'])
  77. value['kk'] = LZ(value['k'])
  78. if (H > 11) {
  79. value['a'] = 'PM'
  80. } else {
  81. value['a'] = 'AM'
  82. }
  83. value['m'] = m
  84. value['mm'] = LZ(m)
  85. value['s'] = s
  86. value['ss'] = LZ(s)
  87. while (i_format < format.length) {
  88. c = format.charAt(i_format)
  89. token = ''
  90. while (format.charAt(i_format) == c && i_format < format.length) {
  91. token += format.charAt(i_format++)
  92. }
  93. if (value[token] != null) {
  94. result += value[token]
  95. } else {
  96. result += token
  97. }
  98. }
  99. return result
  100. }
  101. function _isInteger(val) {
  102. return new RegExp(/^\d+$/).test(val)
  103. }
  104. function _getInt(str, i, minlength, maxlength) {
  105. for (var x = maxlength; x >= minlength; x--) {
  106. var token = str.substring(i, i + x)
  107. if (token.length < minlength) {
  108. return null
  109. }
  110. if (_isInteger(token)) {
  111. return token
  112. }
  113. }
  114. return null
  115. }
  116. /**
  117. * parseDate( date_string , format_string )
  118. *
  119. * This function takes a date string and a format string. It matches
  120. * If the date string matches the format string, it returns the date.
  121. * If it does not match, it returns 0.
  122. * @param {Object} val
  123. * @param {Object} format
  124. */
  125. function parseDate(val, format) {
  126. val = val + ''
  127. format = format + ''
  128. var i_val = 0
  129. var i_format = 0
  130. var c = ''
  131. var token = ''
  132. var token2 = ''
  133. var x, y
  134. var now = new Date(1900, 0, 1)
  135. var year = now.getYear()
  136. var month = now.getMonth() + 1
  137. var date = 1
  138. var hh = now.getHours()
  139. var mm = now.getMinutes()
  140. var ss = now.getSeconds()
  141. var ampm = ''
  142. while (i_format < format.length) {
  143. // Get next token from format string
  144. c = format.charAt(i_format)
  145. token = ''
  146. while (format.charAt(i_format) == c && i_format < format.length) {
  147. token += format.charAt(i_format++)
  148. }
  149. // Extract contents of value based on format token
  150. if (token == 'yyyy' || token == 'yy' || token == 'y') {
  151. if (token == 'yyyy') {
  152. x = 4
  153. y = 4
  154. }
  155. if (token == 'yy') {
  156. x = 2
  157. y = 2
  158. }
  159. if (token == 'y') {
  160. x = 2
  161. y = 4
  162. }
  163. year = _getInt(val, i_val, x, y)
  164. if (year == null) {
  165. return 0
  166. }
  167. i_val += year.length
  168. if (year.length == 2) {
  169. if (year > 70) {
  170. year = 1900 + (year - 0)
  171. } else {
  172. year = 2e3 + (year - 0)
  173. }
  174. }
  175. } else if (token == 'MMM' || token == 'NNN') {
  176. month = 0
  177. for (var i = 0; i < MONTH_NAMES.length; i++) {
  178. var month_name = MONTH_NAMES[i]
  179. if (val.substring(i_val, i_val + month_name.length).toLowerCase() == month_name.toLowerCase()) {
  180. if (token == 'MMM' || token == 'NNN' && i > 11) {
  181. month = i + 1
  182. if (month > 12) {
  183. month -= 12
  184. }
  185. i_val += month_name.length
  186. break
  187. }
  188. }
  189. }
  190. if (month < 1 || month > 12) {
  191. return 0
  192. }
  193. } else if (token == 'EE' || token == 'E') {
  194. for (var i = 0; i < DAY_NAMES.length; i++) {
  195. var day_name = DAY_NAMES[i]
  196. if (val.substring(i_val, i_val + day_name.length).toLowerCase() == day_name.toLowerCase()) {
  197. i_val += day_name.length
  198. break
  199. }
  200. }
  201. } else if (token == 'MM' || token == 'M') {
  202. month = _getInt(val, i_val, token.length, 2)
  203. if (month == null || month < 1 || month > 12) {
  204. return 0
  205. }
  206. i_val += month.length
  207. } else if (token == 'dd' || token == 'd') {
  208. date = _getInt(val, i_val, token.length, 2)
  209. if (date == null || date < 1 || date > 31) {
  210. return 0
  211. }
  212. i_val += date.length
  213. } else if (token == 'hh' || token == 'h') {
  214. hh = _getInt(val, i_val, token.length, 2)
  215. if (hh == null || hh < 1 || hh > 12) {
  216. return 0
  217. }
  218. i_val += hh.length
  219. } else if (token == 'HH' || token == 'H') {
  220. hh = _getInt(val, i_val, token.length, 2)
  221. if (hh == null || hh < 0 || hh > 23) {
  222. return 0
  223. }
  224. i_val += hh.length
  225. } else if (token == 'KK' || token == 'K') {
  226. hh = _getInt(val, i_val, token.length, 2)
  227. if (hh == null || hh < 0 || hh > 11) {
  228. return 0
  229. }
  230. i_val += hh.length
  231. } else if (token == 'kk' || token == 'k') {
  232. hh = _getInt(val, i_val, token.length, 2)
  233. if (hh == null || hh < 1 || hh > 24) {
  234. return 0
  235. }
  236. i_val += hh.length
  237. hh--
  238. } else if (token == 'mm' || token == 'm') {
  239. mm = _getInt(val, i_val, token.length, 2)
  240. if (mm == null || mm < 0 || mm > 59) {
  241. return 0
  242. }
  243. i_val += mm.length
  244. } else if (token == 'ss' || token == 's') {
  245. ss = _getInt(val, i_val, token.length, 2)
  246. if (ss == null || ss < 0 || ss > 59) {
  247. return 0
  248. }
  249. i_val += ss.length
  250. } else if (token == 'a') {
  251. if (val.substring(i_val, i_val + 2).toLowerCase() == 'am') {
  252. ampm = 'AM'
  253. } else if (val.substring(i_val, i_val + 2).toLowerCase() == 'pm') {
  254. ampm = 'PM'
  255. } else {
  256. return 0
  257. }
  258. i_val += 2
  259. } else {
  260. if (val.substring(i_val, i_val + token.length) != token) {
  261. return 0
  262. } else {
  263. i_val += token.length
  264. }
  265. }
  266. }
  267. // If there are any trailing characters left in the value, it doesn't match
  268. if (i_val != val.length) {
  269. return 0
  270. }
  271. // Is date valid for month?
  272. if (month == 2) {
  273. // Check for leap year
  274. if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
  275. // leap year
  276. if (date > 29) {
  277. return 0
  278. }
  279. } else {
  280. if (date > 28) {
  281. return 0
  282. }
  283. }
  284. }
  285. if (month == 4 || month == 6 || month == 9 || month == 11) {
  286. if (date > 30) {
  287. return 0
  288. }
  289. }
  290. // Correct hours value
  291. if (hh < 12 && ampm == 'PM') {
  292. hh = hh - 0 + 12
  293. } else if (hh > 11 && ampm == 'AM') {
  294. hh -= 12
  295. }
  296. return new Date(year, month - 1, date, hh, mm, ss)
  297. }
  298. Date.prototype.formatDate = function(dateFmt) {
  299. return formatDate(this, dateFmt)
  300. }
  301. String.prototype.parseDate = function(dateFmt) {
  302. if (this.length < dateFmt.length) {
  303. dateFmt = dateFmt.slice(0, this.length)
  304. }
  305. return parseDate(this, dateFmt)
  306. }
  307. /**
  308. * replaceTmEval('{1+2}-{2-1}')
  309. */
  310. function replaceTmEval(data) {
  311. return data.replace(RegExp('({[A-Za-z0-9_+-]*})', 'g'), function($1) {
  312. return eval('(' + $1.replace(/[{}]+/g, '') + ')')
  313. })
  314. }
  315. /**
  316. * dateFmt:%y-%M-%d
  317. * %y-%M-{%d+1}
  318. * ex: new Date().formatDateTm('%y-%M-{%d-1}')
  319. * new Date().formatDateTm('2012-1')
  320. */
  321. Date.prototype.formatDateTm = function(dateFmt) {
  322. var y = this.getFullYear()
  323. var m = this.getMonth() + 1
  324. var d = this.getDate()
  325. var sDate = dateFmt.replaceAll('%y', y).replaceAll('%M', m).replaceAll('%d', d)
  326. sDate = replaceTmEval(sDate)
  327. var _y = 1900, _m = 0, _d = 1
  328. var aDate = sDate.split('-')
  329. if (aDate.length > 0) _y = aDate[0]
  330. if (aDate.length > 1) _m = aDate[1] - 1
  331. if (aDate.length > 2) _d = aDate[2]
  332. return new Date(_y, _m, _d).formatDate('yyyy-MM-dd')
  333. }
  334. }(jQuery);