• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022 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   */
24  getTime() {
25    const DATETIME = new Date()
26    return this.concatTime(DATETIME.getHours(), DATETIME.getMinutes(), DATETIME.getSeconds())
27  }
28
29  /**
30   * 年月日
31   */
32  getDate() {
33    const DATETIME = new Date()
34    return this.concatDate(DATETIME.getFullYear(), DATETIME.getMonth() + 1, DATETIME.getDate())
35  }
36
37  /**
38   * 日期不足两位补充0
39   * @param value-数据值
40   */
41  fill(value: number) {
42    return (value > 9 ? '' : '0') + value
43  }
44
45  /**
46   * 年月日格式修饰
47   * @param year
48   * @param month
49   * @param date
50   */
51  concatDate(year: number, month: number, date: number) {
52    return `${year}${this.fill(month)}${this.fill(date)}`
53  }
54
55  /**
56   * 时分秒格式修饰
57   * @param hours
58   * @param minutes
59   * @param seconds
60   */
61  concatTime(hours: number, minutes: number, seconds: number) {
62    return `${this.fill(hours)}${this.fill(minutes)}${this.fill(seconds)}`
63  }
64}