![]() |
| |||||||
| Sponsored Links |
![]() | « Previous Thread | Next Thread » |
| | LinkBack | Thread Tools | Display Modes |
|
#1
| ||||
| ||||
| hi i want to create a file name in below format.any idea how to do this/ • The file name will be of the form: • Company Code (1A) – derived from the unit company code • “W” + week number (2N, 01-53) – counting from first week in January • “D” + day of week number (1N, 1-7) – counting from Monday • Year number (2N) • “.txt” E.g. “RW01D0108.txt” is the file from Monday 31st December 2007 (1st Jan was a Tuesday) sorry for cross post.
__________________ Love is physical attraction and mental destruction |
| Sponsored Links |
|
#2
| ||||
| ||||
| Try this:- Code: Dim filename, company_code, week_no, day_no, year_no, date_today
date_today = "31/12/2007"
company_code = "R"
week_no = datediff("ww","01/01/2008",date_today,vbMonday,vbFirstJan1) + 1
if week_no<10 then week_no = "0" & week_no
day_no = "0" & weekday(date_today,vbMonday)
year_no = right(year(date_today),2)
filename = company_code & "W" & week_no & "D" & day_no & year_no & ".txt"
|
|
#3
| ||||
| ||||
| RR Thanks for reply.can u explain me what this codes are doing.why u are taken this date "01/01/2008". |
|
#4
| ||||
| ||||
| we should use DatePart or DateDiff for this stuff. |
|
#5
| ||||
| ||||
| I have added notes to each line of the code. Code: 'declare variables
Dim filename, company_code, week_no, day_no, year_no, date_today
'set date to today or whatever date you want manually or from a form element
date_today = "21/07/2008"
'set company code manually or from a form element
company_code = "R"
'calculate datediff in weeks (ww) between date at beginning of the year and date set above, based on Monday being first day of week. Then add 1 to the value as it starts at 0 not 1.
week_no = datediff("ww","01/01/2008",date_today,vbMonday,vbFirstJan1) + 1
'if the number of weeks is less than 10 then add a zero to the front
if week_no<10 then week_no = "0" & week_no
'find the day of the week from the date set above, based on first day of the week being Monday
day_no = "0" & weekday(date_today,vbMonday)
'take the last 2 digits of the year from the date above
year_no = right(year(date_today),2)
'concatenate everything together into a filename variable.
filename = company_code & "W" & week_no & "D" & day_no & year_no & ".txt"
Hope that makes more sense. |
|
#6
| ||||
| ||||
| ok still i have doubt over this line.why u are adding 1 here 'calculate datediff in weeks (ww) between date at beginning of the year and date set above, based on Monday being first day of week. Then add 1 to the value as it starts at 0 not 1. week_no = datediff("ww","01/01/2008",date_today,vbMonday,vbFirstJan1) + 1 |
|
#7
| ||||
| ||||
| Sorry, my brain's not quite working right today... ![]() If you had a date in the first week of the year, using datediif would return 0, so I added 1 to it, ie it would be in week 1. You are right, you could just use datepart. Code: week_no = datepart("ww",date_today,vbMonday,vbFirstJan1)
|
|
#8
| ||||
| ||||
| ok let me try as per my need.Thanks a lot RR. |
|
#9
| ||||
| ||||
| hi E.g. “RW01D0108.txt” is the file from Monday 31st December 2007 (1st Jan was a Tuesday) see in example . if we put date 31/12/2007 then in this case file name would be “RW01D0108.txt” .but RR when i m using your code it returns me this file name RW53D0107.txt. |
|
#10
| ||||
| ||||
| I think, technically, it is correct in that the 31st would fall in to week 53 of 2007, although week 53 would only contain 1 day. I think you could add some sort of filter for week 53 instances like:- Code: .
.
.
week_no = datepart("ww",date_today,vbMonday,vbFirstJan1)
'if the number of weeks is less than 10 then add a zero to the front
'find the day of the week from the date set above, based on first day of the week being Monday
day_no = "0" & weekday(date_today,vbMonday)
'take the last 2 digits of the year from the date above
year_no = year(date_today)
if week_no = 53 then
week_no = 1
year_no = year_no+1
end if
if week_no<10 then week_no = "0" & week_no
year_no = right(year_no,2)
filename = company_code & "W" & week_no & "D" & day_no & year_no & ".txt"
<edit>I just tried this and I think you can get an instance of a week 54, on 31/12/2012. In the code above, that would make the previous week, 24-30 Dec week 1 of the following year, which would be wrong. I can't quite understand how you want to handle these events.</edit> Last edited by richyrich; July 21st, 2008 at 10:01 AM. |
![]() |
|
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|