• 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  getTime() {
24    const DATETIME = new Date()
25    return this.concatTime(DATETIME.getHours(), DATETIME.getMinutes(), DATETIME.getSeconds())
26  }
27
28  getHour() {
29    const DATETIME = new Date()
30    return DATETIME.getHours()
31  }
32
33  getMinute() {
34    const DATETIME = new Date()
35    return DATETIME.getMinutes()
36  }
37
38  getSecond() {
39    const DATETIME = new Date()
40    return DATETIME.getSeconds()
41  }
42
43  /**
44   * 年月日
45   */
46  getDate() {
47    const DATETIME = new Date()
48    return this.concatDate(DATETIME.getFullYear(), DATETIME.getMonth() + 1, DATETIME.getDate())
49  }
50
51  getFullYear() {
52    const DATETIME = new Date()
53    return DATETIME.getFullYear()
54  }
55
56  getMonth() {
57    const DATETIME = new Date()
58    return DATETIME.getMonth() + 1
59  }
60
61  getDay() {
62    const DATETIME = new Date()
63    return DATETIME.getDate()
64  }
65
66  /**
67   * 日期不足两位补充0
68   * @param value-数据值
69   */
70  fill(value: number) {
71    return (value > 9 ? '' : '0') + value
72  }
73
74  /**
75   * 年月日格式修饰
76   * @param year
77   * @param month
78   * @param date
79   */
80  concatDate(year: number, month: number, date: number) {
81    return `${year}${this.fill(month)}${this.fill(date)}`
82  }
83
84  /**
85   * 时分秒格式修饰
86   * @param hours
87   * @param minutes
88   * @param seconds
89   */
90  concatTime(hours: number, minutes: number, seconds: number) {
91    return `${this.fill(hours)}${this.fill(minutes)}${this.fill(seconds)}`
92  }
93}