Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to calculate Date?

Status
Not open for further replies.

neomorpheus

Programmer
Mar 9, 2001
47
US
I need to find the date that was two months back from current date (today). It also needs to keep in mind 30 or 31 days in a month.

I thought about calculating it by subtracting 60 days, but it doesnt give me an exact/ correct date. Since the data I am going to display is time-specific, i need it to be absolutely correct.

Please help.... suggestions welcome.

Thanks
 
let's say the current date is april 29, 2003

which date would you like the function to return?

also, do you want to use this date in a database query? if so, you may be better off using a database function than a coldfusion function


rudy
 

<cfset todaysDate = now()>
<cfset lastMonthsDate = dateAdd(&quot;m&quot;, -1, todaysDate)>
<cfset date2MonthsAgo = dateAdd(&quot;m&quot;, -2, todaysDate)>

and if you want to use this date in a query then just use

createODBCDate(lastMonthDate) ------------------------------------------------------------------------------
brannonH
if( !succeed ) try( );
 
good one, brannon, but did you notice that april 29 and april 30 both yield the same date, feb 28?

that's only &quot;absolutely correct&quot; if you allow it :)
 
Sounds like that's what he wanted, since he said subtracting 60 days wasn't giving him the correct date. Why can't all months just have 30 days, and be done with it ;-)


-Carl
 
Heeding to the advic eon one of the posts, I did it using a databse function-

****************************************************
CREATE PROCEDURE [GetDateRange] AS
DECLARE @date DATETIME,
@fdate DATETIME,
@ldate DATETIME

SELECT @date = Getdate()
SELECT @ldate = @date

--For the months Jan and Feb, we hardcode to prevent
--negative values

If datepart( mm, @date) = 1
Begin
SELECT @fdate = '11/' + Convert(varchar(2),datepart( dd, @date)) + '/' + Convert(varchar(4),datepart( yyyy, @date))
End
--
If datepart( mm, @date) = 2
Begin
SELECT @fdate = '12/' + Convert(varchar(2),datepart( dd, @date)) + '/' + Convert(varchar(4),datepart( yyyy, @date))
End

-- For month grater than Feb, use the formula below

If datepart( mm, @date) > 2
Begin
SELECT @fdate = Convert(varchar(2),(datepart( mm, @date)-2)) + '/' + Convert(varchar(2),datepart( dd, @date)) + '/' + Convert(varchar(4),datepart( yyyy, @date))
End

--
SELECT 'FirstDay' = @fdate,
'LastDay' = @ldate

***********************************************************

Thanks for your feedback.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top