1/*
2 * Copyright (c) 2023 Hunan OpenValley Digital Industry Development 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 */
19
20const NINE = 9; // 这是数字9
21
22export default class DateTimeUtil {
23  /**
24   * 时分秒
25   */
26  getTime(): string {
27    const DATETIME = new Date();
28    return this.concatTime(
29      DATETIME.getHours(),
30      DATETIME.getMinutes(),
31      DATETIME.getSeconds()
32    );
33  }
34
35  /**
36   * 年月日
37   */
38  getDate(): string {
39    const DATETIME = new Date();
40    return this.concatDate(
41      DATETIME.getFullYear(),
42      DATETIME.getMonth() + 1,
43      DATETIME.getDate()
44    );
45  }
46
47  /**
48   * 日期不足两位补充0
49   * @param value-数据值
50   */
51  fill(value: number): string {
52    return (value > NINE ? '' : '0') + value;
53  }
54
55  /**
56   * 年月日格式修饰
57   * @param year
58   * @param month
59   * @param date
60   */
61  concatDate(year: number, month: number, date: number): string {
62    return `${year}${this.fill(month)}${this.fill(date)}`;
63  }
64
65  /**
66   * 时分秒格式修饰
67   * @param hours
68   * @param minutes
69   * @param seconds
70   */
71  concatTime(hours: number, minutes: number, seconds: number): string {
72    return `${this.fill(hours)}${this.fill(minutes)}${this.fill(seconds)}`;
73  }
74}
75