Convert Date to Week Number - page 2

 
Emerald_King:

I would like to thank everyone who took the time to respond to my query and for posting solutions. None of them worked for me which is probably due to me not stating what I am seeking. I think I will just take a date and then scroll back bar by bar to find the previous Friday for me that is the end of the week and a new week begins on Sunday.

Again Thanks to all for your help is getting my thinking clearer.

EK


I'm very sorry to hear, but i think that from the whole topic doesn't sound, that what you want to know, is when is Friday and Sunday before Current DayOfWeek()
 
int start()
   {
   //-----
   Sun_Befor();
   Fri_Befor();
   Alert(Sun_Befor() + " Days Ago Was Sunday");
   Alert(Fri_Befor() + " Days Ago Was Friday");

return(0);
}

int Sun_Befor()
   {
   int i = DayOfWeek();
   int k = 0;
   while (i != 0)
      {
      i = i -1;
      k++;
      }
   return(k);
   }
   
int Fri_Befor()
   {
   int i = DayOfWeek();
   int k = 0;
   while (i != 5)
      {
      i = i +1;
      k++;
      }
    int z = MathAbs(k-7);
   return(z);
   }
 

and of cource

(Sun_Befor()+ 2) == Fri_Befor

int start()
   {
   //-----
   Sun_Befor();
   Alert(Sun_Befor() + " Days Ago Was Sunday");
   Alert((Sun_Befor()+2) + " Days Ago Was Friday");
return(0);
}

int Sun_Befor()
   {
   int i = DayOfWeek();
   int k = 0;
   while (i != 0)
      {
      i = i -1;
      k++;
      }
   return(k);
   }

(I like it more, less code)

 

Hello to All,

Again thank you for taking the time to respond in this thread.

My ultimate goal is to know when a week has passed.

int iDay  = ( TimeDayOfWeek(ThisTime) ) % 7 + 1;                    // convert day to standard index (1=Mon,...,7=Sun)
int iWeek = ( TimeDayOfYear(ThisTime) - iDay + 10 ) / 7;                // calculate standard week number

This is the modified code that Gordon posted and it is working for me.

Thank you

EK

 

Hey I think I have an answer:


datetime FN = D'2019.12.31 00:00:00';// FN is the end of the year date in datetime.

int FNint,Nowint,WeekNo;


void OnInit()

{

FNint = (int)FN;// Convert FN to an integer value

Nowint = (int)TimeLocal();// Get the current date/time as an integer value

WeekNo = 52 - ((FNint - Nowint)/604800);

// There are 604800 seconds in 1 week  (60 x 60 x 24 x 7)

// The time difference in seconds between now and the end of the year is FNint - Nowint

// Divide the time difference by 604800 to get the number of weeks till year end

// Subtract the number of weeks till year end from 52 to find the number of the current week

Comment(WeekNo);

}

 
gordon #:

See here -> https://www.mql5.com/en/forum/122061/page2#comment_3212721 (posted it a long time ago, so please verify if it ain't buggy...).

it works till now. Thanks!

(even taking into consideration that each new build of MetaTrader4 have enough new stuff - and usually it's better to check your code for correctness with each new build release)

p.s. perhaps surprises can arise in leap-years? - see here
How to find the number of days in the current month using MQL4?
How to find the number of days in the current month using MQL4?
  • 2017.03.17
  • Boris Morozov Boris Morozov 115 5 5 bronze badges
  • stackoverflow.com
Is there a built-in function or some elegant way to get the number of days in the current month using MQL4? Or alternatively, is there a way to detect the last Monday of the current month?
 
Scott Watson:

Hello Everyone,

Does anyone know how I can convert a date to a week number in mql4?

Thank you for your Time

EK

Hi, I know this post is old. But I think it has not been answered yet ...!

so this is my solution

// global variables
int WeekNumber;
//---
int WeekCounter()
{  
  
  for(int i=0;i<=52;i++)
    {      
     datetime week = iTime(_Symbol,PERIOD_W1,i);
     if(TimeYear(week) != Year())
       {
          WeekNumber = i;
          break;
       }
    }
  return WeekNumber;
}

Good Luck.

MRah76

 
You know there are specific algorithms to calculate the woy, take a look here:


Also, as usual, the US have again managed to screw the rest of the world by having their own standard.