宜搭+
    正在准备搜索索引...

    类 DateX

    扩展 Date 的日期时间运算类。

    DateX 在原生 Date 基础上增加了 DateX.addDateX.subtract 方法,支持按年、月、周、日、时、分、秒、 毫秒进行偏移计算。同时覆盖 DateX.toStringDateX.toJSON,输出统一的 YYYY-MM-DD HH:MM:SS 格式。

    26.4.13

    const now = new DateX("2026-07-01 12:00:00")
    const later = now.add({ days: 1, hours: 2 })
    console.log(later.toString()) // "2026-07-02 14:00:00"

    层级

    • Date
      • DateX
    索引

    构造函数

    • 返回 DateX

    • 参数

      • value: string | number

      返回 DateX

    • Creates a new Date.

      参数

      • year: number

        The full year designation is required for cross-century date accuracy. If year is between 0 and 99 is used, then year is assumed to be 1900 + year.

      • monthIndex: number

        The month as a number between 0 and 11 (January to December).

      • 可选date: number

        The date as a number between 1 and 31.

      • 可选hours: number

        Must be supplied if minutes is supplied. A number from 0 to 23 (midnight to 11pm) that specifies the hour.

      • 可选minutes: number

        Must be supplied if seconds is supplied. A number from 0 to 59 that specifies the minutes.

      • 可选seconds: number

        Must be supplied if milliseconds is supplied. A number from 0 to 59 that specifies the seconds.

      • 可选ms: number

        A number from 0 to 999 that specifies the milliseconds.

      返回 DateX

    • 参数

      • vd: VarDate

      返回 DateX

    • 参数

      • value: string | number | Date

      返回 DateX

    属性

    getVarDate: () => VarDate
    ONE_DAY: number = ...

    一日的毫秒数。

    常量值为 24 * 60 * 60 * 1000,即 86,400,000 毫秒。

    26.4.13

    ONE_WEEK: number = ...

    一周的毫秒数。

    常量值为 7 * ONE_DAY

    26.4.13

    方法

    • Converts a Date object to a string.

      参数

      • hint: "default"

      返回 string

    • Converts a Date object to a string.

      参数

      • hint: "string"

      返回 string

    • Converts a Date object to a number.

      参数

      • hint: "number"

      返回 number

    • Converts a Date object to a string or number.

      参数

      • hint: string

        The strings "number", "string", or "default" to specify what primitive to return.

      返回 string | number

      A number if 'hint' was "number", a string if 'hint' was "string" or "default".

      If 'hint' was given something other than "number", "string", or "default".

    • 返回在当前日期时间基础上加上指定时间段后的新 DateX 实例。

      该方法会归一化传入的 DateXDuration,支持标准字段名及其常用别名 (如 yearmonweekdayhourminsecmill 等)。 计算按“年 → 月 → 日 → 时 → 分 → 秒 → 毫秒”的顺序依次应用。

      参数

      返回 DateX

      增加后的新 DateX 实例;原实例不被修改。

      26.4.13

    • Gets the day-of-the-month, using local time.

      返回 number

    • Gets the day of the week, using local time.

      返回 number

    • Gets the year, using local time.

      返回 number

    • Gets the hours in a date, using local time.

      返回 number

    • Gets the milliseconds of a Date, using local time.

      返回 number

    • Gets the minutes of a Date object, using local time.

      返回 number

    • Gets the month, using local time.

      返回 number

    • Gets the seconds of a Date object, using local time.

      返回 number

    • Returns the stored time value in milliseconds since midnight, January 1, 1970 UTC.

      返回 number

    • Gets the difference in minutes between Universal Coordinated Time (UTC) and the time on the local computer.

      返回 number

    • Gets the day-of-the-month, using Universal Coordinated Time (UTC).

      返回 number

    • Gets the day of the week using Universal Coordinated Time (UTC).

      返回 number

    • Gets the year using Universal Coordinated Time (UTC).

      返回 number

    • Gets the hours value in a Date object using Universal Coordinated Time (UTC).

      返回 number

    • Gets the milliseconds of a Date object using Universal Coordinated Time (UTC).

      返回 number

    • Gets the minutes of a Date object using Universal Coordinated Time (UTC).

      返回 number

    • Gets the month of a Date object using Universal Coordinated Time (UTC).

      返回 number

    • Gets the seconds of a Date object using Universal Coordinated Time (UTC).

      返回 number

    • Sets the numeric day-of-the-month value of the Date object using local time.

      参数

      • date: number

        A numeric value equal to the day of the month.

      返回 number

    • Sets the year of the Date object using local time.

      参数

      • year: number

        A numeric value for the year.

      • 可选month: number

        A zero-based numeric value for the month (0 for January, 11 for December). Must be specified if numDate is specified.

      • 可选date: number

        A numeric value equal for the day of the month.

      返回 number

    • Sets the hour value in the Date object using local time.

      参数

      • hours: number

        A numeric value equal to the hours value.

      • 可选min: number

        A numeric value equal to the minutes value.

      • 可选sec: number

        A numeric value equal to the seconds value.

      • 可选ms: number

        A numeric value equal to the milliseconds value.

      返回 number

    • Sets the milliseconds value in the Date object using local time.

      参数

      • ms: number

        A numeric value equal to the millisecond value.

      返回 number

    • Sets the minutes value in the Date object using local time.

      参数

      • min: number

        A numeric value equal to the minutes value.

      • 可选sec: number

        A numeric value equal to the seconds value.

      • 可选ms: number

        A numeric value equal to the milliseconds value.

      返回 number

    • Sets the month value in the Date object using local time.

      参数

      • month: number

        A numeric value equal to the month. The value for January is 0, and other month values follow consecutively.

      • 可选date: number

        A numeric value representing the day of the month. If this value is not supplied, the value from a call to the getDate method is used.

      返回 number

    • Sets the seconds value in the Date object using local time.

      参数

      • sec: number

        A numeric value equal to the seconds value.

      • 可选ms: number

        A numeric value equal to the milliseconds value.

      返回 number

    • Sets the date and time value in the Date object.

      参数

      • time: number

        A numeric value representing the number of elapsed milliseconds since midnight, January 1, 1970 GMT.

      返回 number

    • Sets the numeric day of the month in the Date object using Universal Coordinated Time (UTC).

      参数

      • date: number

        A numeric value equal to the day of the month.

      返回 number

    • Sets the year value in the Date object using Universal Coordinated Time (UTC).

      参数

      • year: number

        A numeric value equal to the year.

      • 可选month: number

        A numeric value equal to the month. The value for January is 0, and other month values follow consecutively. Must be supplied if numDate is supplied.

      • 可选date: number

        A numeric value equal to the day of the month.

      返回 number

    • Sets the hours value in the Date object using Universal Coordinated Time (UTC).

      参数

      • hours: number

        A numeric value equal to the hours value.

      • 可选min: number

        A numeric value equal to the minutes value.

      • 可选sec: number

        A numeric value equal to the seconds value.

      • 可选ms: number

        A numeric value equal to the milliseconds value.

      返回 number

    • Sets the milliseconds value in the Date object using Universal Coordinated Time (UTC).

      参数

      • ms: number

        A numeric value equal to the millisecond value.

      返回 number

    • Sets the minutes value in the Date object using Universal Coordinated Time (UTC).

      参数

      • min: number

        A numeric value equal to the minutes value.

      • 可选sec: number

        A numeric value equal to the seconds value.

      • 可选ms: number

        A numeric value equal to the milliseconds value.

      返回 number

    • Sets the month value in the Date object using Universal Coordinated Time (UTC).

      参数

      • month: number

        A numeric value equal to the month. The value for January is 0, and other month values follow consecutively.

      • 可选date: number

        A numeric value representing the day of the month. If it is not supplied, the value from a call to the getUTCDate method is used.

      返回 number

    • Sets the seconds value in the Date object using Universal Coordinated Time (UTC).

      参数

      • sec: number

        A numeric value equal to the seconds value.

      • 可选ms: number

        A numeric value equal to the milliseconds value.

      返回 number

    • 返回在当前日期时间基础上减去指定时间段后的新 DateX 实例。

      该方法将传入时间段的每个字段取反后调用 DateX.add 完成计算。

      参数

      返回 DateX

      减少后的新 DateX 实例;原实例不被修改。

      26.4.13

      DateX.add

    • Returns a date as a string value.

      返回 string

    • Returns a date as a string value in ISO format.

      返回 string

    • 返回用于 JSON 序列化的日期时间字符串。

      默认调用 DateX.toString 并传入 "json" 模式,输出 YYYY-MM-DDTHH:MM:SS 格式。

      返回 string

      JSON 序列化字符串。

      26.4.13

    • Returns a date as a string value appropriate to the host environment's current locale.

      返回 string

    • Converts a date to a string by using the current or specified locale.

      参数

      • 可选locales: string | string[]

        A locale string or array of locale strings that contain one or more language or locale tags. If you include more than one locale string, list them in descending order of priority so that the first entry is the preferred locale. If you omit this parameter, the default locale of the JavaScript runtime is used.

      • 可选options: DateTimeFormatOptions

        An object that contains one or more properties that specify comparison options.

      返回 string

    • Converts a date to a string by using the current or specified locale.

      参数

      • 可选locales: LocalesArgument

        A locale string, array of locale strings, Intl.Locale object, or array of Intl.Locale objects that contain one or more language or locale tags. If you include more than one locale string, list them in descending order of priority so that the first entry is the preferred locale. If you omit this parameter, the default locale of the JavaScript runtime is used.

      • 可选options: DateTimeFormatOptions

        An object that contains one or more properties that specify comparison options.

      返回 string

    • Returns a value as a string value appropriate to the host environment's current locale.

      返回 string

    • Converts a date and time to a string by using the current or specified locale.

      参数

      • 可选locales: string | string[]

        A locale string or array of locale strings that contain one or more language or locale tags. If you include more than one locale string, list them in descending order of priority so that the first entry is the preferred locale. If you omit this parameter, the default locale of the JavaScript runtime is used.

      • 可选options: DateTimeFormatOptions

        An object that contains one or more properties that specify comparison options.

      返回 string

    • Converts a date and time to a string by using the current or specified locale.

      参数

      • 可选locales: LocalesArgument

        A locale string, array of locale strings, Intl.Locale object, or array of Intl.Locale objects that contain one or more language or locale tags. If you include more than one locale string, list them in descending order of priority so that the first entry is the preferred locale. If you omit this parameter, the default locale of the JavaScript runtime is used.

      • 可选options: DateTimeFormatOptions

        An object that contains one or more properties that specify comparison options.

      返回 string

    • Returns a time as a string value appropriate to the host environment's current locale.

      返回 string

    • Converts a time to a string by using the current or specified locale.

      参数

      • 可选locales: string | string[]

        A locale string or array of locale strings that contain one or more language or locale tags. If you include more than one locale string, list them in descending order of priority so that the first entry is the preferred locale. If you omit this parameter, the default locale of the JavaScript runtime is used.

      • 可选options: DateTimeFormatOptions

        An object that contains one or more properties that specify comparison options.

      返回 string

    • Converts a time to a string by using the current or specified locale.

      参数

      • 可选locales: LocalesArgument

        A locale string, array of locale strings, Intl.Locale object, or array of Intl.Locale objects that contain one or more language or locale tags. If you include more than one locale string, list them in descending order of priority so that the first entry is the preferred locale. If you omit this parameter, the default locale of the JavaScript runtime is used.

      • 可选options: DateTimeFormatOptions

        An object that contains one or more properties that specify comparison options.

      返回 string

    • 将日期时间格式化为 YYYY-MM-DD HH:MM:SS 形式的字符串。

      当传入 "json" 模式时,日期与时间之间使用 T 连接,以兼容 ISO 风格。

      参数

      • 可选mode: "json"

        可选的格式化模式。传入 "json" 时使用 T 分隔符。

      返回 string

      格式化后的日期时间字符串。

      26.4.13

    • Returns a time as a string value.

      返回 string

    • Returns a date converted to a string using Universal Coordinated Time (UTC).

      返回 string

    • Returns the stored time value in milliseconds since midnight, January 1, 1970 UTC.

      返回 number

    • Returns the number of milliseconds elapsed since midnight, January 1, 1970 Universal Coordinated Time (UTC).

      返回 number

    • Parses a string containing a date, and returns the number of milliseconds between that date and midnight, January 1, 1970.

      参数

      • s: string

        A date string

      返回 number

    • Returns the number of milliseconds between midnight, January 1, 1970 Universal Coordinated Time (UTC) (or GMT) and the specified date.

      参数

      • year: number

        The full year designation is required for cross-century date accuracy. If year is between 0 and 99 is used, then year is assumed to be 1900 + year.

      • monthIndex: number

        The month as a number between 0 and 11 (January to December).

      • 可选date: number

        The date as a number between 1 and 31.

      • 可选hours: number

        Must be supplied if minutes is supplied. A number from 0 to 23 (midnight to 11pm) that specifies the hour.

      • 可选minutes: number

        Must be supplied if seconds is supplied. A number from 0 to 59 that specifies the minutes.

      • 可选seconds: number

        Must be supplied if milliseconds is supplied. A number from 0 to 59 that specifies the seconds.

      • 可选ms: number

        A number from 0 to 999 that specifies the milliseconds.

      返回 number

    • Returns the number of milliseconds between midnight, January 1, 1970 Universal Coordinated Time (UTC) (or GMT) and the specified date.

      参数

      • year: number

        The full year designation is required for cross-century date accuracy. If year is between 0 and 99 is used, then year is assumed to be 1900 + year.

      • 可选monthIndex: number

        The month as a number between 0 and 11 (January to December).

      • 可选date: number

        The date as a number between 1 and 31.

      • 可选hours: number

        Must be supplied if minutes is supplied. A number from 0 to 23 (midnight to 11pm) that specifies the hour.

      • 可选minutes: number

        Must be supplied if seconds is supplied. A number from 0 to 59 that specifies the minutes.

      • 可选seconds: number

        Must be supplied if milliseconds is supplied. A number from 0 to 59 that specifies the seconds.

      • 可选ms: number

        A number from 0 to 999 that specifies the milliseconds.

      返回 number