• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16const twelve = 12;
17
18/*
19 * date package tool class
20 */
21export default class DateAndTimeUtil {
22  constructor() {
23  }
24
25  /**
26   *
27   * Get the current time
28   */
29  now() {
30    const datetime = new Date();
31    const hours = datetime.getHours();
32    const minutes = datetime.getMinutes();
33    return this.concatTime(hours, minutes);
34  }
35
36  /**
37   *
38   * Get the current time
39   */
40  nowWithSeconds() {
41    const datetime = new Date();
42    const hours = datetime.getHours();
43    const minutes = datetime.getMinutes();
44    const seconds = datetime.getMilliseconds()
45    console.info('now: ' + hours + ':' + minutes + ':' + seconds)
46    var now = this.concatTimeWithSeconds(hours, minutes, seconds);
47    console.info('now concat: ' + now);
48    return now;
49  }
50
51  /**
52   * format
53   * @param value
54   * @return
55   */
56  fill(value) {
57    return (value > 9 ? '' : '0') + value;
58  }
59
60  /**
61   * concat date
62   * @param year m d
63   * @return
64   */
65  concatDate(year, month, date) {
66    return year + this.year + month + this.month + date + this.day;
67  }
68
69  concatTime(hours, minutes) {
70    return `${this.fill(hours)}:${this.fill(minutes)}`;
71  }
72
73  concatTimeWithSeconds(hours, minutes, milliseconds) {
74    return `${this.fill(hours)}:${this.fill(minutes)}:${this.fill(milliseconds)}`;
75  }
76
77  /**
78   * Turn to 24-hour clock
79   * @param str
80   * @return
81   */
82  transform24(str) {
83    const timeFlag = str.substr(0, 2);
84    if (timeFlag == this.morning) {
85      const h = str.substr(2).split(':')[0];
86      if (h == twelve) {
87        const time = '0' + ':' + str.substr(2).split(':')[1];
88        return time;
89      } else {
90        return h + ':' + str.substr(2).split(':')[1];
91      }
92    } else {
93      const h = str.substr(2).split(':')[0];
94      const h1 = parseInt(h) + twelve;
95      if (h != twelve) {
96        const time = h1 + ':' + str.substr(2).split(':')[1];
97        return time;
98      }
99    }
100  }
101
102  /**
103   * Turn to 12-hour clock
104   * @param str
105   * @return
106   */
107  transform12(str) {
108    const hours = str.substring(0, str.indexOf(':'));
109    const minutes = str.split(':')[1];
110    if (hours < twelve) {
111      return this.morning.concat(`${hours}:${minutes}`);
112    }
113    if (hours == twelve) {
114      return this.afternoon.concat(`${hours}:${minutes}`);
115    } else {
116      const reduceHours = parseInt(hours) - twelve;
117      return this.afternoon.concat(`${reduceHours}:${minutes}`);
118    }
119  }
120}
121