string GetTimeSpan(datetime time_sec)
{
if(time_sec <= 0)
return "0 seconds";
string time_text = {};
datetime remaining_sec = time_sec;
int years = (int)remaining_sec / 31536000;
remaining_sec %= 31536000;
int months = (int)remaining_sec / 2592000;
remaining_sec %= 2592000;
int days = (int)remaining_sec / 86400;
remaining_sec %= 86400;
int hours = (int)remaining_sec / 3600;
remaining_sec %= 3600;
int minutes = (int)remaining_sec / 60;
remaining_sec %= 60;
int seconds = (int)remaining_sec;
if(years > 0)
time_text += (string)years + " years ";
if(months > 0)
time_text += (string)months + " months ";
if(days > 0)
time_text += (string)days + " days ";
if(hours > 0)
time_text += (string)hours + " hours ";
if(minutes > 0)
time_text += (string)minutes + " minutes ";
if(seconds > 0)
time_text += (string)seconds + " seconds ";
return time_text;
}
#58198ScriptsMetaTrader 5👁 1830 views
Function for converting the number of seconds to years, months, days, hours, minutes, seconds
A
Author
Anton IaroshenkoPublished date
May 13, 2025
📚Related Articles
← View other articles →#77077Scripts
Clock Diagnostic: TimeCurrent() freezes and steps backwards
ClockDiagnostic.mq5 reads TimeCurrent() and TimeLocal() every few seconds for a fixed observation window and reports the two failure modes of the server clock: it freezes when no t...
Joao Prata Silva YglesiasView detail →
#77073Scripts
Gold Hour Profile - when gold moves, and when the spread eats it
Every trader knows that some hours are better than others. This script answers that question with numbers from your own broker, instead of a general rule of thumb. For each hour of...
Petr KostalView detail →
#77063Scripts
OHLC Reality Check - at what stop distance does your backtest start lying?
Every backtest on 1 minute OHLC data makes one silent assumption: that inside a bar, price walked Open Low High Close on a bullish bar and Open High Low Close on a bearish one. It ...
Petr KostalView detail →