Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

274 lignes
7.3 KiB

  1. // Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
  2. // MIT License. See license.txt
  3. // Date
  4. function same_day(d1, d2) {
  5. if(d1.getFullYear()==d2.getFullYear() && d1.getMonth()==d2.getMonth() && d1.getDate()==d2.getDate())return true; else return false;
  6. }
  7. var month_list = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
  8. 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}
  9. var month_list_full = ['January','February','March','April','May','June','July','August','September','October','November','December'];
  10. var week_list = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
  11. var week_list_full = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
  12. function int_to_str(i, len) {
  13. i = ''+i;
  14. if(i.length<len)for(c=0;c<(len-i.length);c++)i='0'+i;
  15. return i
  16. }
  17. wn.datetime = {
  18. str_to_obj: function(d) {
  19. if(typeof d=="object") return d;
  20. if(!d) return new Date();
  21. var tm = [0, 0];
  22. if(d.search(' ')!=-1) {
  23. var tm = d.split(' ')[1].split(':');
  24. var d = d.split(' ')[0];
  25. }
  26. if(d.search('-')!=-1) {
  27. var t = d.split('-'); return new Date(t[0],t[1]-1,t[2],tm[0],tm[1]);
  28. } else if(d.search('/')!=-1) {
  29. var t = d.split('/'); return new Date(t[0],t[1]-1,t[2],tm[0],tm[1]);
  30. } else {
  31. return new Date();
  32. }
  33. },
  34. obj_to_str: function(d) {
  35. if(typeof d=='string') return d;
  36. return d.getFullYear() + '-' + int_to_str(d.getMonth()+1,2) + '-' + int_to_str(d.getDate(),2);
  37. },
  38. obj_to_user: function(d) {
  39. return dateutil.str_to_user(dateutil.obj_to_str(d));
  40. },
  41. get_ms_diff: function(d1, d2) {
  42. if(typeof d1=='string') d1 = dateutil.str_to_obj(d1);
  43. if(typeof d2=='string') d2 = dateutil.str_to_obj(d2);
  44. return d1-d2
  45. },
  46. get_diff: function(d1, d2) {
  47. return Math.round(dateutil.get_ms_diff(d1, d2) / 86400000);
  48. },
  49. get_hour_diff: function(d1, d2) {
  50. return Math.round(dateutil.get_ms_diff(d1, d2) / 3600000);
  51. },
  52. get_day_diff: function(d1, d2) {
  53. return dateutil.get_diff(new Date(d1.getYear(), d1.getMonth(), d1.getDate(), 0, 0),
  54. new Date(d2.getYear(), d2.getMonth(), d2.getDate(), 0, 0))
  55. },
  56. add_days: function(d, days) {
  57. var dt = dateutil.str_to_obj(d);
  58. var new_dt = new Date(dt.getTime()+(days*24*60*60*1000));
  59. return dateutil.obj_to_str(new_dt);
  60. },
  61. add_months: function(d, months) {
  62. dt = dateutil.str_to_obj(d)
  63. months = cint(months);
  64. new_dt = new Date(dt.getFullYear(), dt.getMonth()+months, dt.getDate())
  65. if(new_dt.getDate() != dt.getDate()) {
  66. // month has changed, go the last date of prev month
  67. return dateutil.month_end(new Date(dt.getFullYear(), dt.getMonth()+months, 1))
  68. }
  69. return dateutil.obj_to_str(new_dt);
  70. },
  71. month_start: function() {
  72. var d = new Date();
  73. return d.getFullYear() + '-' + int_to_str(d.getMonth()+1,2) + '-01';
  74. },
  75. month_end: function(d) {
  76. if(!d)var d = new Date();
  77. var m = d.getMonth() + 1;
  78. var y = d.getFullYear();
  79. last_date = month_last[m];
  80. if(m==2 && (y % 4)==0 && ((y % 100)!=0 || (y % 400)==0)) // leap year test
  81. last_date = 29;
  82. return y+'-'+int_to_str(m,2)+'-'+last_date;
  83. },
  84. get_user_fmt: function() {
  85. var t = sys_defaults.date_format;
  86. if(!t) t = 'dd-mm-yyyy';
  87. return t;
  88. },
  89. str_to_user: function(val, no_time_str) {
  90. var user_fmt = dateutil.get_user_fmt();
  91. var time_str = '';
  92. if(val==null||val=='')return null;
  93. // separate time string if there
  94. if(val.search(' ')!=-1) {
  95. var tmp = val.split(' ');
  96. if(tmp[1])
  97. time_str = ' ' + tmp[1];
  98. var d = tmp[0];
  99. } else {
  100. var d = val;
  101. }
  102. if(no_time_str)time_str = '';
  103. // set to user fmt
  104. d = d.split('-');
  105. if(d.length==3) {
  106. if(user_fmt=='dd-mm-yyyy')
  107. val = d[2]+'-'+d[1]+'-'+d[0] + time_str;
  108. else if(user_fmt=='dd/mm/yyyy')
  109. val = d[2]+'/'+d[1]+'/'+d[0] + time_str;
  110. else if(user_fmt=='yyyy-mm-dd')
  111. val = d[0]+'-'+d[1]+'-'+d[2] + time_str;
  112. else if(user_fmt=='mm/dd/yyyy')
  113. val = d[1]+'/'+d[2]+'/'+d[0] + time_str;
  114. else if(user_fmt=='mm-dd-yyyy')
  115. val = d[1]+'-'+d[2]+'-'+d[0] + time_str;
  116. }
  117. return val;
  118. },
  119. full_str: function() {
  120. var d = new Date();
  121. return d.getFullYear() + '-' + (d.getMonth()+1) + '-' + d.getDate() + ' '
  122. + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds();
  123. },
  124. user_to_str: function(d, no_time_str) {
  125. var user_fmt = this.get_user_fmt();
  126. var time_str = '';
  127. if (d.search(/ /)!=-1) {
  128. time_str = " " + d.split(" ")[1];
  129. d = d.split(" ")[0];
  130. }
  131. if(user_fmt=='dd-mm-yyyy') {
  132. var d = d.split('-');
  133. var val = d[2]+'-'+d[1]+'-'+d[0];
  134. }
  135. else if(user_fmt=='dd/mm/yyyy') {
  136. var d = d.split('/');
  137. var val = d[2]+'-'+d[1]+'-'+d[0];
  138. }
  139. else if(user_fmt=='yyyy-mm-dd') {
  140. var val = d;
  141. }
  142. else if(user_fmt=='mm/dd/yyyy') {
  143. var d = d.split('/');
  144. var val = d[2]+'-'+d[0]+'-'+d[1];
  145. }
  146. else if(user_fmt=='mm-dd-yyyy') {
  147. var d = d.split('-');
  148. var val = d[2]+'-'+d[0]+'-'+d[1];
  149. }
  150. if(no_time_str)time_str = '';
  151. return val + time_str;
  152. },
  153. user_to_obj: function(d) {
  154. return dateutil.str_to_obj(dateutil.user_to_str(d));
  155. },
  156. global_date_format: function(d) {
  157. if(d.substr) d = this.str_to_obj(d);
  158. return nth(d.getDate()) + ' ' + month_list_full[d.getMonth()] + ' ' + d.getFullYear();
  159. },
  160. get_today: function() {
  161. var today = new Date();
  162. var m = (today.getMonth()+1)+'';
  163. if(m.length==1)m='0'+m;
  164. var d = today.getDate()+'';
  165. if(d.length==1)d='0'+d;
  166. return today.getFullYear()+'-'+m+'-'+d;
  167. },
  168. get_cur_time: function() {
  169. return wn.datetime.now_time();
  170. }
  171. }
  172. wn.datetime.only_date = function(val) {
  173. if(val==null||val=='')return null;
  174. if(val.search(':')!=-1) {
  175. var tmp = val.split(' ');
  176. var d = tmp[0].split('-');
  177. } else {
  178. var d = val.split('-');
  179. }
  180. if(d.length==3)
  181. val = d[2]+'-'+d[1]+'-'+d[0];
  182. return val;
  183. }
  184. /*
  185. * JavaScript Pretty Date
  186. * Copyright (c) 2011 John Resig (ejohn.org)
  187. * Licensed under the MIT and GPL licenses.
  188. */
  189. // Takes an ISO time and returns a string representing how
  190. // long ago the date represents.
  191. function prettyDate(time){
  192. if(!time) return ''
  193. var date = time;
  194. if(typeof(time)=="string")
  195. date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ").replace(/\.[0-9]*/, ""));
  196. var diff = (((new Date()).getTime() - date.getTime()) / 1000),
  197. day_diff = Math.floor(diff / 86400);
  198. if ( isNaN(day_diff) || day_diff < 0 )
  199. return '';
  200. return when = day_diff == 0 && (
  201. diff < 60 && "just now" ||
  202. diff < 120 && "1 minute ago" ||
  203. diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
  204. diff < 7200 && "1 hour ago" ||
  205. diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
  206. day_diff == 1 && "Yesterday" ||
  207. day_diff < 7 && day_diff + " days ago" ||
  208. day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago" ||
  209. day_diff < 365 && Math.ceil( day_diff / 30) + " months ago" ||
  210. "> " + Math.floor( day_diff / 365 ) + " year(s) ago";
  211. }
  212. var comment_when = function(datetime) {
  213. return '<span class="wn-timestamp" data-timestamp="'+datetime+'">' + prettyDate(datetime) + '</span>';
  214. };
  215. wn.datetime.comment_when = prettyDate;
  216. wn.datetime.refresh_when = function() {
  217. if(jQuery) {
  218. $(".wn-timestamp").each(function() {
  219. $(this).html(prettyDate($(this).attr("data-timestamp")));
  220. })
  221. }
  222. }
  223. setInterval(function() { wn.datetime.refresh_when() }, 60000); // refresh every minute
  224. // globals (deprecate)
  225. var date = dateutil = wn.datetime;
  226. var get_today = wn.datetime.get_today
  227. var only_date = wn.datetime.only_date;