MQL5 - Return the day of the week
Is there any function to return the day of the week of today?
Like: Sunday, Monday, Tuesday...
bool WeekDays_Check(datetime aTime) { MqlDateTime stm; TimeToStruct(aTime,stm); return(WeekDays[stm.day_of_week]); }
Something like this maybe.....you can read more about this here
- www.mql5.com
Is there any function to return the day of the week of today?
Like: Sunday, Monday, Tuesday...
The code:
//+------------------------------------------------------------------+ //| DayOfWeek.mq5 | //| Copyright © 2019, Vladimir Karputov | //| http://wmua.ru/slesar/ | //+------------------------------------------------------------------+ #property copyright "Copyright © 2019, Vladimir Karputov" #property link "http://wmua.ru/slesar/" #property version "1.001" //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { //--- MqlDateTime STime; datetime time_current=TimeCurrent(); datetime time_local=TimeLocal(); TimeToStruct(time_current,STime); Print("Time Current ",TimeToString(time_current,TIME_DATE|TIME_SECONDS)," day of week ",DayOfWeekDescription(STime.day_of_week)); TimeToStruct(time_local,STime); Print("Time Local ",TimeToString(time_local,TIME_DATE|TIME_SECONDS)," day of week ",DayOfWeekDescription(STime.day_of_week)); } //+------------------------------------------------------------------+ //| Day Of Week Description | //+------------------------------------------------------------------+ string DayOfWeekDescription(const int day_of_week) { string text=""; switch(day_of_week) { case 0: text="Sunday"; break; case 1: text="Monday"; break; case 2: text="Tuesday"; break; case 3: text="Wednesday"; break; case 4: text="Thursday"; break; case 5: text="Friday"; break; case 6: text="Saturday"; break; default: text="Another day"; break; } //--- return(text); } //+------------------------------------------------------------------+
Startup Result:
2019.12.21 11:53:13.988 DayOfWeek (EURUSD,H1) Time Current 2019.12.20 23:54:59 day of week Friday 2019.12.21 11:53:13.988 DayOfWeek (EURUSD,H1) Time Local 2019.12.21 11:53:13 day of week Saturday
The code:
Startup Result:
Thank You very much!
I fixed the error and replaced it with version 1.001
The code:
Startup Result:
void OnStart() { //--- MqlDateTime STime; datetime time_current=TimeCurrent(STime); Print("Time Current ",time_current," day of week ",EnumToString((ENUM_DAY_OF_WEEK)STime.day_of_week)); datetime time_local=TimeLocal(STime); Print("Time Local ",time_local," day of week ",EnumToString((ENUM_DAY_OF_WEEK)STime.day_of_week)); }
Is there any function to return the day of the week of today?
Like: Sunday, Monday, Tuesday...
Try this if you're a stickler for results like "Friday" instead of "FRIDAY"
void OnStart() { //--- MqlDateTime today; TimeCurrent(today); Print( "Today is " + DayOfWeek(today.day_of_week) ); Print( "Today is " + EnumToString((ENUM_DAY_OF_WEEK)today.day_of_week) ); } //+------------------------------------------------------------------+ string DayOfWeek(int dow){ string day[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; return day[dow]; }
Haha, thats been asked 2 years ago.
here, for reliability, i suggest using modulo operator on the array indexing like this:
const string DayOfWeek(const uchar dow){ static string day[7] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; return day[(dow%7)]; }
Is there any function to return the day of the week of today?
Like: Sunday, Monday, Tuesday...