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 16import {LogUtil} from './LogUtil' 17import GlobalResourceManager from './GlobalResourceManager' 18 19const TAG = "DateUtil" 20 21/** 22 * 日期和时间均显示为2位数 23 * @param time 24 */ 25function getUnifiedTime(time: number): any { 26 return time > 9 ? time : "0" + time 27} 28 29/** 30 * 日期时间工具类, 中文暂时不写到string.json, $r("")取出来的是Resource, 转化为string困难 31 */ 32export class DateUtil { 33 /** 34 * 获取笔记标题的格式化时间 35 * @param date note modified time 36 */ 37 formateDateForNoteTitle(date: Date): string{ 38 let nowDate = new Date() 39 let diffMin = Math.floor((nowDate.getTime() - date.getTime()) / 1000 / 60) 40 if (diffMin < 0) { 41 LogUtil.info(TAG, "DateUtils, formateDateForNoteTitle : date is error") 42 return this.formateDateForNoteContent(date) 43 } 44 45 let noteTitletTime = "" 46 let year = date.getFullYear() 47 let month = date.getMonth() 48 let day = date.getDate() 49 let hours = date.getHours() 50 let mins = date.getMinutes() 51 52 let nowYear = nowDate.getFullYear() 53 let nowMonth = nowDate.getMonth() 54 let nowDay = nowDate.getDate() 55 56 if (diffMin < 1) { 57 // 1分钟内, 显示“刚刚” 58 noteTitletTime = "刚刚" 59 } else if (diffMin < 60) { 60 // 1小时内, 显示“X分钟前” 61 noteTitletTime = diffMin + "分钟前" 62 } else if (nowYear === year && nowMonth === month && nowDay === day) { 63 // 今天, 显示“hh:mm” 64 noteTitletTime = getUnifiedTime(hours) + ":" + getUnifiedTime(mins) 65 } else if (nowYear === year && nowMonth === month && nowDay === (day + 1)) { 66 // 昨天, 场景1: 2022年1月1日 - 2022年1月2日, 显示“昨天” 67 noteTitletTime = "昨天" 68 } else if (nowYear === year && nowDay === 1) { 69 // 昨天, 场景2: 2022年1月31日 - 2022年2月1日, 显示“昨天” 70 let now = new Date() 71 now.setDate(0) // 设置为上个月的最后一天 72 noteTitletTime = now.getDate() === day ? "昨天" : "" 73 } else if (nowYear === year + 1 && nowDay === 1) { 74 // 昨天, 场景3: 2022年12月31日 - 2023年1月1日, 显示“昨天” 75 let now = new Date() 76 now.setMonth(-1) // 设置为去年的最后一个月 77 noteTitletTime = now.getMonth() === month ? "昨天" : "" 78 } else if (nowYear === year) { 79 // 今年, 显示“X月X日” 80 noteTitletTime = getUnifiedTime(month + 1) + "月" + getUnifiedTime(day) + "日" 81 } else { 82 // 不是今年, 显示“X年X月X日” 83 noteTitletTime = year + "年" + getUnifiedTime(month + 1) + "月" + getUnifiedTime(day) + "日" 84 } 85 return noteTitletTime 86 } 87 88 /** 89 * 获取笔记内容的格式化时间 90 * @param date note modified time 91 */ 92 formateDateForNoteContent(date: Date): string{ 93 // formatted time 94 let noteContentTime = "" 95 // note modified time 96 let year = date.getFullYear() 97 let month = date.getMonth() 98 let day = date.getDate() 99 let hours = date.getHours() 100 let mins = date.getMinutes() 101 // now date 102 let nowDate = new Date() 103 let nowYear = nowDate.getFullYear() 104 let nowMonth = nowDate.getMonth() 105 let nowDay = nowDate.getDate() 106 if (nowYear === year && nowMonth === month && nowDay === day) { 107 // 今天, 显示“hh:mm” 108 noteContentTime = "今天" + " " + getUnifiedTime(hours) + ":" + getUnifiedTime(mins) 109 } else if (nowYear === year) { 110 // 今年, 显示“X月X日 hh:mm” 111 noteContentTime = getUnifiedTime(month + 1) + "月" + getUnifiedTime(day) + "日" 112 + " " + getUnifiedTime(hours) + ":" + getUnifiedTime(mins) 113 } else { 114 // 不是今年, 显示“X年X月X日 hh:mm” 115 noteContentTime = year + "年" + getUnifiedTime(month + 1) + "月" + getUnifiedTime(day) + "日" 116 + " " + getUnifiedTime(hours) + ":" + getUnifiedTime(mins) 117 } 118 return noteContentTime 119 } 120} 121 122let dateUtil = new DateUtil() 123 124export default dateUtil as DateUtil