MQL5 - Return the day of the week - page 2

 
Dominik Egert #:

Haha, thats been asked 2 years ago.

here, for reliability, i suggest using modulo operator on the array indexing like this:

Can i use this on MQL4?

 
Yes, this is "universal" code.


 
enum wkday  // enumerated constants
   {
      Monday=1,
      Tuesday,
      Wednesday,
      Thursday,
      Friday,
      Saturday,
      Sunday
   };


void OnStart()
  {
//---
   MqlDateTime theday;
   TimeToStruct(D'16.09.2021', theday);
   Print( "D 16.09.2021 is " + EnumToString((ENUM_DAY_OF_WEEK)theday.day_of_week) );
   
   // and bakward conversion (from name to number):
   wkday wdnum=Monday;
   Print(EnumToString(wdnum)+"="+IntegerToString(wdnum));
  }
//+---
 
JeeyCi #:

The values use in your wkday are incompatible with those in ENUM_DAY_OF_WEEK. Your Sunday is seven, but SUNDAY is zero.

Use the provided enumeration.

 
William Roeder #:

The values use in your wkday are incompatible with those in ENUM_DAY_OF_WEEK. Your Sunday is seven, but SUNDAY is zero.

this is exactly what I need - enumeration from Monday as one, and Sunday as 7 (different standards act in different countries)... and yes ENUM_DAY_OF_WEEK of MQL can have its own standard as mql-constant

 
William Roeder #:

The values use in your wkday are incompatible with those in ENUM_DAY_OF_WEEK. Your Sunday is seven, but SUNDAY is zero.

I found where it is in MT4:

folder MQL4/Tools/DateTime.mqh

string CDateTime::DayName(const int num) const
  {
   switch(num)
     {
      case 0: return("Sunday");
// ...
        }
}

I think, I can change here for necessary order for me... anyway thank you for the remark!

 
Vladimir Karputov #:

The code:

Startup Result:

Hi gurus,

i am stuck. 'able to obtain the date from OnStart but my problem is how to allow another function to access the said date and integrate the date into a string within a filename generated by OnStart. I'm trying to do this once within OnStart to simplify in order to avoid redundancies to allow multiple functions to access by generating filename only once. One of the function writes to a file( Excel) everyday and i have to rename the filename everyday manually. I am hoping to automate the file-naming. 

It seems onStart does not allow to accept/pass parameters in and out and it also doesnt allow to return a result such as : 

string filename = OnStart () { ....}
 
  1. JimSingadventure #:
    string filename = OnStart () { ....}

    Perhaps you should read the manual. OnStart does not return anything. Event Handling Functions - Functions - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
       How To Ask Questions The Smart Way. (2004)
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

  2. All you want should be inside OnStart.

    You haven't stated a problem, you stated a want. Show us your attempt (using the CODE button) and state the nature of your problem.
              No free help (2017)

    Or pay someone. Top of every page is the link Freelance.
              Hiring to write script - General - MQL5 programming forum (2018)

    We're not going to code it for you (although it could happen if you are lucky or the problem is interesting).
              No free help (2017)

  3. Why do you have to rename the file? Just create it with the proper name to start with.


 
William Roeder #:
  1. Perhaps you should read the manual. OnStart does not return anything. Event Handling Functions - Functions - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
       How To Ask Questions The Smart Way. (2004)
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

  2. All you want should be inside OnStart.

    You haven't stated a problem, you stated a want. Show us your attempt (using the CODE button) and state the nature of your problem.
              No free help (2017)

    Or pay someone. Top of every page is the link Freelance.
              Hiring to write script - General - MQL5 programming forum (2018)

    We're not going to code it for you (although it could happen if you are lucky or the problem is interesting).
              No free help (2017)

  3. Why do you have to rename the file? Just create it with the proper name to start with.


william, respect your coding expertise a lot but your reply to item 3 demonstrates you have not really attempted to read with understanding but jumps to conclusion quickly, often assuming the negative in people as in your reply to items 1 & 2. while you are not obliged to help me or anyone, your presumptions and are not helpful and better left unsaid and keep quiet if you do not wish to assist. 

btw, i'm not asking for a code but some general guidance and direction. And the reason why i ask is because i have read the documents and have attempted to code them but still lacks clarity.

lastly, i am writing a file to Excel everyday and it gets too large if i let it run for a few days with the same filename. i want the fileame to change everyday accoridng to date.

 
JimSingadventure #: . i want the fileame to change everyday accoridng to date.

So I ask again, why do you have to rename the file? Just create it with the proper name to start with.

Reason: