Skip to content

Instantly share code, notes, and snippets.

@ohmypxl
Last active September 15, 2024 08:59
Show Gist options
  • Save ohmypxl/c100d28a42801b7bd123d001d941d1c7 to your computer and use it in GitHub Desktop.
Save ohmypxl/c100d28a42801b7bd123d001d941d1c7 to your computer and use it in GitHub Desktop.
Simple get current date, nothing fancy.
/*
@Title: Datetime Script 1.0
@Author: Aiura
@License: MIT
@Date: February 24th, 2023
Feel free to copy and paste the code, but don't forget to put the credits at the top of the function
- Aiura
*/
#if defined datetime_stuff
#endinput
#endif
#define datetime_stuff
/**
* <summary>Getting the current (Real) time with fancy string format.</summary>
* <param name="dest">The destination string to save the string in, passed by reference</param>
* <param name="len">The maximum size to insert (optional=<b><c>sizeof (dest)</c></b>)</param>
* <param name ="timeFormat">The time format you want to use (<b><c>12</c></b> or <b><c>24</c></b>)</param>
* <param name = "amPm">When set to true, the output will add AM/PM at the end of the time.
* When set to false, the outout will not add it</param>
* <remarks>This function only uses Real time and not in-game or timestamp time</remarks>
* <remarks>The output may varies depending on your actual server time</remarks>
*/
void:GetCurrentDate(dest[], len = sizeof(dest), timeFormat = 12, amPm = true)
{
// Prepare static arrays (if not created)
static g_sArrMonths[][] = {
"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"
};
// Get Real Time
new hour, minute;
new year, month, day;
gettime(hour, minute, _);
getdate(year, month, day);
// Determine AM/PM (if enabled)
new amPmText[3];
if (amPm)
{
amPmText[0] = (hour >= 12) ? 'P' : 'A';
amPmText[1] = 'M';
}
// Changing time format (if timeformat set to 12)
hour %= timeFormat;
// Determine ordinal number
new ord[3]; ord = ((day % 10 == 1) ? ("st") : ((day % 10 == 2) ? ("nd") : ((day % 10 == 3) ? ("rd") : ("th"))));
// Format everything
// Output: January 1st, 1997 12:00 AM
format(output, len, "%s %02d%s, %04d %02d:%02d %s", g_sArrMonths[month - 1], day, ord, year, hour, minute, amPmText);
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment