displaying the date in mysql format for insertion into db

Discussion in 'Javascript' started by libsfan01, Jun 13, 2006.

  1. libsfan01

    libsfan01 Guest

    HI all!

    ive need to format the date (variable "tomorrow") in this form to mysql
    format yyyy-mm-dd can anyone please show me how to amend my script?

    regards

    Marc



    <script language="javascript">

    function showDate(thisObj){
    /* create a Date object using the system clock */
    today=new Date();
    td = today.getDate();
    tm = today.getMonth();
    ty = today.getFullYear();
    tomorrow=new Date(ty,tm,td+1)

    /* convert contents to string and place in control */
    document.getElementById(thisObj).value=tomorrow;
    }



    </script>
    <body onLoad="showDate('date');">



    <input id="date" type="text" readonly="readonly" />

    </body>
     
    libsfan01, Jun 13, 2006
    #1
    1. Advertisements

  2. libsfan01

    RobG Guest

    libsfan01 wrote:
    > HI all!
    >
    > ive need to format the date (variable "tomorrow") in this form to mysql
    > format yyyy-mm-dd can anyone please show me how to amend my script?
    >
    > regards
    >
    > Marc
    >
    >
    >
    > <script language="javascript">


    The language attribute is deprecated, type is required:

    <script type="text/javascript">


    > function showDate(thisObj){
    > /* create a Date object using the system clock */
    > today=new Date();


    Always use var to keep variables local unless you really do need globals:

    var today = new Date();

    And tomorrow is:

    var x = new Date();
    x.setDate(x.getDate() + 1);


    Now x is tomorrow's date[1]. You may need to be careful around
    midnight, getting today's date at 23:59:59 may result in tomorrow being
    today very soon. :)

    > td = today.getDate();
    > tm = today.getMonth();
    > ty = today.getFullYear();
    > tomorrow=new Date(ty,tm,td+1)
    >
    > /* convert contents to string and place in control */
    > document.getElementById(thisObj).value=tomorrow;


    function addZ(n){
    return (n<10)? '0'+n : ''+n;
    }

    ...value = x.getFullYear()
    + '-' + addZ(x.getMonth()+1)
    + '-' + addZ(x.getDate());


    1. Creating a date of say - new Date('2006/06/31') - will result in 1st
    of July, or - new Date('2006/06/32') - 2nd of July.


    --
    Rob
     
    RobG, Jun 13, 2006
    #2
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Candy
    Replies:
    2
    Views:
    1,413
    Janaka
    Feb 18, 2004
  2. DC
    Replies:
    5
    Views:
    3,211
    Brendan Reynolds
    Jul 1, 2005
  3. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,756
    Michael Borgwardt
    May 30, 2004
  4. Chu
    Replies:
    3
    Views:
    4,457
    Shawn
    Aug 15, 2006
  5. Crustov
    Replies:
    2
    Views:
    473
    Crustov
    Apr 7, 2009
  6. Josh Sharpe
    Replies:
    1
    Views:
    325
    Brian Candler
    Sep 21, 2010
  7. Replies:
    1
    Views:
    232
    Mumia W.
    Oct 21, 2007
  8. libsfan01
    Replies:
    8
    Views:
    243
    Dr John Stockton
    Oct 12, 2006
Loading...