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.
 
 
 
 
 
 

290 lines
8.3 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. months = cint(months);
  83. new_dt = new Date(dt.getFullYear(), dt.getMonth()+months, dt.getDate())
  84. if(new_dt.getDate() != dt.getDate()) {
  85. // month has changed, go the last date of prev month
  86. return dateutil.month_end(new Date(dt.getFullYear(), dt.getMonth()+months, 1))
  87. }
  88. return dateutil.obj_to_str(new_dt);
  89. },
  90. month_start: function() {
  91. var d = new Date();
  92. return d.getFullYear() + '-' + int_to_str(d.getMonth()+1,2) + '-01';
  93. },
  94. month_end: function(d) {
  95. if(!d)var d = new Date();
  96. var m = d.getMonth() + 1;
  97. var y = d.getFullYear();
  98. last_date = month_last[m];
  99. if(m==2 && (y % 4)==0 && ((y % 100)!=0 || (y % 400)==0)) // leap year test
  100. last_date = 29;
  101. return y+'-'+int_to_str(m,2)+'-'+last_date;
  102. },
  103. get_user_fmt: function() {
  104. var t = sys_defaults.date_format;
  105. if(!t) t = 'dd-mm-yyyy';
  106. return t;
  107. },
  108. str_to_user: function(val, no_time_str) {
  109. var user_fmt = dateutil.get_user_fmt();
  110. var time_str = '';
  111. if(val==null||val=='')return null;
  112. // separate time string if there
  113. if(val.search(' ')!=-1) {
  114. var tmp = val.split(' ');
  115. if(tmp[1])
  116. time_str = ' ' + tmp[1];
  117. var d = tmp[0];
  118. } else {
  119. var d = val;
  120. }
  121. if(no_time_str)time_str = '';
  122. // set to user fmt
  123. d = d.split('-');
  124. if(d.length==3) {
  125. if(user_fmt=='dd-mm-yyyy')
  126. val = d[2]+'-'+d[1]+'-'+d[0] + time_str;
  127. else if(user_fmt=='dd/mm/yyyy')
  128. val = d[2]+'/'+d[1]+'/'+d[0] + time_str;
  129. else if(user_fmt=='yyyy-mm-dd')
  130. val = d[0]+'-'+d[1]+'-'+d[2] + time_str;
  131. else if(user_fmt=='mm/dd/yyyy')
  132. val = d[1]+'/'+d[2]+'/'+d[0] + time_str;
  133. else if(user_fmt=='mm-dd-yyyy')
  134. val = d[1]+'-'+d[2]+'-'+d[0] + time_str;
  135. }
  136. return val;
  137. },
  138. full_str: function() {
  139. var d = new Date();
  140. return d.getFullYear() + '-' + (d.getMonth()+1) + '-' + d.getDate() + ' '
  141. + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds();
  142. },
  143. user_to_str: function(d, no_time_str) {
  144. var user_fmt = this.get_user_fmt();
  145. var time_str = '';
  146. if (d.search(/ /)!=-1) {
  147. time_str = " " + d.split(" ")[1];
  148. d = d.split(" ")[0];
  149. }
  150. if(user_fmt=='dd-mm-yyyy') {
  151. var d = d.split('-');
  152. var val = d[2]+'-'+d[1]+'-'+d[0];
  153. }
  154. else if(user_fmt=='dd/mm/yyyy') {
  155. var d = d.split('/');
  156. var val = d[2]+'-'+d[1]+'-'+d[0];
  157. }
  158. else if(user_fmt=='yyyy-mm-dd') {
  159. var val = d;
  160. }
  161. else if(user_fmt=='mm/dd/yyyy') {
  162. var d = d.split('/');
  163. var val = d[2]+'-'+d[0]+'-'+d[1];
  164. }
  165. else if(user_fmt=='mm-dd-yyyy') {
  166. var d = d.split('-');
  167. var val = d[2]+'-'+d[0]+'-'+d[1];
  168. }
  169. if(no_time_str)time_str = '';
  170. return val + time_str;
  171. },
  172. user_to_obj: function(d) {
  173. return dateutil.str_to_obj(dateutil.user_to_str(d));
  174. },
  175. global_date_format: function(d) {
  176. if(d.substr) d = this.str_to_obj(d);
  177. return nth(d.getDate()) + ' ' + month_list_full[d.getMonth()] + ' ' + d.getFullYear();
  178. },
  179. get_today: function() {
  180. var today = new Date();
  181. var m = (today.getMonth()+1)+'';
  182. if(m.length==1)m='0'+m;
  183. var d = today.getDate()+'';
  184. if(d.length==1)d='0'+d;
  185. return today.getFullYear()+'-'+m+'-'+d;
  186. },
  187. get_cur_time: function() {
  188. return wn.datetime.now_time();
  189. }
  190. }
  191. wn.datetime.only_date = function(val) {
  192. if(val==null||val=='')return null;
  193. if(val.search(':')!=-1) {
  194. var tmp = val.split(' ');
  195. var d = tmp[0].split('-');
  196. } else {
  197. var d = val.split('-');
  198. }
  199. if(d.length==3)
  200. val = d[2]+'-'+d[1]+'-'+d[0];
  201. return val;
  202. }
  203. /*
  204. * JavaScript Pretty Date
  205. * Copyright (c) 2011 John Resig (ejohn.org)
  206. * Licensed under the MIT and GPL licenses.
  207. */
  208. // Takes an ISO time and returns a string representing how
  209. // long ago the date represents.
  210. function prettyDate(time){
  211. if(!time) return ''
  212. var date = time;
  213. if(typeof(time)=="string")
  214. date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ").replace(/\.[0-9]*/, ""));
  215. var diff = (((new Date()).getTime() - date.getTime()) / 1000),
  216. day_diff = Math.floor(diff / 86400);
  217. if ( isNaN(day_diff) || day_diff < 0 )
  218. return '';
  219. return day_diff == 0 && (
  220. diff < 60 && "just now" ||
  221. diff < 120 && "1 minute ago" ||
  222. diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
  223. diff < 7200 && "1 hour ago" ||
  224. diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
  225. day_diff == 1 && "Yesterday" ||
  226. day_diff < 7 && day_diff + " days ago" ||
  227. day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago" ||
  228. day_diff < 365 && Math.ceil( day_diff / 30) + " months ago" ||
  229. "more than " + Math.floor( day_diff / 365 ) + " year(s) ago";
  230. }
  231. // If jQuery is included in the page, adds a jQuery plugin to handle it as well
  232. if ( typeof jQuery != "undefined" )
  233. jQuery.fn.prettyDate = function(){
  234. return this.each(function(){
  235. var date = prettyDate(this.title);
  236. if ( date )
  237. jQuery(this).text( date );
  238. });
  239. };
  240. var comment_when = prettyDate;
  241. wn.datetime.comment_when = prettyDate;
  242. // globals (deprecate)
  243. var date = dateutil = wn.datetime;
  244. var get_today = wn.datetime.get_today
  245. var only_date = wn.datetime.only_date;