Variable timeUtilsConst

timeUtils: {
    beforeOrAfterDay(dateString): string;
    chineseDate(dateTime?, isYear?): string;
    endTime(dateTime): string;
    isAfterNow(dateString): boolean;
    nowFullTime(): string;
    nowTimestamp(isUnix?): number;
    startTime(dateTime): string;
    timeFormat(dateTime?, formatStr?, emptyResult?): string;
    timeFrom(date?, format?, emptyResult?): string;
    toDate(dateTime?): Date;
    toTimestamp(dateTime, isUnix?): number;
} = ...

主要概念: 时间:date, new Date(...), 其实也就是某一时刻 时间戳:Timestamp, 以毫秒数字表示。一个时间戳对应的其实也就是一个时刻 unix 时间戳精确到秒,为10位。其他精确到毫秒,为13位 时间字符:可能为时间,也可能为时间戳

Type declaration

  • beforeOrAfterDay:function
    • 根据当前时间计算出传递的时间是几天前或者几天后

      Parameters

      • dateString: string

        时间字符串

      Returns string

      计算后的返回结果

      Example

      timeUtils.beforeOrAfterDay('2024-10-21 23:11:11') // 3天前
      timeUtils.beforeOrAfterDay('2024-10-24 23:11:11') // 今天
      timeUtils.beforeOrAfterDay('2024-11-24 23:11:11') // 31天后
  • chineseDate:function
    • 转时间加文字 例:(2022-12-01 转 2022年12月01日) || (12-01 转 12月01日);

      Parameters

      • dateTime: undefined | null | string | number | Date = null

        时间戳,时间字符串(仅支持 转 年月日字符)

      • isYear: boolean = true

        是否有年 默认为true 转 2022年10月12日, false 转 10月12日

      Returns string

      返回格式化后的字符串

      Example

      timeUtils.chineseDate('2023-12-01',false) // 12月01日
      timeUtils.chineseDate('2023-12-01') // 2023年12月01日
  • endTime:function
    • 年月日 + 23:59:59

      Parameters

      • dateTime: undefined | null | string | number | Date

        date不传或传入null 表示取当前时间

      Returns string

      年月日 + 23:59:59

      Example

      timeUtils.endTime('2024-02-15 15:12:18') // 2024-02-15 23:59:59
      timeUtils.startTime('') // 2024-08-14 23:59:59
  • isAfterNow:function
    • 当前时间和传递的时间进行比较,传递的时间大于当前时间返回true 小于则返回false

      Parameters

      • dateString: string

        时间字符串

      Returns boolean

      Example

      timeUtils.isAfterNow('2024-10-21 23:11:11') // false
      timeUtils.isAfterNow('2024-10-24 23:11:11') // true
      timeUtils.isAfterNow('2024-11-24 23:11:11') // true
  • nowFullTime:function
    • 当前时间的完整显示 timeFormat(null, "yyyy-mm-dd hh:MM:ss")

      Returns string

      yyyy-mm-dd hh:MM:ss 格式时间

      Example

      timeUtils.nowFullTime() // 2024-01-01 17:11:35
      
  • nowTimestamp:function
    • 当前时间时间戳

      Parameters

      • isUnix: boolean = false

        普通的为 13位(包含毫秒); unix 的为10位,不包含毫秒

      Returns number

      时间戳数值

      Example

      timeUtils.nowTimestamp() // 1723624829143
      timeUtils.nowTimestamp(false) // 1723624829
  • startTime:function
    • 年月日 + 00:00:00

      Parameters

      • dateTime: undefined | null | string | number | Date

        date不传或传入null 表示取当前时间

      Returns string

      年月日 + 00:00:00

      Example

      timeUtils.startTime('1710486738911') // 2024-03-15 00:00:00
      timeUtils.startTime('') // 2024-08-14 00:00:00
  • timeFormat:function
    • 格式化时间,输出时间字符串, yyyy-mm-dd hh:MM:ss

      Parameters

      • dateTime: undefined | null | string | number | Date = null

        时间,时间字符串,时间戳,时间戳字符串都可以。date不传或传入null 表示取当前时间

      • formatStr: string = 'yyyy-mm-dd'

        格式化规则 yyyy:mm:dd|yyyy:mm|yyyy年mm月dd日|yyyy年mm月dd日 hh时MM分等,可自定义组合 默认yyyy-mm-dd。yyyy-mm-dd hh:MM:ss 显示时分秒

      • emptyResult: string = "-"

      Returns string

      返回格式化后的字符串

      Example

      timeUtils.timeFormat('1710486738911','yyyy-mm-dd hh:MM:ss') // 2024-03-15 15:12:18
      timeUtils.timeFormat('1710486738911','mm-dd hh:MM') // 03-15 15:12
      timeUtils.timeFormat('1710486738911','yyyy年mm月dd日 hh时MM分') // 2024年03月15日 15时12分
  • timeFrom:function
    • 距离现在多久

      Parameters

      • date: undefined | null | string | number | Date = null

        时间,时间字符串,时间戳,时间戳字符串都可以。date不传或传入null 表示取当前时间

      • format: string = 'yyyy-mm-dd'

        格式化规则如果为时间格式字符串,超出一定时间范围,返回固定的时间格式; 如果为布尔值false,无论什么时间,都返回多久以前的格式

      • emptyResult: string = "-"

      Returns string

      转化后的内容

      Example

      timeUtils.timeFrom(timeUtils.nowFullTime()) // 刚刚
      timeUtils.timeFrom(timeUtils.nowTimestamp() - 3600000) // 1小时前
  • toDate:function
    • 转时间

      Parameters

      • Optional dateTime: null | string | number | Date

        时间,时间字符串,时间戳,时间戳字符串都可以 date不传或传入null 表示取当前时间

      Returns Date

      Example

      timeUtils.toDate('1710486738911') // 2024-03-15T07:12:18.911Z
      timeUtils.toDate(null) // 2024-08-14T08:44:22.991Z
  • toTimestamp:function
    • 转时间戳

      Parameters

      • dateTime: undefined | null | string | number | Date

        时间,时间字符串,时间戳,时间戳字符串都可以 date不传或传入null 表示取当前时间

      • isUnix: boolean = false

        是否为unix格式

      Returns number

      Example

      timeUtils.toTimestamp(null) // 1723625151828
      timeUtils.toTimestamp(null,true) // 1723625151
      timeUtils.toTimestamp('2024-02-15 15:12:18') // 1707981138000

Generated using TypeDoc