shishuangxi

增加文章发布时间工具转换

... ... @@ -6,12 +6,12 @@ import { DateTimeUtils } from 'wdKit'
@Component
export struct TitleAbbrComponent {
@State compDTO: CompDTO = {} as CompDTO
@State @Watch('isPoint') source:boolean=false;//来源是否为空 publishTime 字段是否为空
@State isPo:number=Visibility.Visible;
@State @Watch('isPoint') source: boolean = false; //来源是否为空 publishTime 字段是否为空
@State isPo: number = Visibility.Visible;
isPoint(){
if(this.source){
this.isPo=Visibility.None
isPoint() {
if (this.source) {
this.isPo = Visibility.None
}
}
... ... @@ -39,7 +39,7 @@ export struct TitleAbbrComponent {
.height(16)
.visibility(this.isPo)
Text(DateTimeUtils.formatDate(Number.parseFloat(this.compDTO.operDataList[0].publishTime)))
Text(DateTimeUtils.getCommentTime(Number.parseFloat(this.compDTO.operDataList[0].publishTime)))
.fontSize($r("app.float.font_size_12"))
.fontColor($r("app.color.color_B0B0B0"))
.fontWeight(400)
... ... @@ -59,12 +59,12 @@ export struct TitleAbbrComponent {
.margin({ bottom: 8 })
}
aboutToAppear(){
if(this.compDTO.operDataList[0].source==""){
this.source=true;
aboutToAppear() {
if (this.compDTO.operDataList[0].source == "") {
this.source = true;
}
if(this.compDTO.operDataList[0].publishTime==""){
this.source=true;
if (this.compDTO.operDataList[0].publishTime == "") {
this.source = true;
}
}
}
\ No newline at end of file
... ...
... ... @@ -7,12 +7,12 @@ import { Logger } from 'wdKit/src/main/ets/utils/Logger'
@Component
export struct TitleAllComponent {
@State compDTO: CompDTO = {} as CompDTO
@State @Watch('isPoint') source:boolean=false;//来源是否为空 publishTime 字段是否为空
@State isPo:number=Visibility.Visible;
@State @Watch('isPoint') source: boolean = false; //来源是否为空 publishTime 字段是否为空
@State isPo: number = Visibility.Visible;
isPoint(){
if(this.source){
this.isPo=Visibility.None
isPoint() {
if (this.source) {
this.isPo = Visibility.None
}
}
... ... @@ -38,7 +38,7 @@ export struct TitleAllComponent {
.height(16)
.visibility(this.isPo)
Text(DateTimeUtils.formatDate(Number.parseFloat(this.compDTO.operDataList[0].publishTime)))
Text(DateTimeUtils.getCommentTime(Number.parseFloat(this.compDTO.operDataList[0].publishTime)))
.fontSize($r("app.float.font_size_12"))
.fontColor($r("app.color.color_B0B0B0"))
.fontWeight(400)
... ...
... ... @@ -50,6 +50,7 @@ export class DateTimeUtils {
static readonly PATTERN_DATE_CN: string = 'yyyy年MM月dd日'; // 日期中包含包含中文年月日
static readonly PATTERN_DATE_SLASH_WITHOUT_YEAR: string = 'MM/dd'; // 日期中不包含年份
static readonly PATTERN_DATE_CN_WITHOUT_YEAR: string = 'MM月dd日'; // 日期中不包含年份,且month与day是中文
static readonly PATTERN_DATE_SLASH_WITHOUT_YEAR2: string = 'MM-dd'; // 日期中不包含年份
// 仅时间格式(不包含日期)
static readonly PATTERN_TIME_DEFAULT: string = 'HHmmss'; // 时分秒
... ... @@ -331,6 +332,33 @@ export class DateTimeUtils {
public static getCurrentTimeMillis(): number {
return new Date().getTime();
}
/**
* 获取文章发布时间
* */
public static getCommentTime(publishTime: number): string {
let currentTime: number = new Date().getTime();
let timeGap = currentTime - publishTime;
let timeStr = ""
if (timeGap >= 60 * 60 * 1000 * 48) {
let publishYear = new Date(publishTime).getFullYear();
let currentYear = new Date(currentTime).getFullYear();
if (publishYear == currentYear) {
timeStr = this.formatDate(publishTime, DateTimeUtils.PATTERN_DATE_SLASH_WITHOUT_YEAR2)
} else {
timeStr = this.formatDate(publishTime)
}
} else if (timeGap > 60 * 60 * 1000 * 24) {
timeStr = Math.floor(timeGap / (60 * 60 * 1000 * 24)) + "天前";
} else if (timeGap > 60 * 60 * 1000) { // 1小时-24小时
timeStr = Math.floor(timeGap / (60 * 60 * 1000)) + "小时前";
} else if (timeGap > 60 * 1000) { // 1分钟-59分钟
timeStr = Math.floor(timeGap / (60 * 1000)) + "分钟前";
} else { // 1秒钟-59秒钟
timeStr = "刚刚";
}
return timeStr;
}
}
// const dateTimeUtils = new DateTimeUtils()
\ No newline at end of file
... ...