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