We help IT Professionals succeed at work.

Having trouble with dates

chaduka
chaduka asked
on
202 Views
Last Modified: 2013-12-24
I have a form with a date input box.
<input type="text" name="myDate">
My CF code to manipulate this is:
<cfset userDate = trim(form.myDate)>
 <cfif isDate(userDate)>
  <cfset dbDate = dateFormat(userDate, "dd/mm/yy")>
 </cfif>

Now, the problem is: if I enter the date for 4 February 2000 as 04/02/2000, #dbDate# comes out as 02/04/00. ..and if I enter 02/04/2000, it comes out as 04/02/00. There is no problem if the dd value is greater than 12.

Since I suspected it to be a locale problem, of which my default is English (US), SetLocale("English (UK)") didn't solve the problem, neither did any other supported locale managed to change this.

How can I solve this problem?
Comment
Watch Question

Nathan Stanford SrSenior Programmer

Commented:
myDate input '02/04/2000'

<input type="text" name="myDate">


======================================

 <cfif isDate(myDate)>
  <cfset DisplayDate1 = dateFormat(userDate, "dd/mm/yyyy")>
 </cfif>

 <cfif isDate(myDate)>
  <cfset DisplayDate2 = dateFormat(userDate, "mm/dd/yyyy")>
 </cfif>

 <cfif isDate(myDate)>
  <cfset DisplayDate3 = dateFormat(userDate, "mmmm dd, yyyy")>
 </cfif>

<cfoutput>
#DisplayDate1#<BR>
#DisplayDate2#<BR>
#DisplayDate3#<BR>
</cfoutput>


=====================================

OUTPUT WOULD BE

04/02/2000
02/04/2000
February 04, 2000

Does this answer your problem...???

Author

Commented:
No, houw would you validate that the line <<< myDate input '02/04/2000' >>>
does read 4 February, and not 2 April?

Nathan Stanford SrSenior Programmer

Commented:
I see I see said the blind man who picked up the hammer and saw.


hmmmmm......

I guess because of the way the dateformat is...


Well you would have to do it the hard way... I think... I will check for other solutions as well but...

What you need is
1. To Request the date either in
   mm/dd/yyyy or dd/mm/yyyy

2. Once you know which way the person is going to enter the date then you can use mm/dd/yyyy like you normally would and if you choose dd/mm/yyyy you can select the first section to the left or the / and second section after the / and the third after the /.  Once you have done this
Nathan Stanford SrSenior Programmer

Commented:
Then you can use the data...

3. Another choice is to create drop downs...

Day   Month  Year

Does this help?

I will go try and find out otherwise

--------------

You have to request the date in either
mm/dd/yy OR dd/mm/yy format or you will not be able to know which one they meant... unless you checked for 12 or less and using javascript and popped up to ask a question which one they are useing.  Since the WEB is World Wide...

Sorry, alot of the web was created with US standards when we are not the majority of the people alive on the planet.  

Author

Commented:
Yeah, so I have figured out. But the funny thing is if then someone enters a date like 21/04/2000, we don't have irony.

The only solution, so it seems, is to ask the user to enter the date as dd-mmm-yyyy format, that is, type the month as text. This way, there is no confusion. dateFormat() won't switch the date and month.

*sigh*

Author

Commented:
Ooops, you submitted your last comment just before I mailed mine.

Ok, tx nathans. Will keep my eyes open for any easier solutions.
Nathan Stanford SrSenior Programmer

Commented:
I checked with Allaire and NO LUCK...

Sorry

Forgive us US folks for not creating a web that made it where you could choose both ways.
Nathan Stanford SrSenior Programmer

Commented:
Still looking around

Nathan Stanford
Mr ColdFusion
------------------------------------
CFTips+ a FREE ColdFusion e-ZINE
subscribe-cftips@nsnd.com
Top Expert 2008

Commented:
what about:

Enter Date:<select name=day>
<cfloop from=1 to=31 index=num>
<option value=#num#>#num#
</cfloop>
/
<select name=month>
<cfloop from=2000 to=2020 index=num>
<option value=#num#>#num#
</cfloop>
/
<select name=year>
<cfloop from=1 to=31 index=num>
<option value=#num#>#num#
</cfloop>


then <cfset date="#day#/#month#/#year#">

that way, no confusion..?

cheers.


Author

Commented:
Yeah, meverest, that solution seems close to total control, if I will have DateFormat() get the month as mmm, otherwise if I just give it a number < 12, CF is bound to switch my dates again!!
Top Expert 2008
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Nathan Stanford SrSenior Programmer

Commented:
You might as well give one of us the points as there is no way you can know with out coding in the Month.

Nathan

Author

Commented:
I am still looking for a best solution. From the allaire forums, I have heard that I can use:

<cfinput .... validate="eurodate" ...>

I haven't tried it as yet. I would want to exhaust more options to conquering this thing, rather than rush to award points, which is a secindary objective for this place. Sharing knowledge is the primary thing, no? I stand to be corrected.

Author

Commented:
I decided to give meverest the points because he came with the idea of splitting the date form to make the user select the day/month. (I wish these points could be split).

Thanx nathans, your help was great too!
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.