Chuyển tới nội dung chính

DateTime()

Lớp tiện ích để thực hiện các tính toán ngày giờ.

Khai báo

Tạo một thể hiện DateTime mới biểu diễn ngày và giờ hiện tại.

DateTime();

Tạo một thể hiện DateTime mới từ một timestamp tính bằng mili giây.

DateTime(timestamp: number)

Tạo một thể hiện DateTime mới bằng cách phân tích một chuỗi ngày.

DateTime(dateString: string)

Tạo một thể hiện DateTime mới cho ngày được chỉ định vào lúc nửa đêm (00:00:00).

DateTime(year: number, month: number, day: number)

Tạo một thể hiện DateTime mới cho ngày và giờ được chỉ định.

DateTime(year: number, month: number, day: number, hour: number, minute: number, second: number)

Tham số

Tham sốKiểuMô tả
timestampnumbertimestamp tính bằng mili giây kể từ epoch
dateStringstringchuỗi biểu diễn ngày cần phân tích
yearnumbergiá trị năm (ví dụ: 2024)
monthnumbergiá trị tháng (1-12)
daynumberngày trong tháng (1-31)
hournumbergiờ trong ngày (0-23)
minutenumberphút trong giờ (0-59)
secondnumbergiây trong phút (0-59)

Trả về

  • Kiểu: DateTime
  • Một thể hiện DateTime mới.

Ví dụ

-- Tạo một đối tượng DateTime cho ngày và giờ hiện tại
local now = DateTime()
toast("DateTime hiện tại: " .. now:format("yyyy-MM-dd HH:mm:ss"))

-- Tạo một đối tượng DateTime từ một timestamp
local timestamp = 1678886400000 -- 15 tháng 3, 2023 00:00:00 UTC
local dateFromTimestamp = DateTime(timestamp)
toast("DateTime từ timestamp: " .. dateFromTimestamp:format("yyyy-MM-dd HH:mm:ss"))

-- Tạo một đối tượng DateTime từ một chuỗi ngày
local dateFromString = DateTime("2023-03-15 10:30:00")
toast("DateTime từ chuỗi: " .. dateFromString:format("yyyy-MM-dd HH:mm:ss"))

-- Tạo một đối tượng DateTime cho một ngày cụ thể
local specificDate = DateTime(2023, 3, 15)
toast("Ngày cụ thể: " .. specificDate:format("yyyy-MM-dd HH:mm:ss"))

-- Tạo một đối tượng DateTime cho một ngày và giờ cụ thể
local specificDateTime = DateTime(2023, 3, 15, 14, 45, 30)
toast("DateTime cụ thể: " .. specificDateTime:format("yyyy-MM-dd HH:mm:ss"))