ColdFusion Insert Today's Date if Not Inputted

Posted on 2012-08-31
Last Modified: 2012-08-31
The code below checks if data has been entered - if yes then update records.

How can I add today's date if the date has not been entered. (And only enter date if the other two fields have data.)


<cfset isEverythingValidated = false>
         <cfif isNumeric(variables.REC_QTY)  
                    and len(variables.INVOICE)
                and isDate(variables.REC_DATE)>
                <cfset isEverythingValidated = true>

            <cfif isEverythingValidated>
                   RUN UPDATE
                  SKIP UPDATE
Question by:DJPr0
    LVL 5

    Expert Comment

    <cfif variables.REC_QTY neq "" and variables.INVOICE neq "">
       <cfif variables.REC_DATE eq "">
          <cfset variables.REC_DATE = now()>
          <!--- If you want the date formated use the commented out code --->
          <!--- <cfset variables.REC_DATE = DateFormat(now(),'mm/dd/yyyy')> ---

    Is this want you are meaning or did I misunderstand?
    LVL 51

    Accepted Solution

    <cfset isEverythingValidated = false>
    <!--- if the other two fields are populated --->
    <cfif isNumeric(variables.REC_QTY) and len(variables.INVOICE)>
            <!--- valid if a date was entered OR is empty --->
            <cfset variables.isDateEmpty = len(trim(variables.REC_DATE)) eq 0>
            <cfif isDate(variables.REC_DATE) OR variables.isDateEmpty>
                  <cfset isEverythingValidated = true>
            <!--- if it's empty, substitute date and time now --->
            <cfif variables.isDateEmpty>
                   <cfset variables.REC_DATE = now()>
    LVL 51

    Assisted Solution

    <cfset variables.REC_DATE = now()>

    One note about the code above. I assumed REC_DATE is a datetime column *not* a string:
            ie   UPDATE yourtable
                  SET      REC_DATE = <cfqueryparam value="#variables.REC_DATE#"
                  , ....

    If it's actually a string/varchar (hopefully not ..) you'll have to dateformat #now()# value first to get the "mm/dd/yyyy" part only.

    Author Closing Comment

    Thanks _agx_!!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: CSS Specialist

    We don’t have to sell you on the idea of becoming a developer. If you’re you here, you already know it’s one of the most lucrative (and fastest growing) career tracks out there. It’s CSS that allows you to set yourself apart from other web and mobile developers.

    Suggested Solutions

    CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
    Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
    In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
    The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now