Weeknumber calculation

Borte

Member
Join Date
Feb 2004
Location
Norway
Posts
238
Hello guys!

It's been a while since last time I posted here. I haven't been working with plc's for a while but no I'm up to my ears... I like it!

I have ran into a problem with a printer. I have to print year, week, day of month and hour. The year, day of month and hour is no problem I can read that directly from the PLC clock but the week number is a little harder to get... I've been looking at calculations I found on the web but it's like 200 - 300 lines of code on a computer so I'm a little worried about how this code will look if I convert it to the plc... 500 lines of code????

Have anyone in here calculated week numbers using the plc?

Btw: I'm using a Siemens 315-DP2 plc

Regards
Borte
 
I'm afraid I don't have a week number calculation handy at the moment, but be aware that there are different ways of calculating the week number. I can't remember the exact details, but if I remember correctly in the US week number 1 is the week in which Jan 1st falls. In Germany, it is the first week with four or more days in it.

I fell into this trap a few years back and ended up with my week numbers being off by one. The details above may not be exactly correct, but the main point is to be aware that there are different ways of handling week 1.
 
I'm aware of the different ways of calculating it. I've found all the different ways of calculating it on the web. My consern was that it seemes to be a big calculation for a plc. So I was hoping someone had a "work around"...
 
Borte

You can get the week day number from the real time clock of the 315.

Using SFC1, you can read out the RTC, then manipulate the bytes to get the 'parts' contained within.

Byte 1 = current year
Byte 2 = current month
Byte 3 = Current day
Byte 4 = hour
Byte 5 = minute
Byte 6 = seconds
Byte 7 (and first 4 bits of byte 8) = milliseconds
Byte 8 (last 4 bits) = day of the week.

So to get the day of the week (1 = sunday....7 = Saturday) you will need to mask off the first 4 bits..

Example

L #byte8
L W#16#F
AW
T #day_of_week

Search this forum for the keyword 'SFC1' and you will find some examples of reading the RTC from an S7 PLC.

Paul

Edit... Ignore this post, I misread what you were after, I thought you wanted the day of the week number, not the actual week number.. Maybe you could just use the day of the week to increment a counter, I.E whenever #day_of_week = 1, increment counter by 1, you will just have to start the counter at the current week number in the year, then when the counter = 52, reset it and start again.
 
Last edited:
I'm aware of the day of week in the DT data type, but I need to know the number of the week in the year.
 
Borte said:
I'm aware of the day of week in the DT data type, but I need to know the number of the week in the year.

I realised that after I posted, which is why I then edited my original reply, perhaps you were replying while I was typing my edit.

The edited part is after my name.

Paul
 
RMA said:
I don't know how UK calendar weeks are numbered Paul, but in Germany they have 53 weeks.

Germans must be strange people Roy. :rolleyes:

I suppose they have 371 days in the year not 365 like the rest of the world..

Definately 52 weeks in a year

Paul
 
Germans must be strange people Roy. :rolleyes:



Well the German banks are certainly a bit strange, their year has 360 days and each month has thirty days, so on my bank statements I have transactions occurring on Feb. 30th!

However, getting back on topic, since 365 days is a tad over 52 weeks, (1 day to be precise) you have two possible ways of handling this - either you have a long week with eight (or more, depending on how week 1 is defined) days in it, or else you allow your weeks a maximum of seven days (and since most wall-calendars are organised in weeks with the days of the week along the top, this is most convenient) and then like it or not your year has week 53!

Cheers

Roy
 
It's not very elegant, but one solution would be to make a lookup table of each year. If the system has a PC-based HMI, then you could store the lookup tables for the life of the machine (say 20 - 25 years) on the PC and load them into the PLC automatically on year change.
 
I think that's the route I'm gonna take... It's like you said RMA not very elegant but at least it's less code and less work... But on the other hand if I created a block for it now, I would have it ready for next time...
 
IF you can get day of the year
AND it is the Julian date ie 250 (or what ever)
WHY NOT divide the Julian by 7 to get week of the year?
Granted you will get a decimal value as a result (250/7 = 35.71xx)
BUT you have the week now you only need to decide where to round.
Just for idle curiosity why do you need the day of the week??

Dan Bentler
 
It's just occurred to me that you don't need a look-up table for the whole year. All you really need is to define when week 1 starts and after that the remaining weeks are a relatively trivial calculation (apart from leap-years, but now that we've got 2000 behind us, even that is fairly easy to handle now).
 
RMA said:
and then like it or not your year has week 53!

I know where you are coming from with this Roy, but at a very early age we are all taught 365 days/ 12 months/ 52 weeks etc...

I agree that in 2004 there was indeed 53 weeks, taking a few days off 2005, but in 2005 there are only 52 weeks, not 53. That is taking the start of the week as Monday.

Week 1 in 2005 started on Monday 3rd January
Week 52 in 2005 starts on Monday 26th December and ends on Sunday 1st January 2006.

When I worked for British Rail we had 13 months in the year, well that's 13 pay months, we were paid on a 4 weekly (month) basis, so we got paid 13 times and I only paid my bills (mortgage) 12 times a year, so in one calender month I got 4 weeks pay to do with what I wanted.. I liked that system.

There's that 52 weeks in the year again... 13*4 = 52!

It's that odd day, that throws it out.

Paul

P.S I was not being disrespectful when I posted 'Germans must be strange people'
 

Similar Topics

Hello everybody, I believe that I will find experts here who have already solved a similar topic. I need to calculate the length of the...
Replies
7
Views
324
Does this instruction calculate values during a single scan, or does it require number of scans based on element count in the array? For Example...
Replies
3
Views
113
Hi!! I'm looking for Temperature rise calculation software from Rockwell, I just download "Product selection toolbox 2022" but this software is...
Replies
1
Views
210
I have a bunch of tags in Historian/VantagePoint that off by one decimal point. I looked into the HMI displaying the same number, and the HMI is...
Replies
2
Views
115
I might be overthinking my problem, but I need some help. I am trying to write a formula in my plc to calculate the footage of paper being...
Replies
6
Views
627
Back
Top Bottom