跳到主要内容

DateTime():format()

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

签名​

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

参数​

参数类型描述
pattern(可选)string要应用的格式模式。如未提供,则使用默认格式。可接受的格式包括:yyyy-MM-dd'T'HH:mm:ss.SSSZ、yyyy-MM-dd'T'HH:mm:ssZ、yyyy-MM-dd'T'HH:mm:ss、yyyy-MM-dd HH:mm:ss、yyyy-MM-dd、MM/dd/yyyy、dd/MM/yyyy、yyyy/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)