StringToTime

"yyyy.mm.dd [hh:mi]”形式の時刻および/または日付を含む文字列を日時型の数値に変換します。

datetime  StringToTime(
  const string  time_string      // 日付の文字列
  );

パラメータ

time_string

[in]  次のいずれかで指定された形式での文字列:

  •  "yyyy.mm.dd [hh:mi]"
  •  "yyyy.mm.dd [hh:mi:ss]"
  •  "yyyymmdd [hh:mi:ss]"
  •  "yyyymmdd [hhmiss]"
  •  "yyyy/mm/dd [hh:mi:ss]"
  •  "yyyy-mm-dd [hh:mi:ss]"

戻り値

1970年1月1日からの経過秒数の合計を含むdatetime型の値

注意事項

StringToTime()を呼び出す前のtime_stringによる追加処理を避けるために、日付と時刻の間にある一連のスペースと集計文字は単一スペースと見なされます。

 

例:

//--- 入力パラメータ
input group   "The date can be entered in any of the formats:"
input group   "yyyy.mm.dd [hh:mi], yyyy.mm.dd [hh:mi:ss]"
input group   "yyyymmdd [hh:mi:ss], yyyymmdd [hhmiss]"
input group   "yyyy/mm/dd [hh:mi:ss], yyyy-mm-dd [hh:mi:ss]"
input string   InpDateStr;   // Please enter the date here as a string
 
//+------------------------------------------------------------------+
//| スクリプトプログラム開始関数                                              |
//+------------------------------------------------------------------+
void OnStart()
 {
//--- 入力に文字列として入力された時刻を日時値に変換する
  datetime time=StringToTime(InpDateStr);
//--- 入力された文字列と取得した時刻を操作ログに表示する
  PrintFormat("Date entered as a string in the form '%s' is converted to datetime in the form '%s'",
              InpDateStr, TimeToString(time, TIME_DATE|TIME_MINUTES|TIME_SECONDS));
//--- 受信した日時上に垂直線を作成し、チャートをこの位置に移動する
  if(CreateVLine(time))
    ChartNavigateToTime(time);
  /*
  結果:
  Date entered as a string in the form '' is converted to datetime in the form '1970.01.01 00:00:00'
  Date entered as a string in the form '2024' is converted to datetime in the form '2024.02.24 20:24:00'
  Date entered as a string in the form '202400' is converted to datetime in the form '2024.02.24 20:24:00'
  Date entered as a string in the form '20240000' is converted to datetime in the form '2024.02.24 00:00:00'
  Date entered as a string in the form '2024022410' is converted to datetime in the form '2030.09.06 00:00:00'
  Date entered as a string in the form '20240224 10' is converted to datetime in the form '2024.02.24 10:00:00'
  Date entered as a string in the form '20240224 01' is converted to datetime in the form '2024.02.24 01:00:00'
  Date entered as a string in the form '20240224 0030' is converted to datetime in the form '2024.02.24 23:00:00'
  Date entered as a string in the form '20240224 0100' is converted to datetime in the form '2024.02.24 01:00:00'
  */
 }
//+------------------------------------------------------------------+
//| 垂直線オブジェクトを作成する
//+------------------------------------------------------------------+
bool CreateVLine(const datetime line_time)
 {
  ResetLastError();
 
  string name=MQLInfoString(MQL_PROGRAM_NAME)+"_VLINE";
  if(!ObjectCreate(0, name, OBJ_VLINE, 0, line_time, 0))
    {
    Print("ObjectCreate() failed. Error code: ", GetLastError());
    return(false);
    }
  ObjectSetInteger(0, name, OBJPROP_STYLE, STYLE_DOT);
  ObjectSetInteger(0, name, OBJPROP_SELECTABLE, true);
 
  return(true);
 }
//+------------------------------------------------------------------+
//| チャートを指定されたバーの開始時間にシフトする
//+------------------------------------------------------------------+
bool ChartNavigateToTime(const datetime time)
 {
  ChartSetInteger(0, CHART_AUTOSCROLL, false);
  ResetLastError();
 
  int bar=iBarShift(_Symbol, PERIOD_CURRENT, time);
  if(bar<0)
    {
    PrintFormat("%s: iBarShift() failed. Error code: %d", __FUNCTION__, GetLastError());
    return(false);
    }
 
  long first=0;
  if(!ChartGetInteger(0, CHART_FIRST_VISIBLE_BAR, 0, first))
    {
    PrintFormat("%s: ChartGetInteger() failed. Error code: %d", __FUNCTION__, GetLastError());
    return(false);
    }
 
  return(ChartNavigate(0, CHART_CURRENT_POS, (int)first-bar));
 }

参照

TimeToStringTimeToStruct