Skip to content
## toDate(date : string) : Date > 将字符串格式的日期转换为JavaScript Date对象

支持平台 ​

安卓iosweb微信小程序
√√√√

参数 ​

  • date: string - 要转换的日期字符串。

返回值 ​

  • Date: 如果字符串是有效的日期格式,则返回一个Date对象,否则可能返回null或抛出错误。

示例 ​

javascript
import { TuiDate } from '@/uni_modules/tui-common'
const date = TuiDate.toDate('2025年8月15日')

now() : string ​

获取当前日期(静态方法)

支持平台 ​

安卓iosweb微信小程序
√√√√

参数 ​

  • 无参数。

返回值 ​

  • string: 当前日期和时间的字符串,格式为yyyy-MM-dd HH:mm:ss。

示例 ​

javascript
import { TuiDate } from '@/uni_modules/tui-common'
const currentDate = TuiDate.now()
console.log(currentDate) // 输出当前日期时间,例如 "2024-11-20 08:30:45"

timestamp():number ​

获取当前时间时间戳

支持平台 ​

安卓iosweb微信小程序
√√√√

参数 ​

  • 无参数。

返回值 ​

  • number: 当前日期和时间的Unix时间戳(单位:毫秒)。

示例 ​

javascript
import { TuiDate } from '@/uni_modules/tui-common'
const currentTimeStamp = TuiDate.timestamp()
console.log(currentTimeStamp) // 输出当前时间的时间戳,例如 1679096400000

nowYear():number ​

获取今年的年份

支持平台 ​

安卓iosweb微信小程序
√√√√

参数 ​

  • 无参数。

返回值 ​

  • number: 当前年份的数字表示。

示例 ​

javascript
import { TuiDate } from '@/uni_modules/tui-common'
const currentYear = TuiDate.nowYear()
console.log(currentYear) // 输出当前年份,例如 2024

lastYear():number ​

获取去年的年份

支持平台 ​

安卓iosweb微信小程序
√√√√

参数 ​

  • 无参数。

返回值 ​

  • number: 去年的年份的数字表示。

示例 ​

javascript
import { TuiDate } from '@/uni_modules/tui-common'
const lastYear = TuiDate.lastYear()
console.log(lastYear) // 输出去年的年份,例如 2023

nextYear():number ​

获取明年的年份

支持平台 ​

安卓iosweb微信小程序
√√√√

参数 ​

  • 无参数。

返回值 ​

  • number: 明年的年份的数字表示。

示例 ​

javascript
import { TuiDate } from '@/uni_modules/tui-common'
const nextYear = TuiDate.nextYear()
console.log(nextYear) // 输出明年的年份,例如 2025

month():number ​

获取今年的月份

支持平台 ​

安卓iosweb微信小程序
√√√√

参数 ​

  • 无参数。

返回值 ​

  • number: 当前月份的数字表示(1-12)。

示例 ​

javascript
import { TuiDate } from '@/uni_modules/tui-common'
const currentMonth = TuiDate.month()
console.log(currentMonth) // 输出当前月份,例如 11

lastMonth():number ​

获取去年的月份

支持平台 ​

安卓iosweb微信小程序
√√√√

参数 ​

  • 无参数。

返回值 ​

  • number: 去年同一个月的数字表示(1-12)。

示例 ​

javascript
import { TuiDate } from '@/uni_modules/tui-common'
const lastMonth = TuiDate.lastMonth()
console.log(lastMonth) // 输出去年同一个月的月份,例如 11

nextMonth():number ​

获取明年的月份

支持平台 ​

安卓iosweb微信小程序支付宝小程序QQ小程序
√√√xxx

参数 ​

  • 无参数。

返回值 ​

  • number: 明年同一个月的数字表示(1-12)。

示例 ​

javascript
import { TuiDate } from '@/uni_modules/tui-common'
const nextMonth = TuiDate.nextMonth()
console.log(nextMonth) // 输出明年同一个月的月份,例如 11

monthDays(year:number,month:number):number ​

获取指定年月的天数

支持平台 ​

安卓iosweb微信小程序
√√√√

参数 ​

  • year: number - 指定的年份。
  • month: number - 指定的月份(1-12)。

返回值 ​

  • number: 指定年月的天数。

示例 ​

javascript
import { TuiDate } from '@/uni_modules/tui-common'
const daysInMonth = TuiDate.monthDays(2025, 8)
console.log(daysInMonth) // 输出2025年8月的天数,例如 31

monthDates(date:string):string[] ​

获取指定日期所在月份的所有日期

支持平台 ​

安卓iosweb微信小程序
√√√√

参数 ​

  • date: string - 指定的日期字符串,格式为 'YYYY-MM-DD'。

返回值 ​

  • string[]: 包含指定月份所有日期的字符串数组,格式为 'YYYY-MM-DD'。

示例 ​

javascript
import { TuiDate } from '@/uni_modules/tui-common'
const allDatesInMonth = TuiDate.monthDates('2025-08-15')
console.log(allDatesInMonth) // 输出2025年8月所有日期的数组,例如 ['2025-08-01', '2025-08-02', ..., '2025-08-31']

months(startMonth:string,endMonth:string):string[] ​

获取两个月份之间的所有月份

支持平台 ​

安卓iosweb微信小程序
√√√√

参数 ​

  • startMonth: string - 起始月份,格式为 'yyyy-MM'。
  • endMonth: string - 结束月份,格式为 'yyyy-MM'。

返回值 ​

  • string[]: 两个月份之间的所有月份,格式为 'yyyy-MM'。

示例 ​

