simple html question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • VJ
    Moderator
    • Mar 2001
    • 9733

    #1

    simple html question

    Hello,

    A quick question: how can I put the current date in the format "dd/mm/yyyy" in an input field?

    Jörg
    pixar
    Dream as if you'll live forever. Live as if you'll die tomorrow. (James Dean)
  • Jessterw
    Style Master, Rtd.
    • Jul 2002
    • 4265

    #2
    Essentially you're limited to Javascript or a server-side/page pre-processing language such as ASP, JSP, PHP, etc.

    An example for Javascript might be:

    Code:
    <script language="javascript">
    function setDate() {
      var d = new Date();
      var currentDate = d.getDate();
      var currentMonth = d.getMonth();
      currentMonth ++;
      var currentYear = d.getFullYear();
    
      document.formName.fieldName.value = (currentDate + "/" + currentMonth 
          + "/" + currentYear);
    }
    </script>
    First notice that the month variable is incremented after being obtained, this is because the value returned is 0-based indexed (i.e., January = 0, December = 11).

    You would also have to add in some value manipulation for the date (day) and month to get them in "dd" and "mm" form, respectively.
    “And, remember: there's no 'I' in 'irony'” ~ Merlin Mann

    Comment

    • VJ
      Moderator
      • Mar 2001
      • 9733

      #3
      Thanks!
      When do I best call this function?

      It is not necessary to prepend unary digits with 0, so that will suffice.

      Jorg
      pixar
      Dream as if you'll live forever. Live as if you'll die tomorrow. (James Dean)

      Comment

      • Jessterw
        Style Master, Rtd.
        • Jul 2002
        • 4265

        #4
        Well if you want it to be called after the page has finished loading, then you can do:
        Code:
        <body onload="setDate()">
        “And, remember: there's no 'I' in 'irony'” ~ Merlin Mann

        Comment

        • VJ
          Moderator
          • Mar 2001
          • 9733

          #5
          Thanks!
          (I also figured out how to do this in Typo3, as the body-tag is not directly available in this CMS)


          Jörg
          pixar
          Dream as if you'll live forever. Live as if you'll die tomorrow. (James Dean)

          Comment

          Working...