MQL5 - Return the day of the week

 

Is there any function to return the day of the week of today?


Like: Sunday, Monday, Tuesday...

 
Guilherme Mendonca:

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

MQL5 Programming Basics: Time
MQL5 Programming Basics: Time
  • www.mql5.com
MQL5 offers a number of simple functions for working with time and you should not find it difficult getting familiar with them. The range of tasks that require use of date and time is quite small. The main tasks are: To perform certain actions at a given point of time (Fig. 1). These may be actions performed at the same time each day or at a...
 
Kenneth Parling:

Something like this maybe.....you can read more about this here

I tryied it, but still not working.

 
Guilherme Mendonca: I tryied it, but still not working.
"Doesn't work" is meaningless — just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires — meaningless.
 
Guilherme Mendonca :

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
Files:
DayOfWeek.mq5  5 kb
 
Vladimir Karputov:

The code:

Startup Result:

Thank You very much! 
 
Guilherme Mendonca :
Thank You very much! 

I fixed the error and replaced it with version 1.001

 
Vladimir Karputov:

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));
  }
 
Guilherme Mendonca:

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];
}
 
rogue-ichiban:

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)];
}
 
Thanks guys for your reply!