Skip to main content
Inspiring
May 8, 2008
Question

Displaying dates before today's date

  • May 8, 2008
  • 7 replies
  • 560 views
Hi,
How can I display dates before the current date. The default date needs to be the current date, but rest of the dates need to be in the past. Here's my code:
Thanks!
    This topic has been closed for replies.

    7 replies

    Inspiring
    May 9, 2008
    i think what you need is just client-side and sever-side validation of
    selected date.
    Ian's code will limit the YEARS to current and prior years, but your
    users will still be able to select a day in the future if they select a
    month or date which are in the future in current year.

    use Ian's code to limit selectable years, validate the full submitted
    date to make sure it is not in the future, and display alert if the
    selected date is gt current date. make sure you take into account user's
    timezone and server's timezone if your users can come from around the
    world: your server's time may be behind your user's time...


    Azadi Saryev
    Sabai-dee.com
    http://www.sabai-dee.com/
    Inspiring
    May 8, 2008
    jenn wrote:
    > I have a drop down for each date part (e.g. month, day, year), and the default
    > is the current date. So, May, 8, 2008 will be the default. Year choice will
    > be 2008, 2007, 2006 etc. I'm trying to restrict users to only selecting the
    > current date or past dates, no future dates. Does this make sense?
    >

    Then try:

    <cfloop from="#year(Now())#" to="#(Year(Now())-5)#" index="i" step="-1">
    <option value="#i#" <cfif i EQ Year(todaysdate)>Selected</cfif>>#(i)#
    </cfloop>
    jenn1Author
    Inspiring
    May 8, 2008
    I have a drop down for each date part (e.g. month, day, year), and the default is the current date. So, May, 8, 2008 will be the default. Year choice will be 2008, 2007, 2006 etc. I'm trying to restrict users to only selecting the current date or past dates, no future dates. Does this make sense?
    Inspiring
    May 8, 2008
    quote:

    Originally posted by: jenn
    Hi,
    How can I display dates before the current date. The default date needs to be the current date, but rest of the dates need to be in the past. Here's my code:
    Thanks!

    The rest of what dates?
    Participating Frequently
    May 8, 2008
    quote:

    Originally posted by: jenn
    Hi,
    How can I display dates before the current date. The default date needs to be the current date, but rest of the dates need to be in the past. Here's my code:
    Thanks!


    Take a look at the dateadd function

    dateadd("datepart", number, "date")

    If you use a negative number it's like subtracting so for example dateadd(yyyy,-1,now()) returns a date exactly 1 year ago

    HTH
    jenn1Author
    Inspiring
    May 8, 2008
    Indeed!
    Here's my code (pasting again):
    <cfset todaysdate=DateFormat(today, "mmmm, d yyyy")>
    <select name="mymonth">
    <cfloop from="1" to="12" index="i">
    <option value="#i#" <cfif i EQ Month(todaysdate)>Selected</cfif>>#monthAsString(i)#
    </cfloop>
    </select>
    <select name="myday">
    <cfloop from="1" to="31" index="i">
    <option value="#i#" <cfif i EQ Day(todaysdate)>Selected</cfif>>#(i)#
    </cfloop>
    </select>
    <select name="myyear">
    <cfloop from="2005" to="#(Year(Now())+5)#" index="i">
    <option value="#i#" <cfif i EQ Year(todaysdate)>Selected</cfif>>#(i)#
    </cfloop>
    </select>
    Participating Frequently
    May 8, 2008
    quote:

    Here's my code
    ??? looks like an oops.....