is this the right way to get the week if the weeke...
# cfml-general
g
is this the right way to get the week if the weekending is sunday so it starts from monday and ends on sunday <cfset dtToday = Now() /> <cfset dtLastWeek = (Fix(dtToday) - 8) /> <cfset dtStartOfWeek = DateAdd("d", 1, dtLastWeek) /> <cfset dtEndOfWeek = DateAdd("d", 6, dtStartOfWeek) /> <cfdump var="#dtStartOfWeek#" /> <cfdump var="#dtEndOfWeek#" />
g
It isn't clear what you're asking for here; • Is it the start and send of "last" week - as of today? • Is it the start and end of last week - regardless of what day "today" is? Your current code will only work if literally "today" is a "Monday" Eg: If you change
now()
to
<cfset dtToday = createDate(2024,04,04) />
Then your start of week and end of week are Not Monday ->Sunday They are Thursday -> Wednesday.
Which might be exactly what you're after - if what you're after is exactly: Starting from today - Go back a week and give the two dates that are a week apart.
If it is specifically; Regardless of what day today is... Tell me what the dates were for Monday of last week -> to the very "next" Sunday that follows (last week's) Monday... Then you're current code - does NOT do that.
g
I have only the weekend which is Sunday in this case so going back I will get last Monday to Sunday as late as week
Weekend is a variable and comes from config table which has Sunday as its value
g
That doesn't really help (me to help you). Can you give us the preceding lines of code? So we can get the context of what leads into your code snippet?
g
Sure
i can say this is the screenshot i have based upon the below its value coming from DB, could be friday/saturday or sunday but lets deal with sunday first, because sunday is solved other two will be solved by itself now in the case of Sunday weekday starts on Monday and Ends on Friday, with sat/sun as weekends so a total of cycle moves from Monday to sunday and based upon this scenario, if considering today is wednesday and its last week ended on sunday and weekstart will be the monday of the week, not this monday but previous one i believe i made it clear this time
g
Your screenshot doesn't help any. Sometimes a question on a forum doesn't need any background. • How do I format a date (that is a string) into a string that is the following format, "YYYY-mm-dd" There is no need in this instance for anything extra. A person helping you - doesn; thave to guess anything and they give you any number of ways to format a date. However in this thread, you asked; is this the right way to get the week if the weekending is sunday so it starts from monday and ends on sunday The code you posted will only ever work if it is run on a Monday. If I exercise the code you wrote on a Wednesday (or any other day that is NOT a Monday, then your code WILL NOT give you the results you expect - it is wrong on every day of the week that is not Monday! If for whatever reason you can't give us the code you're using - then you'll need to give us more information, in the question you ask - as opposed to the code you provide. What are you trying to achieve. What are the constraints if any? What have you tried? What did you expect to see as a result? What did you get as a result? Let me give you an example of a user story that I have "guessed" - about your form / code. User story for what I want Today is Monday. I ONLY use this form on a Monday. I am using a form to enter in the amount of hours that I worked last week. My start "day" is always a Monday. My end day is always the following Sunday. When I am using the form I can see Monday -> Sunday. What I need a hand with is this the right way to get the week if the weekending is sunday What I have tried
Copy code
XXX
...
The results I expect To see a date this last Monday and another date that is last Monday. The results I see A date this last Monday and another date that is last Monday. My actual question It seems right - but is it? Now... I have a clear understanding of the requirements. And I can reply with; "Yes" your code will work for your requirements. So my answer is sound. But YOUR original question - doesn't state that it will only ever be used on a Monday and no other day of the week ever needs to be considered. As a result - "I" have to guess... "What happens if the user, uses this code on some day that is not a Monday?" I better add in some more information - because of the "guesses" I have made. OR I just reply with YES that works fine. And tomorrow - you start getting complaints - because your screen no longer shows a week that starts on a Monday and finished on a Sunday. Because TODAY is Tuesday - your screen shows a week with a start day of Tuesday, that ends on a Monday. And that could be EXACTLY what you want. ALWAYS show me two dates that are week apart - where the start DAY is ALWAYS 1 week ago. it just isn't clear, from what you have written.
g
Great reply But honestly I do not have much context to show I get the value from Deb and I have get the previous week I maybe not able to convey my message or question I asked possibly but I tried to be very precise here to show what I have Apart from that I tried the code it’s not something that I asked before even trying I added a code which it seems to be that could work but it’s not right so asking for advise is not bad here
g
I am sorry, but that still isn't helping. Tell me which scenario is right? Scenario One: 1. I get a date from a db query. 2. I need to work out 7 days ago from THAT date [1] 3. I need to work out "yesterday" from THAT date [1] (The solution MUST always answer with 7 days ago / 1 day ago. OR Scenario Two: 1. I get a date from a db query. 2. I need to work out, what was the date of "Monday" last week before [1] 3. I need to work out the "Sunday" that is after the Monday [2] (The solution MUST always answer with Monday/Sunday) They're different questions. They have different answers. Your solution will work for Scenario One - only.
g
Senario 2
g
Right well in that case - your code will not work.
a
dayOfWeek
is likely to help you here: https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-functions/functions-c-d/DayOfWeek.html The general algorithm is gonna be along the lines of: • get the DOW of the date in question; • offset that date back to the previous monday (which will be DOW = 2); • offset that by -1 weeks.
👆 2
👍🏼 1
c
Additionally, you might look into creating a Date Dimension or Calendar table in your database that you can join in your queries to figure out things like what week, what month, what day of week, etc. I find this really helpful. For SQL Server, look at: https://www.mssqltips.com/sqlservertip/4054/creating-a-date-dimension-or-calendar-table-in-sql-server/
👍🏼 1
g
@cfvonner I have always just used built in functions / math to do this. It never, ever occurred to me to use actual data. How does it perform from a speed perspective? - as opposed to using a CFML built-in-function (BIF)?
c
@gavinbaumanis Pretty dang fast in my experience. Since the table is pre-populated and static (rather than running DB-level functions to provide the data), joining it to any other table(s) and pulling whichever columns from it are needed doesn't appear to add any appreciable latency. I'd say the performance payoff multiplies if you are pulling multiple records from the database.
👍🏼 1
g
VERY interesting... This is one of those times (perhaps) where the solution is so damn obvious... But "I" never, ever thought of doing that. And I am currently doing a Masters of Data Science - where everything is about data!