date.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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);
  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 timestamp to date-string 'DAY_OF_THE_WEEK MONTH DAY YEAR'
  40. * @param timestamp
  41. * @return {*}
  42. */
  43. dateFormatShort: function(timestamp) {
  44. if (!validator.isValidInt(timestamp)) return timestamp;
  45. var date = new Date(timestamp);
  46. var today = new Date();
  47. if (date.toDateString() === today.toDateString()) {
  48. return 'Today ' + date.toLocaleTimeString();
  49. }
  50. return date.toDateString();
  51. },
  52. /**
  53. * Convert starTimestamp to 'DAY_OF_THE_WEEK, MONTH DAY, YEAR HOURS:MINUTES, lasted for DURATION', except for the case: year equals 1969
  54. * @param startTimestamp
  55. * @return string startTimeSummary
  56. */
  57. startTime: function (startTimestamp) {
  58. if (!validator.isValidInt(startTimestamp)) return '';
  59. var startDate = new Date(startTimestamp);
  60. var months = this.dateMonths;
  61. var days = this.dateDays;
  62. // generate start time
  63. if (startDate.getFullYear() == 1969 || startTimestamp < 1) {
  64. return 'Not started';
  65. }
  66. var startTimeSummary = '';
  67. if (new Date(startTimestamp).setHours(0, 0, 0, 0) == new Date().setHours(0, 0, 0, 0) ) { //today
  68. startTimeSummary = 'Today ' + this.dateFormatZeroFirst(startDate.getHours()) + ':' + this.dateFormatZeroFirst(startDate.getMinutes());
  69. } else {
  70. startTimeSummary = days[startDate.getDay()] + ' ' + months[startDate.getMonth()] + ' ' + this.dateFormatZeroFirst(startDate.getDate()) + ' ' + startDate.getFullYear() + ' '
  71. + this.dateFormatZeroFirst(startDate.getHours()) + ':' + this.dateFormatZeroFirst(startDate.getMinutes());
  72. }
  73. return startTimeSummary;
  74. },
  75. /**
  76. * Provides the duration between the given start and end timestamp. If start time
  77. * not valid, duration will be ''. If end time is not valid, duration will
  78. * be till now, showing 'Lasted for xxx secs'.
  79. * @param startTimestamp
  80. * @param endTimestamp
  81. * @return string durationSummary
  82. */
  83. durationSummary: function (startTimestamp, endTimestamp) {
  84. // generate duration
  85. var durationSummary = '';
  86. var startDate = new Date(startTimestamp);
  87. var endDate = new Date(endTimestamp);
  88. if (startDate.getFullYear() == 1969 || startTimestamp < 1) {
  89. return '';
  90. }
  91. if (endDate.getFullYear() != 1969 && endTimestamp > 0) {
  92. durationSummary = '' + this.timingFormat(endTimestamp - startTimestamp, 1); //lasted for xx secs
  93. } else {
  94. durationSummary = '' + this.timingFormat(new Date().getTime() - startTimestamp, 1);
  95. }
  96. return durationSummary;
  97. },
  98. /**
  99. * Convert time in mseconds to
  100. * 30 ms = 30 ms
  101. * 300 ms = 300 ms
  102. * 999 ms = 999 ms
  103. * 1000 ms = 1.00 secs
  104. * 3000 ms = 3.00 secs
  105. * 35000 ms = 35.00 secs
  106. * 350000 ms = 350.00 secs
  107. * 999999 ms = 999.99 secs
  108. * 1000000 ms = 16.66 mins
  109. * 3500000 secs = 58.33 mins
  110. * @param time
  111. * @param zeroValid for the case to show 0 when time is 0, not null
  112. * @return string formatted date
  113. */
  114. timingFormat:function (time, /* optional */ zeroValid) {
  115. var intTime = parseInt(time);
  116. if (zeroValid && intTime == 0) return 0 + '';
  117. if (!intTime) return null;
  118. var timeStr = intTime.toString();
  119. var lengthOfNumber = timeStr.length;
  120. var oneMinMs = 60000;
  121. var oneHourMs = 3600000;
  122. var oneDayMs = 86400000;
  123. if (lengthOfNumber < 4) {
  124. return time + ' ms';
  125. } else if (lengthOfNumber < 7) {
  126. time = (time / 1000).toFixed(2);
  127. return time + ' secs';
  128. } else if (time < oneHourMs) {
  129. time = (time / oneMinMs).toFixed(2);
  130. return time + ' mins';
  131. } else if (time < oneDayMs) {
  132. time = (time / oneHourMs).toFixed(2);
  133. return time + ' hours';
  134. } else {
  135. time = (time / oneDayMs).toFixed(2);
  136. return time + ' days';
  137. }
  138. },
  139. /**
  140. * Provides the duration between the given start and end time. If start time
  141. * is not given, duration will be 0. If end time is not given, duration will
  142. * be till now.
  143. *
  144. * @param {Number} startTime Start time from epoch
  145. * @param {Number} endTime End time from epoch
  146. * @return {Number} duration
  147. */
  148. duration : function(startTime, endTime) {
  149. var duration = 0;
  150. if (startTime && startTime > 0) {
  151. if (!endTime || endTime < 1) {
  152. endTime = new Date().getTime();
  153. }
  154. duration = endTime - startTime;
  155. }
  156. return duration;
  157. }
  158. };