os.date lua
-- Getting the current date and time
local currentDate = os.date("*t")
-- Accessing individual components of the date and time
local year = currentDate.year
local month = currentDate.month
local day = currentDate.day
local hour = currentDate.hour
local min = currentDate.min
local sec = currentDate.sec
-- Displaying the date and time components
print("Current Date and Time:")
print("Year: " .. year)
print("Month: " .. month)
print("Day: " .. day)
print("Hour: " .. hour)
print("Minute: " .. min)
print("Second: " .. sec)
Explanation:
os.date("t")
retrieves the current date and time information and stores it in a table format (t
) namedcurrentDate
.currentDate.year
,currentDate.month
,currentDate.day
,currentDate.hour
,currentDate.min
, andcurrentDate.sec
are used to access individual components (year, month, day, hour, minute, second) of the current date and time stored in thecurrentDate
table.print("Current Date and Time:")
and subsequentprint
statements are used to display the extracted date and time components to the console by concatenating the component names with their respective values using the..
operator.