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