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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Since The First Of The Year Date Issue 1

Status
Not open for further replies.

3dColor

Programmer
Jan 10, 2006
240
US
I am trying to generate a ODBC date so I can conditionally run a query based off the 1st of the year.

But I am having a strange issue.

Here is what I have and it works:
<cfset today = CreateODBCDate(Now())>
<CFSET sinceThe1st = CreateDateTime(#year(today)#, #month(monthCurrentPast)#, #Day(FirstDayOfMonth(Now()))#, #hour(today)#, #minute(today)#, #second(today)#)>
<!--- WARNING: when you place a 1 in for month(1) below it thinks it is December --->
<CFSET sinceThe1stOfTheYear = CreateDateTime(#year(today)#, #month(12)#, #Day(FirstDayOfMonth(Now()))#, #hour(today)#, #minute(today)#, #second(today)#)>

But when I change the month(1) I get an output of December instead of January.
 
But when I change the month(1) I get an output of December instead of January.

Correct. The Month() function expects a date/time value. Your code is not passing in a date/time value. It is passing in "1". So CF will attempt to convert "1" to a date/time first. The result being: 1899-12-31 00:00:00. So when CF extracts the month number from that date, it returns 12 (ie December). If you want the month to be 1 (ie January), just pass in "1".

<cfset blah = CreateDateTime( year(today), 1, ....)>

The same goes for the Day() function. It expects a date, but your code is passing in a number: (ie FirstDayOfMonth(Now()).

On an unrelated note, you do not need any of those extra pound signs.



----------------------------------
 
Thanks for your input, i didn't know that piece of info!

Here is the working code:

<cfset today = CreateODBCDate(Now())>
<CFSET tektips = CreateDateTime(year(today), 1, 1, 12, 12, 12)>
<CFSET sinceThe1st = CreateDateTime(year(today), month(today), Day(tektips), hour(tektips), minute(tektips), second(tektips))>
<CFSET sinceThe1stOfTheYear = CreateDateTime(year(today), month(tektips), Day(tektips), hour(tektips), minute(tektips), second(tektips))>
 
I don't think I do, I put them in there for future use mostly if I wanted to refine my conditional statement more.

Thanks again.
 
<CFSET tektips = CreateDateTime(year(today), 1, 1, 12, 12, 12)>

Okay. Well, just be careful with the date/time values. If you forget about the time segment it can throw off your query results. I have done that more than once. Then ended up wondering why my queries did not include some of the records I thought they should ;-)

Cheers





----------------------------------
 
Good thought, if I want to do it for the beginning of the day than it should be:

<CFSET tektips = CreateDateTime(year(today), 1, 1, 0, 0, 0)>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top