Convert Date to Week Number

 

Hello Everyone,

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

Thank you for your Time

EK

 

try this

MQLTALK

 
codersguru:

try this

MQLTALK

Codersguru, linking to your site and answering the question there is extremely bad netiquette. You don't even have a link back to MQL4.com on your site!! Take warning that this can be considered as violation of rule 3. Please stop doing it.
 
gordon:
Codersguru, linking to your site and answering the question there is extremely bad netiquette. You don't even have a link back to MQL4.com on your site!! Take warning that this can be considered as violation of rule 3. Please stop doing it.

It's OK!

Here's my answer:

Try this code:

datetime cDate = TimeCurrent();
int WeekNumber = TimeDayOfYear(cDate) / 7;
Alert(WeekNumber);

 
codersguru:

try this

MQLTALK


Hello Codersguru,

At first I thought that this type of solution wouldn't work because of Leap year and that Sunday starts the week, but for what I need I think it will do the job.

Thank you

EK

 

Hello to All,

Just wanted to update this info, I am sure that this code as it it will not work correctly.

When I use this to get the day number within the year it returns 309

Print(TimeDayOfYear(StrToTime("2010.11.05")));

now a date of 2010.11.04 returns 308, and 2010.11.03 returns 307

307/7 = 43.85

308/7 = 44 which indicates a new week has begun but 2010.11.04 = a Thursday

So with the solution that Codersguru posted it will not work correctly. Now maybe if the year started on Jan 1st and it is not a leap year this might work.

Anyone have a solution?

Thank you for your Time

EK

 
Emerald_King:

Hello to All,

Just wanted to update this info, I am sure that this code as it it will not work correctly.

When I use this to get the day number within the year it returns 309

Print(TimeDayOfYear(StrToTime("2010.11.05")));

now a date of 2010.11.04 returns 308, and 2010.11.03 returns 307

307/7 = 43.85

308/7 = 44 which indicates a new week has begun but 2010.11.04 = a Thursday

So with the solution that Codersguru posted it will not work correctly. Now maybe if the year started on Jan 1st and it is not a leap year this might work.

Anyone have a solution?

Thank you for your Time

EK


but it's = 44 weeks from the begining of the year

what's your point

TimeDayOfYear(StrToTime("2010.01.01")) = friday

 
Emerald_King:

Just wanted to update this info, I am sure that this code as it it will not work correctly. [...]

Partly depends how you want to define weeks. Taking two examples more or less at random, ISO 8601 defines weeks as beginning on a Monday, whereas Google (e.g. in Analytics) usually defines a week as beginning on a Sunday.

If January 1st is a Friday, is that in week #0 or #1?
 
Emerald_King:

[...] Anyone have a solution?

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

 

Additional note: if it's running in a looped script over a long period of time (>days), then the following change might be needed:

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

   return(iWeek);
   }

The problem is that both DayOfWeek() and DayOfYear() return the days of the last known server time at the moment of program start (similar to Minute(), Hour() functions) and NOT as stated in the documentation (see here -> https://www.mql5.com/en/forum/122601). This fixes this problem...

 
gordon:

Additional note: if it's running in a looped script over a long period of time (>days), then the following change might be needed:

The problem is that both DayOfWeek() and DayOfYear() return the days of the last known server time at the moment of program start (similar to Minute(), Hour() functions) and NOT as stated in the documentation (see here -> https://www.mql5.com/en/forum/122601). This fixes this problem...

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