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