Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Dealing with Yesterday

Contributor ,
Jul 19, 2007 Jul 19, 2007
Hi all,

I have a slice of code that I created that goes and grabs files from a website and processes them.

I have discovered that 'yesterday' is becoming an issue. What if you are on a Monday and need to process files for Friday? I got that far, but now I am dealing with month changes. and finally year changes.

Surely someone has futzed with something like this before.

My question is how would you guys deal with the date issue. If you want I can post my code...it is not to long, but if requested I will post it.
1.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jul 19, 2007 Jul 19, 2007
What is the issue? You can use the DayOfWeek() function to get the day of week (1-Sunday, etc). Then use DateAdd() to deduct the necessary number of days.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jul 20, 2007 Jul 20, 2007
Yes. Find DayOfWeek(), if it is 2(Monday) the deduct 3 days from current date using DateAdd(). Deduct 1 other weekdays. Similarly for Saturday and Sunday (if you need)

Then use the date after deduction to grab files from website.

Thanks
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jul 20, 2007 Jul 20, 2007
Ok,

Here it is...for everyone who need to find yesterday's date, and only consider business days Monday through Friday. It's ugly but it works...

It also deals with the month and year changes.


<!---Setting month day year variables--->

lets test this:

<cfset testmode = 0>

<cfif testmode EQ 1>

<cfset getmonth = "06">
<cfset getday = "02">
<cfset getyear = "08">
<cfset getyearAlt = "2008">
<cfset dateMasher = getmonth & getday & getyearAlt>
<cfset setDayOfweek = dayofweek("2008-06-02 07:38:51")>

<cfelse>

<cfset getmonth = dateformat(now(), "mm")>
<cfset getday = dateformat(now(), "dd")>
<cfset getyear = dateformat(now(), "yy")>
<cfset getyearAlt = dateformat(now(), "yyyy")>
<cfset dateMasher = getmonth & getday & getyearAlt>
<cfset setDayOfweek = dayofweek(now())>

</cfif>
<!---Now we are going to determine what yesterday's date was using a series of if statements. We have to consider if it is the new year or a now month and then set the yesterday's date to either yesterday or to friday's date (if it is a monday)--->

<cfoutput>
#now()#<br />
#DateAdd('yyyy', -1, Now())#<br /><br />
total date: #datemasher#<br><br>
day of the week is: #setDayOfweek#<br><br>
Month: #getmonth#<br>
Day: #getday#<br>
Year: #getyear#<br>

<!---if year day is 1 and month is 1 then
date is 1231 and last year's year--->
<cfif testmode EQ 1>
<cfif setDayOfWeek EQ 2 AND getmonth EQ "01" and getday EQ "01">

<cfset dateMasher = "1231" & #dateformat(DateAdd('yyyy', -1, "2008-06-02 07:38:51"), "yy")#>

<cfelseif setDayOfWeek EQ 3 OR setDayOfWeek EQ 4 OR setDayOfWeek EQ 5 OR setDayOfWeek EQ 6 AND getmonth EQ 1>

<cfset getmonth = #dateformat(dateadd("m", -1, "2008-06-02 07:38:51"), "mm")#>
<cfset getday = #dateformat(dateadd("d", -1, "2008-06-02 07:38:51"), "dd")#>
<cfset getyear = #dateformat(dateadd("yyyy", -1, "2008-06-02 07:38:51"), "yy")#>
<cfset dateMasher = getmonth & getday & getyear>
</cfif>
mash the date: #dateMasher#
Month: #getmonth#<br>
Day: #getday#<br>
Year: #getyear#<br>

<!---<cfabort showerror="stopping here for debugging">--->
<cfelse>
<cfif setDayOfWeek EQ 2 AND getmonth EQ "01" and getday EQ "01">

<cfset dateMasher = "1231" & #dateformat(DateAdd('yyyy', -1, now()), "yy")#>

<cfelseif setDayOfWeek EQ 3 OR setDayOfWeek EQ 4 OR setDayOfWeek EQ 5 OR setDayOfWeek EQ 6 AND getmonth EQ 1>

<cfset getmonth = #dateformat(dateadd("m", -1, now()), "mm")#>
<cfset getday = #dateformat(dateadd("d", -1, now()), "dd")#>
<cfset getyear = #dateformat(dateadd("yyyy", -1, now()), "yy")#>
<cfset dateMasher = getmonth & getday & getyear>
</cfif>
</cfif>
<br /><br />

<!---if day of week is 2 and the day is the 1st of the month
get get friday day value from last week.--->

<cfif testmode EQ 1>

<cfif setDayOfWeek EQ 2>

<cfif getday EQ "01" OR getday EQ "02" OR getday EQ "03">

<cfset getday = dateformat(dateadd("d", -3, "2008-06-02 07:38:51"), "dd")>
<cfset getmonth = dateformat(dateadd("m", -1, "2008-06-02 07:38:51"), "mm")>

<cfelse>

<cfset getday = dateformat(dateadd("d", -3, "2008-06-02 07:38:51"), "dd")>

</cfif>

<cfelseif setDayOfWeek EQ 3 OR setDayOfWeek EQ 4 OR setDayOfWeek EQ 5 OR setDayOfWeek EQ 6>

