DateAndTimeUtil.js 3.1 KB
Newer Older
J
jiyong_sd 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
/*
 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

const twelve = 12;

/*
 * date package tool class
 */
export default class DateAndTimeUtil {
  constructor() {
  }

  /**
   *
   * Get the current time
   */
  now() {
    const datetime = new Date();
    const hours = datetime.getHours();
    const minutes = datetime.getMinutes();
    return this.concatTime(hours, minutes);
  }

  /**
   *
   * Get the current time
   */
  nowWithSeconds() {
    const datetime = new Date();
    const hours = datetime.getHours();
    const minutes = datetime.getMinutes();
    const seconds = datetime.getMilliseconds()
    console.info('now: ' + hours + ':' + minutes + ':' + seconds)
    var now = this.concatTimeWithSeconds(hours, minutes, seconds);
    console.info('now concat: ' + now);
    return now;
  }

  /**
   * format
   * @param value
   * @return
   */
  fill(value) {
    return (value > 9 ? '' : '0') + value;
  }

  /**
   * concat date
   * @param year m d
   * @return
   */
  concatDate(year, month, date) {
    return year + this.year + month + this.month + date + this.day;
  }

  concatTime(hours, minutes) {
    return `${this.fill(hours)}:${this.fill(minutes)}`;
  }

  concatTimeWithSeconds(hours, minutes, milliseconds) {
    return `${this.fill(hours)}:${this.fill(minutes)}:${this.fill(milliseconds)}`;
  }

  /**
   * Turn to 24-hour clock
   * @param str
   * @return
   */
  transform24(str) {
    const timeFlag = str.substr(0, 2);
    if (timeFlag == this.morning) {
      const h = str.substr(2).split(':')[0];
      if (h == twelve) {
        const time = '0' + ':' + str.substr(2).split(':')[1];
        return time;
      } else {
        return h + ':' + str.substr(2).split(':')[1];
      }
    } else {
      const h = str.substr(2).split(':')[0];
      const h1 = parseInt(h) + twelve;
      if (h != twelve) {
        const time = h1 + ':' + str.substr(2).split(':')[1];
        return time;
      }
    }
  }

  /**
   * Turn to 12-hour clock
   * @param str
   * @return
   */
  transform12(str) {
    const hours = str.substring(0, str.indexOf(':'));
    const minutes = str.split(':')[1];
    if (hours < twelve) {
      return this.morning.concat(`${hours}:${minutes}`);
    }
    if (hours == twelve) {
      return this.afternoon.concat(`${hours}:${minutes}`);
    } else {
      const reduceHours = parseInt(hours) - twelve;
      return this.afternoon.concat(`${reduceHours}:${minutes}`);
    }
  }
}