1/* 2 * Copyright (c) 2023-2023 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 16import { DateTimeFormat } from '../model/PrintBean'; 17 18/** 19 * Date utils 20 */ 21export class DateUtils { 22 private static readonly NUMBER_TEN: Number = 10; 23 24 /** 25 * get curr date time 26 */ 27 public static getCurrTime(): string { 28 return DateUtils.getCurrentDate(DateTimeFormat.DATE_TIME); 29 } 30 31 /** 32 * get curr date 33 * 34 * @param format DATE_TIME_FORMAT 35 */ 36 public static getCurrentDate(format: DateTimeFormat): string { 37 let now = new Date(); 38 39 let year = now.getFullYear(); //得到年份 40 let month = now.getMonth() + 1; //得到月份 41 let date = now.getDate(); //得到日期 42 let hour = now.getHours(); //得到小时 43 let minus = now.getMinutes(); //得到分钟 44 let sec = now.getSeconds(); //得到秒 45 46 let currMonth = DateUtils.completionZero(month); 47 let currDate = DateUtils.completionZero(date); 48 let currHour = DateUtils.completionZero(hour); 49 let currMinus = DateUtils.completionZero(minus); 50 let currSec = DateUtils.completionZero(sec); 51 52 let time = ''; 53 switch (format) { 54 case DateTimeFormat.DATE: 55 time = year + '-' + currMonth + '-' + currDate; 56 break; 57 case DateTimeFormat.TIME: 58 time = currHour + ':' + currMinus + ':' + currSec; 59 break; 60 case DateTimeFormat.DATE_TIME: 61 time = year + '-' + currMonth + '-' + currDate + ' ' + currHour + ':' + currMinus + ':' + currSec; 62 break; 63 default: 64 break; 65 } 66 return time; 67 } 68 69 static completionZero(data: number): string { 70 let currDate = data.toString(); 71 if (data < DateUtils.NUMBER_TEN) { 72 currDate = '0' + data; 73 } 74 return currDate; 75 } 76}