• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 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
16/**
17 * @file: 日期工具
18 */
19export default class DateTimeUtil {
20  /**
21   * 时分秒
22   *
23   * @return {string} - 返回时分秒
24   */
25  getTime() {
26    const DATETIME = new Date();
27    const HOURS = DATETIME.getHours();
28    const MINUTES = DATETIME.getMinutes();
29    const SECONDS = DATETIME.getSeconds();
30    return this.concatTime(HOURS, MINUTES, SECONDS);
31  }
32
33  /**
34   * 年月日
35   *
36   * @return {string} - 返回年月日
37   */
38  getDate() {
39    const DATETIME = new Date();
40    const YEAR = DATETIME.getFullYear();
41    const MONTH = DATETIME.getMonth() + 1;
42    const DAY = DATETIME.getDate();
43    return this.concatDate(YEAR, MONTH, DAY);
44  }
45
46  /**
47   * 日期不足两位补 0
48   *
49   * @param {string} value - 数据值
50   * @return {string} - 日期不足两位补 0
51   */
52  fill(value): string {
53    return (value > 9 ? '' : '0') + value;
54  }
55
56  /**
57   * 年月日格式修饰
58   *
59   * @param {string} year - 年
60   * @param {string} month - 月
61   * @param {string} date - 日
62   * @return {string} - 年月日格式修饰
63   */
64  concatDate(year, month, date) {
65    return `${year}${month}${date}`;
66  }
67
68  /**
69   * 时分秒格式修饰
70   *
71   * @param {string} hours - 时
72   * @param {string} minutes - 分
73   * @param {string} seconds - 秒
74   * @return {string} - 时分秒格式修饰
75   */
76  concatTime(hours, minutes, seconds) {
77    return `${this.fill(hours)}${this.fill(minutes)}${this.fill(seconds)}`;
78  }
79}
80