TimeToString

Converting a value containing time in seconds elapsed since 01.01.1970 into a string of "yyyy.mm.dd hh:mi" format.

string  TimeToString(
   datetime  value,                           // number
   int       mode=TIME_DATE|TIME_MINUTES      // output format
   );

Parameters

value

[in]  Time in seconds from 00:00 1970/01/01.

mode=TIME_DATE|TIME_MINUTES

[in] Additional data input mode. Can be one or combined flag:
TIME_DATE gets result as "yyyy.mm.dd",
TIME_MINUTES gets result as "hh:mi",
TIME_SECONDS gets results as "hh:mi:ss".

Return Value

String.

 

Example:

datetime ExtBarTimeOpen;
 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- set the timer to one second
   EventSetTimer(1);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   Comment("");
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//--- get the current bar opening time
   ExtBarTimeOpen=time[rates_total-1];
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//--- set the previous bar opening time
   static datetime bar_open_time=ExtBarTimeOpen;
//--- count the number of seconds passed since the bar opened
   static int seconds=int(TimeCurrent()-ExtBarTimeOpen);
//--- if the previous opening time is not equal to the current one, then this is a new bar
//--- write the new opening time as the previous one and set the seconds to zero
   if(bar_open_time!=ExtBarTimeOpen)
     {
      bar_open_time=ExtBarTimeOpen;
      seconds=0;
     }
//--- increase and adjust the number of seconds passed since the bar opened
   seconds++;
   if(seconds>PeriodSeconds(PERIOD_CURRENT))
      seconds=0;
//--- bar opening time as yyyy.mm.dd hh:mi
   string bar_time_open=TimeToString(ExtBarTimeOpen);
//--- current time as yyyy.mm.dd hh:mi:ss
   string time_current=TimeToString(TimeCurrent(),TIME_DATE|TIME_MINUTES|TIME_SECONDS);
//--- number of seconds left until a new bar opens
   int    sec_left=PeriodSeconds(PERIOD_CURRENT)-seconds;
//--- amount of time remaining until the current bar closes as hh:mm:ss
   string time_left=TimeToString(sec_left,TIME_MINUTES|TIME_SECONDS);
//--- create the output string
   string txt=StringFormat("Opening time of the current bar: %s\n"+
                           "Time Current: %s\n"+
                           "Seconds have passed since the bar opened: %d\n"+
                           "Approximately seconds left before bar closes: %d\n"+
                           "Time remaining until bar closes: %s",bar_time_open,time_current,seconds,sec_left,time_left);
//--- display the bar open time and the current time,
//--- the number of seconds passed since the current bar opened and remaining until it closes and
//--- the time remaining until the current bar closes in the comment
   Comment(txt);
   /*
   result on M1:
   Opening time of the current bar2024.02.22 18:06
   Time Current2024.02.22 18:06:24
   Seconds have passed since the bar opened25
   Approximately seconds left before bar closes35
   Time remaining until bar closes00:00:35
 
   result on M5:
   Opening time of the current bar2024.02.22 18:05
   Time Current2024.02.22 18:07:28
   Seconds have passed since the bar opened149
   Approximately seconds left before bar closes151
   Time remaining until bar closes00:02:31
 
   result on H1:
   Opening time of the current bar2024.02.22 18:00
   Time Current2024.02.22 18:08:13
   Seconds have passed since the bar opened494
   Approximately seconds left before bar closes3106
   Time remaining until bar closes00:51:46
   
   result on D1:
   Opening time of the current bar2024.02.22 00:00
   Time Current2024.02.22 18:11:01
   Seconds have passed since the bar opened65462
   Approximately seconds left before bar closes20938
   Time remaining until bar closes05:48:58
   */
  }

See also

StringToTime, TimeToStruct