Skip to content

This library provides functions related to time and date.

About os

This library currently serves the purpose of providing information about the system time under the UTC format. It has been heavily sandboxed from the standard Lua os library and does not allow you to perform any system-altering operations.

Functions 4

  • clock() → doubleReturns elapsed time in seconds since an arbitrary baseline with sub-microsecond precision.

    Returns elapsed time in seconds since an arbitrary baseline with sub-microsecond precision. This function is useful for comparing durations between two events that occur on the same computer, and is the best option for benchmarking.

    Unlike with functions such as os.time or DateTime.now, adjustments to the system clock (such as by the user or NTP) do not cause time to jump forwards or backwards.

    -- Record the initial time:
    local startTime = os.clock()
    -- Do something you want to measure the performance of:
    local a, b = 0, 1
    for _ = 1, 5000000 do
        a, b = b, a
    end
    -- Measure amount of time this took:
    local deltaTime = os.clock() - startTime
    print("Elapsed time: " .. deltaTime)
    -->  Elapsed time: 0.044425600033719 (actual number may vary)
    
  • date(formatString: string, time: int) → DictionaryFormats the given string with date/time information based on the given time.

    Formats the given formatString with date/time information based on the given time, or if not provided, the value returned by os.time.

    This function should be avoided in new work. Instead, use the DateTime API, which supports localized formatting.

    The following specifiers (based on the C function strftime) are supported:

    * This value can vary depending on the current locale.

    † The example provided is for February 12th, 2024 (Monday) at 2:14:35 PM (14:14:35), run using locale "en-us" in Pacific Standard Time (PST).

    If the provided formatString is exactly "*t" (local time) or "!*t" (UTC time), this function instead returns a dictionary containing the following components, which are normally available in the specifiers above.

  • difftime(t2: int, t1: int) → intReturns the number of seconds from one time to another.

    Returns the number of seconds from t1 to t2. The difference is computed assuming that t1 and t2 are correctly casted to the time_t format.

  • time(time: table = UTC time) → intReturns how many seconds have passed since the Unix epoch (1 January 1970, 00:00:00) under current UTC time.

    Returns how many seconds have passed since the Unix epoch (1 January 1970, 00:00:00), under current UTC time. If provided a table formatted similarly to that returned by os.date, it returns the number of seconds from the Unix epoch to that time instead.

    Note that the returned time uses the device's local clock. Most operating systems automatically sync their local time against online time servers, so this should be within a few hundred milliseconds. However, users can easily disable sync behavior and set the system time to anything they want; for synchronized time between client and server, use Workspace.GetServerTimeNow instead.

    This function should be avoided in new work. Instead, use the DateTime API, which supports localized formatting.

    When you need to precisely measure the time elapsed between two points in time, like when testing performance, use os.clock instead.

Reference text from the Roblox Creator Documentation, licensed CC BY 4.0, reorganized by rbxtoolshub.com · Studio 0.743.0.7431133.