FormatDate
From Multi Theft Auto: Wiki
This function formats a date according to the given format string.
Syntax
string FormatDate( string format, [ string escaper = "'", int timestamp = GetTimestamp() ] )
Required Arguments
- format: Format string that determines how the date should be formatted. It provides the following wildcards:
- d: day (01-31)
- h: hour (00-23)
- i: minute (00-59)
- m: month (01-12)
- s: second (00-59)
- w: shortened day of the week (Su-Mo)
- W: day of the week (Sunday-Monday)
- y: shortened year (e.g. 09)
- Y: year (e.g. 2009)
Optional Arguments
NOTE: When using optional arguments, you must supply all arguments before the one you wish to use. For more information on optional arguments, see Optional Arguments.
- escaper: Escape character that determines the beginning or end of a non-format-area, where the wildcards aren't replaced with their corresponding value. Note that the escapers get lost.
- timestamp: Timestamp of the date that should be formatted.
Returns
Returns a string containing the formatted date.
Code
NOTE: This function requires function Check in order to work correctly.
Click to collapse [-]
Server- and/or clientside Scriptlocal gWeekDays = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" } function FormatDate(format, escaper, timestamp) Check("FormatDate", "string", format, "format", {"nil","string"}, escaper, "escaper", {"nil","string"}, timestamp, "timestamp") escaper = (escaper or "'"):sub(1, 1) local time = getRealTime(timestamp) local formattedDate = "" local escaped = false time.year = time.year + 1900 time.month = time.month + 1 local datetime = { d = ("%02d"):format(time.monthday), h = ("%02d"):format(time.hour), i = ("%02d"):format(time.minute), m = ("%02d"):format(time.month), s = ("%02d"):format(time.second), w = gWeekDays[time.weekday+1]:sub(1, 2), W = gWeekDays[time.weekday+1], y = tostring(time.year):sub(-2), Y = time.year } for char in format:gmatch(".") do if (char == escaper) then escaped = not escaped else formattedDate = formattedDate..(not escaped and datetime[char] or char) end end return formattedDate end
Example
Click to collapse [-]
ServerThis example outputs the actual date to a joining player.
-- get the root element local _root = getRootElement() -- define the onPlayerJoin handler function function OnPlayerJoin() -- output the formatted date outputChatBox(FormatDate("'You joined our server on' m/d/Y at h:m.")) end -- add the event handler addEventHandler("onPlayerJoin", _root, OnPlayerJoin)
Author: NeonBlack
See Also
- callClientFunction » This function allows you to call any clientside function from the server's side.
- callServerFunction » This function allows you to call any server-side function from the client's side.
- Check » This function checks if it's arguments are of the right types and calls the error-function if one isn't.
- doForAllElements » This function can be used to execute a specified function for all elements of a specified type.
- iterElements » Returns an iterator for your for loops saving time typing ipairs( getElementsByType( type ) ), instead you type: iterElements( type ).
- findRotation » Takes two points and returns the direction from point A to point B.
- FormatDate » Formats a date on the basis of a format string and returns it.
- getAge » This function calculates the age of a birthday.
- getPointFromDistanceRotation » Finds a point based on a starting point, direction and distance.
- getTimestamp » With this function you can get the UNIX timestamp.
- IfElse » Returns one of two values based on a boolean expression.
- isLeapYear » Checks if the given year is a leap year.
- math.round » Rounds a number whereas the number of decimals to keep and the method may be set.
- setTableProtected » Protects a table and makes it read-only.
- setVehicleGravityPoint » This clientside function sets a vehicle's gravity in the direction of a 3 dimensional coordinate with the strength specified.
- string.explode » This function splits a string at a given separator pattern and returns a table with the pieces.
- table.copy » This function copies a whole table and all the tables in that table.
- table.map » This function goes through a table and replaces every field with the return of the passed function, where the field's value is passed as first argument and optionally more arguments.
- table.size » Finds the absolute size of a table.
- var_dump »This function outputs information about one or more variables using outputConsole().
- RGBToHex » This function returns a string representing the color in hexadecimal.
- getAlivePlayersInTeam » This function returns a table of the alive players in a team.
- getResourceSettings » This function returns a table of the resource settings.
- onVehicleWeaponFire » This code implements an event that is triggered when a player in a vehicle fires a vehicles weapon.
- toHex » This function converts a decimal number to a hexadecimal number, as a fix to be used clientside.
- getElementSpeed » This function allows you to get element speed in kph or mph units.
- setElementSpeed » This function allows you to set moving element speed in kph or mph units.
- centerWindow » This function center the window in any resolution.
- coroutine.resume » Fix for hidden coroutine error messages
- getPlayerFromNamePart » This function allows you to get player From his Name part.