Author Topic: getFullDateTime() - returns MM/DD/YYYY hh:mm:ss  (Read 237 times)

getDateTime() only returns two year digits, meaning it will not work if you happen to be a time traveller from 1920 or 2120.

If you need the full year, this is the function you need.

Code: [Select]
// returns the date and time, with the full current year
function getFullDateTime()
{
// get the current date and time from getDateTime
%value = getDateTime();
%date = getWord(%value, 0);

// if the date has changed, or the date has never been set, get the current century by creating a temporary file
if ($CurrDate $= "" || $CurrDate !$= %date)
{
$CurrDate = %date;
%file = new FileObject();
%file.openForWrite("base/temp/temp.txt");
%file.close();
%file.delete();
$CurrCentury = getSubStr(getFileModifiedSortTime("base/temp/temp.txt"), 0, 2) + 19;
fileDelete("base/temp/temp.txt");
}

// replace the year in the date with the full year
%date = strReplace(%date, "/", " ");
%date = setWord(%date, 2, $CurrCentury * 100 + getWord(%date, 2));

// return the full date and time
return strReplace(%date, " ", "/") SPC getWord(%value,1);
}

Known issues:
Does not work if the current year is before 1900. If you're time travelling from 1899 or before, you're out of luck.
Does not work if the function is called on the same day of the year, 100 years apart.
Does not work if the file base/temp/temp.txt exists, is read-only, and has a last modified date in a different century.
« Last Edit: May 14, 2020, 07:27:20 PM by Xalos »

someone in the future is gonna see this thread and go "jesus, imagine using dates"

Thank god for this. Now I can distinguish between all my data saved in 1919 and 2019.

xalos:
time travelers:

this was always my go to solution
Code: [Select]
function getRealYear() //xd
{
%datetime = getDateTime();
%day = getSubStr(%datetime,3,2);
if($swol_currDayRealYearCheck $= "" || $swol_currDayRealYearCheck !$= %day)
{
$swol_currDayRealYearCheck = %day;
%z = (((getUTC()/86400)|0)+719468)|0;
%era = ((%z>=0?%z:%z-146096)/146097)|0;
%doe = ((%z-%era*146097))|0;
%yoe = ((%doe-(%doe/1460|0)+(%doe/36524|0)-(%doe/146096|0))/365)|0;
%y = (%yoe+%era*400)|0;
%comp = getSubStr(%datetime,6,2);
%len = strLen(%y);
%last = getSubStr(%y,%len-2,2);
%y = (getSubStr(%y,0,%len-2)+(%last $= "99" && %comp $= "00" ? 1 : 0)) @ %comp;
$swol_currRealYear = %y;
}
return $swol_currRealYear;
}

that code is horrible to read. i would hate to be your project manager.

it's like you casually program for r/codegolf in every script. despite having no data limitations you compress everything into nonreadable but effective solutions
« Last Edit: May 22, 2020, 10:40:20 PM by PhantOS »