date.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var validator = require('utils/validator');
  19. module.exports = {
  20. dateMonths:['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
  21. dateDays:['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
  22. dateFormatZeroFirst:function (time) {
  23. if (time < 10) return '0' + time;
  24. return time;
  25. },
  26. /**
  27. * Convert timestamp to date-string 'DAY_OF_THE_WEEK, MONTH DAY, YEAR HOURS:MINUTES'
  28. * @param timestamp
  29. * @return string date
  30. */
  31. dateFormat:function (timestamp) {
  32. if (!validator.isValidInt(timestamp)) return timestamp;
  33. var date = new Date(timestamp * 1);
  34. var months = this.dateMonths;
  35. var days = this.dateDays;
  36. return days[date.getDay()] + ', ' + months[date.getMonth()] + ' ' + this.dateFormatZeroFirst(date.getDate()) + ', ' + date.getFullYear() + ' ' + this.dateFormatZeroFirst(date.getHours()) + ':' + this.dateFormatZeroFirst(date.getMinutes());
  37. },
  38. /**
  39. * Convert date-string 'DAY_OF_THE_WEEK, MONTH DAY, YEAR HOURS:MINUTES' to timestamp
  40. * @param date_string
  41. * @return {String}
  42. */
  43. dateUnformat: function(date_string) {
  44. var date = date_string.substring(4);
  45. var month = date.substring(1, 4);
  46. var day = date.substring(5, 7);
  47. var year = date.substring(9, 13);
  48. var hours = date.substring(14, 16);
  49. var minutes = date.substring(17, 19);
  50. var months = this.dateMonths;
  51. month = months.indexOf(month) + 1;
  52. if (month < 10) month = '0' + month;
  53. return year + month + day + hours + minutes;
  54. },
  55. /**
  56. * Convert timestamp to date-string 'DAY_OF_THE_WEEK MONTH DAY YEAR'
  57. * @param timestamp
  58. * @return {*}
  59. */
  60. dateFormatShort: function(timestamp) {
  61. if (!validator.isValidInt(timestamp)) return timestamp;
  62. var date = new Date(timestamp*1);
  63. var today = new Date();
  64. if (date.toDateString() === today.toDateString()) {
  65. return 'Today ' + date.toLocaleTimeString();
  66. }
  67. return date.toDateString();
  68. },
  69. /**
  70. * Convert date-string 'DAY_OF_THE_WEEK MONTH DAY YEAR' to the timestamp
  71. * @param date_string
  72. * @return {Number}
  73. */
  74. dateUnformatShort: function(date_string) {
  75. var date = new Date(date_string);
  76. return date.getTime();
  77. },
  78. /**
  79. * Convert time in mseconds to 'HOURS:MINUTES:SECONDS'
  80. * @param ms_interval
  81. * @return string formatted date
  82. */
  83. dateFormatInterval:function (ms_interval) {
  84. if (!validator.isValidInt(ms_interval)) return ms_interval;
  85. var hours = Math.floor(ms_interval / (60 * 60000));
  86. var divisor_for_minutes = ms_interval % (60 * 60000);
  87. var minutes = Math.floor(divisor_for_minutes / 60000);
  88. var divisor_for_seconds = divisor_for_minutes % 60000;
  89. var seconds = (divisor_for_seconds / 1000).toFixed(2);
  90. return (hours < 10 ? '0' : '') + hours + ':' + (minutes < 10 ? '0' : '') + minutes + ':' + (seconds < 10 ? '0' : '') + seconds;
  91. },
  92. /**
  93. * Convert 'HOURS:MINUTES:SECONDS' to time in mseconds
  94. * @param formattedDate date string
  95. * @return time in mseconds
  96. */
  97. dateUnformatInterval: function(formattedDate) {
  98. var formattedDateArray = formattedDate.split(' ');
  99. if (Object.prototype.toString.call( formattedDateArray ) === '[object Array]' && formattedDateArray.length == 2) {
  100. var oneMinMs = 60000;
  101. var oneHourMs = 3600000;
  102. var oneDayMs = 86400000;
  103. if (formattedDateArray['1'] == 'ms') {
  104. return formattedDateArray['0'];
  105. } else if (formattedDateArray['1'] == 'secs') {
  106. return formattedDateArray['0'] * 1000;
  107. } else if (formattedDateArray['1'] == 'mins') {
  108. return formattedDateArray['0'] * oneMinMs;
  109. } else if (formattedDateArray['1'] == 'hours') {
  110. return formattedDateArray['0'] * oneHourMs;
  111. } else if (formattedDateArray['1'] == 'days') {
  112. return formattedDateArray['0'] * oneDayMs;
  113. } else {
  114. console.warn('function dateUnformatInterval: Undefined format');
  115. }
  116. } else {
  117. console.warn('function dateUnformatInterval: formattedDateArray');
  118. }
  119. },
  120. /**
  121. * Convert time in mseconds to
  122. * 30 ms = 30 ms
  123. * 300 ms = 300 ms
  124. * 999 ms = 999 ms
  125. * 1000 ms = 1.00 secs
  126. * 3000 ms = 3.00 secs
  127. * 35000 ms = 35.00 secs
  128. * 350000 ms = 350.00 secs
  129. * 999999 ms = 999.99 secs
  130. * 1000000 ms = 16.66 mins
  131. * 3500000 secs = 58.33 mins
  132. * @param time
  133. * @return string formatted date
  134. */
  135. timingFormat:function (time) {
  136. var intTime = parseInt(time);
  137. var timeStr = intTime.toString();
  138. var lengthOfNumber = timeStr.length;
  139. var oneMinMs = 60000;
  140. var oneHourMs = 3600000;
  141. var oneDayMs = 86400000;
  142. if (lengthOfNumber < 4) {
  143. return time + ' ms';
  144. } else if (lengthOfNumber < 7) {
  145. time = (time / 1000).toFixed(2);
  146. return time + ' secs';
  147. } else if (time < oneHourMs) {
  148. time = (time / oneMinMs).toFixed(2);
  149. return time + ' mins';
  150. } else if (time < oneDayMs) {
  151. time = (time / oneHourMs).toFixed(2);
  152. return time + ' hours';
  153. } else {
  154. time = (time / oneDayMs).toFixed(2);
  155. return time + ' days';
  156. }
  157. }
  158. }