DateUtil.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. class DateUtil {
  2. static formatDate(Dates:Date=new Date()){
  3. //三目运算符
  4. // const Dates = new Date();
  5. //年份
  6. const Year : number = Dates.getFullYear();
  7. //月份下标是0-11
  8. const Months : any = ( Dates.getMonth() + 1 ) < 10 ? '0' + (Dates.getMonth() + 1) : ( Dates.getMonth() + 1);
  9. //具体的天数
  10. const Day : any = Dates.getDate() < 10 ? '0' + Dates.getDate() : Dates.getDate();
  11. //小时
  12. const Hours = Dates.getHours() < 10 ? '0' + Dates.getHours() : Dates.getHours();
  13. //分钟
  14. const Minutes = Dates.getMinutes() < 10 ? '0' + Dates.getMinutes() : Dates.getMinutes();
  15. //秒
  16. const Seconds = Dates.getSeconds() < 10 ? '0' + Dates.getSeconds() : Dates.getSeconds();
  17. //返回数据格式
  18. return Year + '-' + Months + '-' + Day + '-' + Hours + ':' + Minutes + ':' + Seconds;
  19. }
  20. static format_hh_mm_ss(Dates:Date=new Date()){
  21. //小时
  22. const Hours = Dates.getHours() < 10 ? '0' + Dates.getHours() : Dates.getHours();
  23. //分钟
  24. const Minutes = Dates.getMinutes() < 10 ? '0' + Dates.getMinutes() : Dates.getMinutes();
  25. //秒
  26. const Seconds = Dates.getSeconds() < 10 ? '0' + Dates.getSeconds() : Dates.getSeconds();
  27. //返回数据格式
  28. return Hours + ':' + Minutes + ':' + Seconds;
  29. }
  30. static formatSecond_00_00(allSecond: number, tag: string = ":") {
  31. let minutes = Math.floor(allSecond / 60);
  32. let second = allSecond - minutes * 60;
  33. let minuteStr = minutes.toString();
  34. if (minutes < 10) {
  35. minuteStr = "0" + minuteStr;
  36. }
  37. let secondStr = second.toString();
  38. if (second < 10) {
  39. secondStr = "0" + secondStr;
  40. }
  41. return minuteStr +tag+secondStr;
  42. }
  43. /**
  44. * 天数 :
  45. * date1在date2天前 ->返回-1,
  46. * date1在date2同一天 ->返回0,
  47. * date1在date2天后 ->返回1,
  48. * @param date1
  49. * @param date2
  50. */
  51. static DaySequence(date1: Date, date2: Date) {
  52. let y1 = date1.getFullYear();
  53. let y2 = date2.getFullYear();
  54. let y = this._DaySequence(y1, y2);
  55. if (y != 0) {
  56. return y;
  57. } else {
  58. let m1 = date1.getMonth();
  59. let m2 = date2.getMonth();
  60. let m = this._DaySequence(m1, m2);
  61. if (m != 0) {
  62. return m;
  63. } else {
  64. let d1 = date1.getDate();
  65. let d2 = date2.getDate();
  66. let d = this._DaySequence(d1, d2);
  67. return d;
  68. }
  69. }
  70. }
  71. private static _DaySequence(val1: number, val2: number) {
  72. if (val1 < val2) {
  73. return -1
  74. } else if (val1 < val2) {
  75. return 1
  76. } else {
  77. return 0;
  78. }
  79. }
  80. /**
  81. * 格式化时间
  82. * 注意:仅取整仅显示小时、仅显示分钟、仅显示秒
  83. * @param second 秒
  84. */
  85. public static secondRoundFormat(second: number): string {
  86. GLog.log("timeRoundFormat:", second);
  87. if (second >= 60) {
  88. if (second >= 2592000)//大于一月
  89. {
  90. //小时显示
  91. return Math.floor(second / 2592000) + "月";
  92. }
  93. else if (second >= 86400)//大于一天
  94. {
  95. //小时显示
  96. return Math.floor(second / 86400) + "天";
  97. }
  98. else if (second >= 3600)//大于一小时
  99. {
  100. //小时显示
  101. return Math.floor(second / 3600) + "小时";
  102. } else {
  103. //分钟显示
  104. return Math.floor(second / 60) + "分钟";
  105. }
  106. } else {
  107. //秒显示
  108. return Math.floor(second) + "秒";
  109. }
  110. }
  111. /**
  112. * 获取当前系统时间的秒数
  113. */
  114. public static getCurTime(): any {
  115. return Number((new Date().getTime() * 0.001).toFixed(0));
  116. }
  117. /**
  118. * 将秒数转换为 00时00分00秒 的形式
  119. * @param second 秒数
  120. * @returns {*} 格式化后的时分秒
  121. */
  122. public static secondConvertStringTime(second: number): string {
  123. let t;
  124. if (second > -1) {
  125. let hour = Math.floor(second / 3600);
  126. let min = Math.floor(second / 60) % 60;
  127. let sec = second % 60;
  128. if (hour < 10) {
  129. t = hour + "小时";
  130. } else {
  131. t = hour + "小时";
  132. }
  133. if (min < 10) { t += "0"; }
  134. t += min + "分";
  135. if (sec < 10) { t += "0"; }
  136. t += sec.toFixed(0) + "秒";
  137. }
  138. return t;
  139. }
  140. /**
  141. * 将秒数转换为 00:00:00 的形式
  142. * @param second 秒数
  143. * @returns {*} 格式化后的时分秒
  144. */
  145. public static secondConvertTime(second: number,part:number = 3): string {
  146. let t:string = `格式化时间,传入值错误:${second}`;
  147. if (second > -1) {
  148. let hour = Math.floor(second / 3600);
  149. let min = Math.floor(second / 60) % 60;
  150. let sec = second % 60;
  151. if (hour < 10) {
  152. t = "0" + hour + ":";
  153. } else {
  154. t = hour + ":";
  155. }
  156. if (min < 10) { t += "0"; }
  157. t += min + ":";
  158. if (sec < 10) { t += "0"; }
  159. t += sec.toFixed(0);
  160. }
  161. return t;
  162. }
  163. /**
  164. * 将服务端返回的时间转换为几时几分几秒(hh:mm:ss)
  165. * @param second 秒数
  166. */
  167. public static secondConverDateTime(second: number) {
  168. let date = new Date(second * 1000);
  169. let date_minus = ""
  170. let date_seconds = "";
  171. if (date.getMinutes() < 10) {
  172. date_minus = `0${date.getMinutes()}`;
  173. } else {
  174. date_minus = date.getMinutes().toString();
  175. }
  176. if (date.getSeconds() < 10) {
  177. date_seconds = `0${date.getSeconds()}`;
  178. } else {
  179. date_seconds = date.getSeconds().toString();
  180. }
  181. return `${date.getHours()}:${date_minus}:${date_seconds}`;
  182. }
  183. /**
  184. * 时分秒(00:00:00) 转为 时间戳
  185. * @param time 传入 00:00:00 的形式
  186. * @returns {string} 时间戳(单位:秒)
  187. */
  188. public static timeConvertUnixStamp(time: any): any {
  189. var s = '';
  190. var hour = time.split(':')[0];
  191. var min = time.split(':')[1];
  192. var sec = time.split(':')[2];
  193. s = Number(hour * 3600).toString() + Number(min * 60).toString() + Number(sec).toString();
  194. return s;
  195. }
  196. /**时间戳转为x月x日 */
  197. public static timestampTo_M_D(_timeStampSec: number): string {
  198. // date.getFullYear(); // 获取完整的年份(4位,1970)
  199. // date.getMonth(); // 获取月份(0-11,0代表1月,用的时候记得加上1)
  200. // date.getDate(); // 获取日(1-31)
  201. // date.getTime(); // 获取时间(从1970.1.1开始的毫秒数)
  202. // date.getHours(); // 获取小时数(0-23)
  203. // date.getMinutes(); // 获取分钟数(0-59)
  204. // date.getSeconds(); // 获取秒数(0-59)
  205. let date = new Date(_timeStampSec * 1000);
  206. return (date.getMonth() + 1).toString() + "月" + date.getDate() + "日";
  207. }
  208. /**时间戳转为年月日数组*/
  209. public static timestampTo_Y_M_D(timeStampSec: number): number[] {
  210. // date.getFullYear(); // 获取完整的年份(4位,1970)
  211. // date.getMonth(); // 获取月份(0-11,0代表1月,用的时候记得加上1)
  212. // date.getDate(); // 获取日(1-31)
  213. // date.getTime(); // 获取时间(从1970.1.1开始的毫秒数)
  214. // date.getHours(); // 获取小时数(0-23)
  215. // date.getMinutes(); // 获取分钟数(0-59)
  216. // date.getSeconds(); // 获取秒数(0-59)
  217. let date = new Date(timeStampSec * 1000);
  218. let arr:number[] = [];
  219. arr.push(date.getFullYear());
  220. arr.push(date.getMonth()+1);
  221. arr.push(date.getDate());
  222. return arr;// (date.getMonth() + 1).toString() + "月" + date.getDate() + "日";
  223. }
  224. /**
  225. * 秒转小时
  226. */
  227. public static timeSecToHours(sec: number): number {
  228. return sec / 3600;
  229. }
  230. }
  231. (<any>window).DateUtil = DateUtil;