lantervj
asked on
CFML dates - not getting what I excpected
The following code yields;
tmdate1=*20130131.txt*
tmdate2=*20130131*
tmdate=*57014/06/04*
d:\inetpub\wwwroot\tat.net \developme nt\control \phonedata \CallData_ 20130131.t xt
57014/06/04
The asterisks are just for marking the output and looking for spaces.
tmdate1=*20130131.txt*
tmdate2=*20130131*
tmdate=*57014/06/04*
d:\inetpub\wwwroot\tat.net
57014/06/04
The asterisks are just for marking the output and looking for spaces.
<cfset tmdate1="#trim(right(attributes.filename,12))#">
<br>
<cfoutput>
tmdate1=*#tmdate1#*
<br><br>
<cfset tmdate2="#trim(left(tmdate1,8))#">
tmdate2=*#tmdate2#*
<br>
<cfset tmdate = "#dateformat(trim(tmdate2),'yyyy/mm/dd')#">
tmdate=*#tmdate#*
</cfoutput>
<br>
<cfoutput>#attributes.filename#</cfoutput><br>
<cfoutput>#dateformat(left(right(attributes.filename,12),8),"yyyy/mm/dd")#</cfoutput>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Great
why is pretty simple
cf doesn't recognize it as a date
#isdate(tmdate)# returns NO
And often when cf doesn't understand format related things, it guesses....
and you get weird results.
cf doesn't recognize it as a date
#isdate(tmdate)# returns NO
And often when cf doesn't understand format related things, it guesses....
and you get weird results.
ASKER
So, your advice to me is; Stop doing that?
> I'd like to know why, but can't stop and play now.
When you pass in something that looks like a number, ie "20130131" DateFormat treats it as the numeric representation of a date (similar to Excel). That's different than interpreting the string as yyyymmdd. With numeric dates:
- 0 is the date 12/30/1899
- 3 is the date 01/02/1900 (ie 12/30/1899 + 3 days)
- ...
- 20130131 is the date 06/04/57014 (ie 12/30/1899 + 20130131 days)
His code converts your string into something DateFormat recognizes as a date string.
When you pass in something that looks like a number, ie "20130131" DateFormat treats it as the numeric representation of a date (similar to Excel). That's different than interpreting the string as yyyymmdd. With numeric dates:
- 0 is the date 12/30/1899
- 3 is the date 01/02/1900 (ie 12/30/1899 + 3 days)
- ...
- 20130131 is the date 06/04/57014 (ie 12/30/1899 + 20130131 days)
His code converts your string into something DateFormat recognizes as a date string.
Oops... didn't see the other replies before posting.
Like Sid said, CF does a lot of guessing - which is both good and bad. It makes for simpler code, but if you don't understand *how* it guesses, you're in for a lot of headaches and unexpected results. Especially with dates.
(Edit) Unfortunately part of the flexibility is that CF doesn't ask how it should parse date strings. It just examines the input and takes its best guess. The standard date functions can make sense of most U.S. date strings, but the safest bet is to stick with the non-ambiguous: yyyy-mm-dd format.
Like Sid said, CF does a lot of guessing - which is both good and bad. It makes for simpler code, but if you don't understand *how* it guesses, you're in for a lot of headaches and unexpected results. Especially with dates.
(Edit) Unfortunately part of the flexibility is that CF doesn't ask how it should parse date strings. It just examines the input and takes its best guess. The standard date functions can make sense of most U.S. date strings, but the safest bet is to stick with the non-ambiguous: yyyy-mm-dd format.
ASKER
My life has been a living hell since 12/31/1999. But, I get it. I really do. I just FORget it sometimes. I rely on you guys to remind me.
No worries, happens to me too. We don't always notice the names of who's asking or remember their complete background. So sometimes we may repeat stuff you already know. But I figure it's better to answer the question asked, than not, and let the asker ignore bits they already know. Nothing worse than taking the time to ask a question and having someone ignore it. (That's my personal pet peeve :)
ASKER
Me too. I didn't really mean anything by it. I'm an old fart and thought I was through before y2k. :)
Haha, if only. There's still the fun of daylight savings time changes and conflicting character encoding issues to play with. Just in case we weren't having enough fun ;-)
ASKER