You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

289 line
8.2 KiB

  1. // Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. //
  3. // MIT License (MIT)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a
  6. // copy of this software and associated documentation files (the "Software"),
  7. // to deal in the Software without restriction, including without limitation
  8. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. // and/or sell copies of the Software, and to permit persons to whom the
  10. // Software is furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. // OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. //
  22. // Date
  23. function same_day(d1, d2) {
  24. if(d1.getFullYear()==d2.getFullYear() && d1.getMonth()==d2.getMonth() && d1.getDate()==d2.getDate())return true; else return false;
  25. }
  26. var month_list = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
  27. var month_last = {1:31,2:28,3:31,4:30,5:31,6:30,7:31,8:31,9:30,10:31,11:30,12:31}
  28. var month_list_full = ['January','February','March','April','May','June','July','August','September','October','November','December'];
  29. var week_list = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
  30. var week_list_full = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
  31. function int_to_str(i, len) {
  32. i = ''+i;
  33. if(i.length<len)for(c=0;c<(len-i.length);c++)i='0'+i;
  34. return i
  35. }
  36. wn.datetime = {
  37. str_to_obj: function(d) {
  38. if(typeof d=="object") return d;
  39. if(!d) return new Date();
  40. var tm = [0, 0];
  41. if(d.search(' ')!=-1) {
  42. var tm = d.split(' ')[1].split(':');
  43. var d = d.split(' ')[0];
  44. }
  45. if(d.search('-')!=-1) {
  46. var t = d.split('-'); return new Date(t[0],t[1]-1,t[2],tm[0],tm[1]);
  47. } else if(d.search('/')!=-1) {
  48. var t = d.split('/'); return new Date(t[0],t[1]-1,t[2],tm[0],tm[1]);
  49. } else {
  50. return new Date();
  51. }
  52. },
  53. obj_to_str: function(d) {
  54. if(typeof d=='string') return d;
  55. return d.getFullYear() + '-' + int_to_str(d.getMonth()+1,2) + '-' + int_to_str(d.getDate(),2);
  56. },
  57. obj_to_user: function(d) {
  58. return dateutil.str_to_user(dateutil.obj_to_str(d));
  59. },
  60. get_ms_diff: function(d1, d2) {
  61. if(typeof d1=='string') d1 = dateutil.str_to_obj(d1);
  62. if(typeof d2=='string') d2 = dateutil.str_to_obj(d2);
  63. return d1-d2
  64. },
  65. get_diff: function(d1, d2) {
  66. return Math.round(dateutil.get_ms_diff(d1, d2) / 86400000);
  67. },
  68. get_hour_diff: function(d1, d2) {
  69. return Math.round(dateutil.get_ms_diff(d1, d2) / 3600000);
  70. },
  71. get_day_diff: function(d1, d2) {
  72. return dateutil.get_diff(new Date(d1.getYear(), d1.getMonth(), d1.getDate(), 0, 0),
  73. new Date(d2.getYear(), d2.getMonth(), d2.getDate(), 0, 0))
  74. },
  75. add_days: function(d, days) {
  76. var dt = dateutil.str_to_obj(d);
  77. var new_dt = new Date(dt.getTime()+(days*24*60*60*1000));
  78. return dateutil.obj_to_str(new_dt);
  79. },
  80. add_months: function(d, months) {
  81. dt = dateutil.str_to_obj(d)
  82. new_dt = new Date(dt.getFullYear(), dt.getMonth()+months, dt.getDate())
  83. if(new_dt.getDate() != dt.getDate()) {
  84. // month has changed, go the last date of prev month
  85. return dateutil.month_end(new Date(dt.getFullYear(), dt.getMonth()+months, 1))
  86. }
  87. return dateutil.obj_to_str(new_dt);
  88. },
  89. month_start: function() {
  90. var d = new Date();
  91. return d.getFullYear() + '-' + int_to_str(d.getMonth()+1,2) + '-01';
  92. },
  93. month_end: function(d) {
  94. if(!d)var d = new Date();
  95. var m = d.getMonth() + 1;
  96. var y = d.getFullYear();
  97. last_date = month_last[m];
  98. if(m==2 && (y % 4)==0 && ((y % 100)!=0 || (y % 400)==0)) // leap year test
  99. last_date = 29;
  100. return y+'-'+int_to_str(m,2)+'-'+last_date;
  101. },
  102. get_user_fmt: function() {
  103. var t = sys_defaults.date_format;
  104. if(!t) t = 'dd-mm-yyyy';
  105. return t;
  106. },
  107. str_to_user: function(val, no_time_str) {
  108. var user_fmt = dateutil.get_user_fmt();
  109. var time_str = '';
  110. if(val==null||val=='')return null;
  111. // separate time string if there
  112. if(val.search(' ')!=-1) {
  113. var tmp = val.split(' ');
  114. if(tmp[1])
  115. time_str = ' ' + tmp[1];
  116. var d = tmp[0];
  117. } else {
  118. var d = val;
  119. }
  120. if(no_time_str)time_str = '';
  121. // set to user fmt
  122. d = d.split('-');
  123. if(d.length==3) {
  124. if(user_fmt=='dd-mm-yyyy')
  125. val = d[2]+'-'+d[1]+'-'+d[0] + time_str;
  126. else if(user_fmt=='dd/mm/yyyy')
  127. val = d[2]+'/'+d[1]+'/'+d[0] + time_str;
  128. else if(user_fmt=='yyyy-mm-dd')
  129. val = d[0]+'-'+d[1]+'-'+d[2] + time_str;
  130. else if(user_fmt=='mm/dd/yyyy')
  131. val = d[1]+'/'+d[2]+'/'+d[0] + time_str;
  132. else if(user_fmt=='mm-dd-yyyy')
  133. val = d[1]+'-'+d[2]+'-'+d[0] + time_str;
  134. }
  135. return val;
  136. },
  137. full_str: function() {
  138. var d = new Date();
  139. return d.getFullYear() + '-' + (d.getMonth()+1) + '-' + d.getDate() + ' '
  140. + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds();
  141. },
  142. user_to_str: function(d, no_time_str) {
  143. var user_fmt = this.get_user_fmt();
  144. var time_str = '';
  145. if (d.search(/ /)!=-1) {
  146. time_str = " " + d.split(" ")[1];
  147. d = d.split(" ")[0];
  148. }
  149. if(user_fmt=='dd-mm-yyyy') {
  150. var d = d.split('-');
  151. var val = d[2]+'-'+d[1]+'-'+d[0];
  152. }
  153. else if(user_fmt=='dd/mm/yyyy') {
  154. var d = d.split('/');
  155. var val = d[2]+'-'+d[1]+'-'+d[0];
  156. }
  157. else if(user_fmt=='yyyy-mm-dd') {
  158. var val = d;
  159. }
  160. else if(user_fmt=='mm/dd/yyyy') {
  161. var d = d.split('/');
  162. var val = d[2]+'-'+d[0]+'-'+d[1];
  163. }
  164. else if(user_fmt=='mm-dd-yyyy') {
  165. var d = d.split('-');
  166. var val = d[2]+'-'+d[0]+'-'+d[1];
  167. }
  168. if(no_time_str)time_str = '';
  169. return val + time_str;
  170. },
  171. user_to_obj: function(d) {
  172. return dateutil.str_to_obj(dateutil.user_to_str(d));
  173. },
  174. global_date_format: function(d) {
  175. if(d.substr) d = this.str_to_obj(d);
  176. return nth(d.getDate()) + ' ' + month_list_full[d.getMonth()] + ' ' + d.getFullYear();
  177. },
  178. get_today: function() {
  179. var today = new Date();
  180. var m = (today.getMonth()+1)+'';
  181. if(m.length==1)m='0'+m;
  182. var d = today.getDate()+'';
  183. if(d.length==1)d='0'+d;
  184. return today.getFullYear()+'-'+m+'-'+d;
  185. },
  186. get_cur_time: function() {
  187. return wn.datetime.now_time();
  188. }
  189. }
  190. wn.datetime.only_date = function(val) {
  191. if(val==null||val=='')return null;
  192. if(val.search(':')!=-1) {
  193. var tmp = val.split(' ');
  194. var d = tmp[0].split('-');
  195. } else {
  196. var d = val.split('-');
  197. }
  198. if(d.length==3)
  199. val = d[2]+'-'+d[1]+'-'+d[0];
  200. return val;
  201. }
  202. /*
  203. * JavaScript Pretty Date
  204. * Copyright (c) 2011 John Resig (ejohn.org)
  205. * Licensed under the MIT and GPL licenses.
  206. */
  207. // Takes an ISO time and returns a string representing how
  208. // long ago the date represents.
  209. function prettyDate(time){
  210. if(!time) return ''
  211. var date = time;
  212. if(typeof(time)=="string")
  213. date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ").replace(/\.[0-9]*/, ""));
  214. var diff = (((new Date()).getTime() - date.getTime()) / 1000),
  215. day_diff = Math.floor(diff / 86400);
  216. if ( isNaN(day_diff) || day_diff < 0 )
  217. return '';
  218. return day_diff == 0 && (
  219. diff < 60 && "just now" ||
  220. diff < 120 && "1 minute ago" ||
  221. diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
  222. diff < 7200 && "1 hour ago" ||
  223. diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
  224. day_diff == 1 && "Yesterday" ||
  225. day_diff < 7 && day_diff + " days ago" ||
  226. day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago" ||
  227. day_diff < 365 && Math.ceil( day_diff / 30) + " months ago" ||
  228. "> " + Math.floor( day_diff / 365 ) + " year(s) ago";
  229. }
  230. // If jQuery is included in the page, adds a jQuery plugin to handle it as well
  231. if ( typeof jQuery != "undefined" )
  232. jQuery.fn.prettyDate = function(){
  233. return this.each(function(){
  234. var date = prettyDate(this.title);
  235. if ( date )
  236. jQuery(this).text( date );
  237. });
  238. };
  239. var comment_when = prettyDate;
  240. wn.datetime.comment_when = prettyDate;
  241. // globals (deprecate)
  242. var date = dateutil = wn.datetime;
  243. var get_today = wn.datetime.get_today
  244. var only_date = wn.datetime.only_date;