<cfif getday EQ "01">

<cfset getday = dateformat(dateadd("d", -1, "2008-06-02 07:38:51"), "dd")>
<cfset getmonth = dateformat(dateadd("m", -1, "2008-06-02 07:38:51"), "mm")>

<cfelse>

<cfset getday = dateformat(dateadd("d", -1, "2008-06-02 07:38:51"), "dd")>

</cfif>

</cfif>
<cfelse>

<cfif setDayOfWeek EQ 2>

<cfif getday EQ "01" OR getday EQ "02" OR getday EQ "03">

<cfset getday = dateformat(dateadd("d", -3, now()), "dd")>
<cfset getmonth = dateformat(dateadd("m", -1, now()), "mm")>

<cfelse>

<cfset getday = dateformat(dateadd("d", -3, now()), "dd")>

</cfif>

<cfelseif setDayOfWeek EQ 3 OR setDayOfWeek EQ 4 OR setDayOfWeek EQ 5 OR setDayOfWeek EQ 6>

<cfif getday EQ "01">

<cfset getday = dateformat(dateadd("d", -1, now()), "dd")>
<cfset getmonth = dateformat(dateadd("m", -1, now()), "mm")>

<cfelse>

<cfset getday = dateformat(dateadd("d", -1, now()), "dd")>

</cfif>

</cfif>

</cfif>

<cfset dateMasher = getmonth & getday & getyear>
#dateMasher#<br />

</cfoutput>

PM me if you have any questions.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 20, 2007 Jul 20, 2007
J*sus on a stick, what the f*** are you doing?

If the DOW is 3-7 (ie: Tues-Sat), then the previous working day is just the
current date -1 day.

If it's 1-2 (Sun, Mon), then subtract (1+DOW) days from the current date.

It's an if/elseif, and two calls to dateAdd().

:-|

--
Adam
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jul 20, 2007 Jul 20, 2007
Haha,

Adam, I am grabbing files on dayofweek 2,3,4,5,6 1,7 are out.

So I am searching for Friday's files on Monday when i am running it.

Hmm, now that you mention it I could run the Script on Monday-Saturday (that would trim the logic down).

;-)

Frank
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jul 23, 2007 Jul 23, 2007
I have a solution for this here:

http://www.bennadel.com/index.cfm?dax=blog:850.view
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jul 25, 2007 Jul 25, 2007
Assuming 7.0.1 HF2+ seems like a good option. For older versions see bug
http://kb.adobe.com/selfservice/viewContent.do?externalId=aae43964&sliceId=2
61361 DateAdd("w") (weekday) can result in weekend date returned
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 25, 2007 Jul 25, 2007
Hi all,

I have one query regarding this issue, that ourmain issue is for finding out the last day for which the user did some work in the application.
Can it be resolved by querying tables and searching for the maximum date less than current date.
Sorry, if i said something wrong.

Regards
Amit
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jul 26, 2007 Jul 26, 2007
Guys,

Let me post my revised code...It is WAY better than that stuff I wrote above.

<cfset getmonth = dateformat(DateAdd("w", -1, now()), "mm")>
<cfset getday = dateformat(DateAdd("w", -1, now()), "dd")>
<cfset getyear = dateformat(DateAdd("w", -1, now()), "yy")>
<cfset alterTime = getmonth & getday & getyear>

You can output the alterTime variable to see the results.

Basically, what Ben showed me is that the "w" parameter of the DateAdd function will automatically jump to the previous Friday if it is running on Monday and grab the date. Then I format the date to display in mmddyy.

Also note: I am running Coldfusion 8 and as one person mentioned you should have Hotfix 2 applied if you are using MX 7.

So to answer your question Amit, if you are recording the date that someone did work to a DB table like time stamp submission you could compare to current date (that is if maximum date is the time stamp you are are capturing).

I will tell you that you have to format the date if you are using now() to compare because it comes out like this:

2008-06-02 07:38:51

and if your time stamp date is this:

060208 or 06022008 then it will fail to compare properly (that might be obvious to you).

Frank

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 26, 2007 Jul 26, 2007
> Let me post my revised code...It is WAY better than that stuff I wrote above.
>
> <cfset getmonth = dateformat(DateAdd("w", -1, now()), "mm")>
> <cfset getday = dateformat(DateAdd("w", -1, now()), "dd")>
> <cfset getyear = dateformat(DateAdd("w", -1, now()), "yy")>
> <cfset alterTime = getmonth & getday & getyear>

dYesterday = DateAdd("w", -1, now());
alterTime = dateFormat(dYesterday, "MMDDYY");

Saves some typing and some unnecessary processing.

I'm a bit concerned that your datestamp fields store the date as a six
digit number (or a six digit string) though. Why aren't you using a date
or a time stamp?

--
Adam
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jul 26, 2007 Jul 26, 2007
LATEST
Adam,

Thanks for the alterTime rework that is a nice refinement. I was doing it the previous way because I was doing some string replace stuff..

About the timestamp question. This tool is used to retrieve files from a website.

The files are named:

mmddyy.biff
mmddyy.make
mmddyy.seft

all the extensions are weird.

because they publish files for yesterday, I have to do this date thing. So t's not for a time stamp.

:)

Frank
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources