date.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 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*1);
  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 time in mseconds to
  54. * 30 ms = 30 ms
  55. * 300 ms = 300 ms
  56. * 999 ms = 999 ms
  57. * 1000 ms = 1.00 secs
  58. * 3000 ms = 3.00 secs
  59. * 35000 ms = 35.00 secs
  60. * 350000 ms = 350.00 secs
  61. * 999999 ms = 999.99 secs
  62. * 1000000 ms = 16.66 mins
  63. * 3500000 secs = 58.33 mins
  64. * @param time
  65. * @param zeroValid, for the case to show 0 when time is 0, not null
  66. * @return string formatted date
  67. */
  68. timingFormat:function (time, /* optional */ zeroValid) {
  69. var intTime = parseInt(time);
  70. if (zeroValid && intTime == 0) return 0 + '';
  71. if (!intTime) return null;
  72. var timeStr = intTime.toString();
  73. var lengthOfNumber = timeStr.length;
  74. var oneMinMs = 60000;
  75. var oneHourMs = 3600000;
  76. var oneDayMs = 86400000;
  77. if (lengthOfNumber < 4) {
  78. return time + ' ms';
  79. } else if (lengthOfNumber < 7) {
  80. time = (time / 1000).toFixed(2);
  81. return time + ' secs';
  82. } else if (time < oneHourMs) {
  83. time = (time / oneMinMs).toFixed(2);
  84. return time + ' mins';
  85. } else if (time < oneDayMs) {
  86. time = (time / oneHourMs).toFixed(2);
  87. return time + ' hours';
  88. } else {
  89. time = (time / oneDayMs).toFixed(2);
  90. return time + ' days';
  91. }
  92. }
  93. }