Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

CFML dates - not getting what I excpected

Posted on 2013-01-31
11
Medium Priority
?
209 Views
Last Modified: 2013-02-03
The following code yields;

tmdate1=*20130131.txt*

tmdate2=*20130131*
tmdate=*57014/06/04*
d:\inetpub\wwwroot\tat.net\development\control\phonedata\CallData_20130131.txt
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>

Open in new window

0
Comment
Question by:lantervj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
11 Comments
 
LVL 36

Accepted Solution

by:
SidFishes earned 2000 total points
ID: 38841471
You can't pass 20130131 to date format as it interprets it as a numerical date value (I think)


try this (with a regex shortcut thrown in as a bonus :)

<cfset tmdate=rereplacenocase(attributes.filename, "[^\d]", "", "all")>
<cfset tmdate= "#left(tmdate, 4)#,#mid(tmdate,5,2)# , #right(tmdate,2)#">
#dateformat(tmdate, "yyyy/mm/dd")#

This works as long as the dates are always 8 chars long (ie: not 2013131)
0
 

Author Comment

by:lantervj
ID: 38841577
Works great.  I'd like to know why, but can't stop and play now.
0
 

Author Closing Comment

by:lantervj
ID: 38841579
Great
0
Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

 
LVL 36

Expert Comment

by:SidFishes
ID: 38841638
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.
0
 

Author Comment

by:lantervj
ID: 38841662
So, your advice to me is;  Stop doing that?
0
 
LVL 52

Expert Comment

by:_agx_
ID: 38841890
> 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.
0
 
LVL 52

Expert Comment

by:_agx_
ID: 38841942
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.
0
 

Author Comment

by:lantervj
ID: 38842323
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.
0
 
LVL 52

Expert Comment

by:_agx_
ID: 38847874
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 :)
0
 

Author Comment

by:lantervj
ID: 38847995
Me too.  I didn't really mean anything by it.  I'm an old fart and thought I was through before y2k.  :)
0
 
LVL 52

Expert Comment

by:_agx_
ID: 38848975
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 ;-)
0

Featured Post

Python: Series & Data Frames With Pandas

Learn the basics of Python’s pandas library of series & data frames and how we can use these tools for data manipulation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them (http://www.randhawaworld.com/) . I did keep the main js functions but made sever…
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question