Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 171
  • Last Modified:

Having trouble with dates

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?
0
chaduka
Asked:
chaduka
  • 6
  • 6
  • 2
1 Solution
 
nathansCommented:
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...???
0
 
chadukaAuthor Commented:
No, houw would you validate that the line <<< myDate input '02/04/2000' >>>
does read 4 February, and not 2 April?

0
 
nathansCommented:
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
0
New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

 
nathansCommented:
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.  

0
 
chadukaAuthor 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*
0
 
chadukaAuthor Commented:
Ooops, you submitted your last comment just before I mailed mine.

Ok, tx nathans. Will keep my eyes open for any easier solutions.
0
 
nathansCommented:
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.
0
 
nathansCommented:
Still looking around

Nathan Stanford
Mr ColdFusion
------------------------------------
CFTips+ a FREE ColdFusion e-ZINE
subscribe-cftips@nsnd.com
0
 
meverestCommented:
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.


0
 
chadukaAuthor 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!!
0
 
meverestCommented:
(hey, i just notice that goof i made with the above cf code snippet - oops! ;-)

anyway, you are right, but you could do the month part like:

<select name=month>
<cfloop list="Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec" index=mon>
<option value=#mon#>#mon#
</cfloop>

then your dat formats can't make that mistake ;-)

cheers.


0
 
nathansCommented:
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

0
 
chadukaAuthor 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.
0
 
chadukaAuthor 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!
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 6
  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now