跳到主要内容

DateTime():format()

以字符串形式返回格式化后的日期。

签名

format(pattern: string): string
format(): string

参数

参数类型描述
pattern(可选)string要应用的格式模式。如未提供,则使用默认格式。可接受的格式包括:yyyy-MM-dd'T'HH:mm:ss.SSSZyyyy-MM-dd'T'HH:mm:ssZyyyy-MM-dd'T'HH:mm:ssyyyy-MM-dd HH:mm:ssyyyy-MM-ddMM/dd/yyyydd/MM/yyyyyyyy/MM/dd

返回值

  • 类型:string
  • 格式化后的日期字符串。

示例

local now = DateTime()

-- 使用特定模式格式化
local formattedDate1 = now:format("yyyy-MM-dd HH:mm:ss")
toast("Formatted Date (yyyy-MM-dd HH:mm:ss): " .. formattedDate1)

-- 使用不同的模式格式化
local formattedDate2 = now:format("MM/dd/yyyy")
toast("Formatted Date (MM/dd/yyyy): " .. formattedDate2)

-- 使用默认模式格式化(未提供模式时)
local formattedDate3 = now:format()
toast("Formatted Date (default): " .. formattedDate3)