javascript
import { TuiDate } from '@/uni_modules/tui-common'
const monthsBetween = TuiDate.months('2025-01', '2025-06')
console.log(monthsBetween) // 输出从2025年1月到2025年6月之间的所有月份,例如 ["2025-01", "2025-02", "2025-03", "2025-04", "2025-05", "2025-06"]

week():number ​

获取本周是星期几

支持平台 ​

安卓iosweb微信小程序
√√√√

参数 ​

  • 无参数。

返回值 ​

  • number: 本周是星期几,其中0代表星期日,1代表星期一,以此类推,6代表星期六。

示例 ​

javascript
import { TuiDate } from '@/uni_modules/tui-common'
const currentWeekDay = TuiDate.week()
console.log(currentWeekDay) // 输出本周是星期几,例如 3 代表星期三

weeks():string[] ​

获取本周的所有日期

支持平台 ​

安卓iosweb微信小程序
√√√√

参数 ​

  • 无参数。

返回值 ​

  • string[]: 包含本周所有日期的数组,格式为 'yyyy-MM-dd'。

示例 ​

javascript
import { TuiDate } from '@/uni_modules/tui-common'
const weekDates = TuiDate.weeks()
console.log(weekDates) // 输出本周的所有日期,例如 ["2024-11-20", "2024-11-21", ..., "2024-11-26"]

today():string ​

获取今天的日期

支持平台 ​

安卓iosweb微信小程序
√√√√

参数 ​

  • 无参数。

返回值 ​

  • string: 当前的日期,格式为 'yyyy-MM-dd'。

示例 ​

javascript
import { TuiDate } from '@/uni_modules/tui-common'
const todayDate = TuiDate.today()
console.log(todayDate) // 输出今天的日期,例如 "2024-11-20"

yesterday():string ​

获取昨天的日期

支持平台 ​

安卓iosweb微信小程序
√√√√

参数 ​

  • 无参数。

返回值 ​

  • string: 昨天的日期,格式为 'yyyy-MM-dd'。

示例 ​

javascript
import { TuiDate } from '@/uni_modules/tui-common'
const yesterdayDate = TuiDate.yesterday()
console.log(yesterdayDate) // 输出昨天的日期,例如 "2024-11-19"

tomorrow():string ​

获取明天的日期

支持平台 ​

安卓iosweb微信小程序
√√√√

参数 ​

  • 无参数。

返回值 ​

  • string: 明天的日期,格式为 'yyyy-MM-dd'。

示例 ​

javascript
import { TuiDate } from '@/uni_modules/tui-common'
const tomorrowDate = TuiDate.tomorrow()
console.log(tomorrowDate) // 输出明天的日期,例如 "2024-11-21"

dates(startDate:string,endDate:string):string[] ​

获取两个日期之间的所有日期

支持平台 ​

安卓iosweb微信小程序
√√√√

参数 ​

  • startDate: 开始日期,格式为 'yyyy-MM-dd'。
  • endDate: 结束日期,格式为 'yyyy-MM-dd'。

返回值 ​

  • string[]: 包含两个日期之间所有日期的数组,格式为 'yyyy-MM-dd'。

示例 ​

javascript
import { TuiDate } from '@/uni_modules/tui-common'
const dateRange = TuiDate.dates('2025-08-01', '2025-08-31')
console.log(dateRange) // 输出从2025-08-01到2025-08-31之间的所有日期

time():string ​

获取当前时间

支持平台 ​

安卓iosweb微信小程序
√√√√

参数 ​

  • 无参数。

返回值 ​

  • string: 当前的具体时间,格式为 'HH:mm:ss'。

示例 ​

javascript
import { TuiDate } from '@/uni_modules/tui-common'
const currentTime = TuiDate.time()
console.log(currentTime) // 输出当前时间,例如 "15:45:30"

compare(date1:string,date2:string):number ​

比较两个日期

支持平台 ​

安卓iosweb微信小程序
√√√√

参数 ​

  • date1: 第一个日期字符串,格式为 'yyyy-MM-dd'。
  • date2: 第二个日期字符串,格式为 'yyyy-MM-dd'。

返回值 ​

  • number: 比较结果,如果 date1 在 date2 之前则返回 -1,如果 date1 在 date2 之后则返回 1,如果两个日期相同则返回 0。

示例 ​

javascript
import { TuiDate } from '@/uni_modules/tui-common'
const comparisonResult = TuiDate.compare('2025-08-15', '2025-08-16')
console.log(comparisonResult) // 输出 -1,因为 '2025-08-15' 在 '2025-08-16' 之前

toTimestamp(date:string):number ​

将日期字符串转换为毫秒时间戳

支持平台 ​

安卓iosweb微信小程序
√√√√

参数 ​

  • date: 日期时间字符串,格式为 'yyyy-MM-dd HH:mm:ss'。

返回值 ​

  • number: 返回一个表示日期时间的毫秒时间戳。

示例 ​

javascript
import { TuiDate } from '@/uni_modules/tui-common'
const timestamp = TuiDate.toTimestamp('2025-08-15 12:00:00')
console.log(timestamp) // 输出对应的毫秒时间戳

toMillisecond(time:string):number ​

将时间字符串转换为毫秒数

支持平台 ​

安卓iosweb微信小程序
√√√√

参数 ​

  • time: 时间字符串,格式为 'HH:mm:ss'。

返回值 ​

  • number: 返回一个表示时间的毫秒数。

示例 ​

javascript
import { TuiDate } from '@/uni_modules/tui-common'
const milliseconds = TuiDate.toMillisecond('12:00:00')
console.log(milliseconds) // 输出对应时间的毫秒数