Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: help with date

  1. #1
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    help with date

    Hi, trying to set up date (yyyy-mm-dd). The following code produces "2014-3-13" - should be "2014-04-13".
    Any help?
    Code:
    var dayNow = dateNow.getDate();
      var datePaid = (dateNow.getFullYear()+"-"+dateNow.getMonth()+"-"+dateNow.getDate());  
       datepaid.value = datePaid;

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,640
    Thanks
    0
    Thanked 649 Times in 639 Posts
    You need to add one th the value returned from getMonth() and then add a leading '0' if it is 9 or less. You will also need to add a leading '0' when getDate returns 9 or less.

    Code:
    var mm = dateNow.getMonth() + 1;
    mm = (mm < 10) ? '0' + mm : mm;
    var dd = dateNow();
    dd = (dd< 10) ? '0' + dd : dd;
    var datePaid = (dateNow.getFullYear()+"-"+mm+"-"+dd);
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi, The following code is my feeble attempt at javascript to be used in my
    HTML doc to autoinsert the date into the input. I thank u for your help.
    I'm told that that since my datepaid is "Date" I probably should use Mysql.
    function $_(IDS) { return document.getElementById(IDS); }
    function calculate_paid()
    {
    var pd = document.getElementById("pd");
    var shipamt = document.getElementById("shipamt");
    var datepaid = document.getElementById("datepaid");
    var paidamt = document.getElementById("paidamt");
    var checkno = document.getElementById("checkno");
    var prevbal= document.getElementById("prevbal");
    var amtdue = document.getElementById("amtdue");
    <!--date setup-->

    var dateNow = new Date();
    var dayNow = dateNow.getDate();
    var datePaid = (dateNow.getFullYear()+"-"+dateNow.getMonth()+"-"+dateNow.getDate());
    datepaid.value = datePaid;

    <--var datepaid = curdate();
    datepaid.value = datePaid;-->
    <!--end date setup-->

    amtdue.value = parsefloat(amtdue.value);
    paidamt.value = parsefloat(paidamt.value);
    checkno.value = parseint(checkno.value);
    amtdue.value = parsefloat(amtdue.value) + prevbal(parsefloat) + parsefloat(shipamt.value) - parsefloat(paidamt.value);;

    var sum = amtdue.value - paidamt.value;
    if (sum==0)
    { pd.value = "P"; prevbal.value = 0; shipamt.value = 0;}
    prevbal.value = sum;pd.value = "N";
    checkno.value = checkno.value + 1;
    following is the pertinent HTML code:
    [QUOTE][QUOTE]
    <td><input type='text' readonly size=15 name='acctno[]' value='" . $row['acctno'] . "' ></td>
    <td><input type='text' readonly size=25 name='bname[]' value='" . $row['bname'] . "'></td>
    <td><input type='text' readonly size=25 name='purpose[]' value='" . $row['purpose'] . "'></td>

    <td><input type='text' size=7 id='paidamt' name='paidamt[]' value='" . $row['paidamt'] ."'
    onBlur='calculate_paid(this)'></td>

    <td><input type='text' size=5 id='checkno'name='checkno[]' value='" . $row['checkno'] . "' ></td>
    <td><input type='text' size=7 id='amtdue' name='amtdue[]' value='" . $row['amtdue'] . "'></td>
    <td><input type='text' size=10 id='datepaid' name='datepaid[]' value='" . $row['datepaid'] . "'></td>
    <td><input type='text' size=1 id='pd' name='pd[]' value='" . $row['pd'] . "' ></td>[/QUOTEhelp with date-sendpay.jpg
    I have a screenshot to show my intent:

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    amtdue.value = parsefloat(amtdue.value);
    Javascript is case sensitive.

    Should be simply amtdue = parseFloat(amtdue.value);
    It is a bad idea to assign the same names/ids to HTML elements and Javascript variables. That just (as here) causes confusion.

    Prefer to do something like:-
    var amtDUE = parseFloat(document.getElementById("amtdue").value;

    prevbal(parsefloat) should be parseFloat(prevbal)

    function $_(IDS) { return document.getElementById(IDS); } // This little function shortens getElementById("id") to $_("id") but you do not use it.


    felgall has already shown you how to format the date as you wish it.
    Last edited by Philip M; 04-16-2014 at 09:02 AM. Reason: typo

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for the help. Is this what you suggested? I don't understand
    "function $_(IDS) { return document.getElementById(IDS); } // This little function shortens getElementById("id") to $_("id") but you do not use it."
    function $_(IDS) { return document.getElementById(IDS); }
    function calculate_paid()
    {
    var pd = parseFloat(document.getElementById("pd").value;
    var shipamt = parseFloat(document.getElementById("shipamt").value;
    var datepaid = parseFloat(document.getElementById("datepaid").value;
    var paidamt = parseFloat(document.getElementById("paidamt").value;
    var checkno = parseFloat(document.getElementById("checkno").value;
    var prevbal = parseFloat(document.getElementById("prevbal").value;
    var amtdue = parseFloat(document.getElementById("amtdue").value;

    var mm = dateNow.getMonth() + 1;
    mm = (mm < 10) ? '0' + mm : mm;
    var dd = dateNow();
    dd = (dd< 10) ? '0' + dd : dd;
    var datePaid = (dateNow.getFullYear()+"-"+mm+"-"+dd);

    amtdue = parseFloat(amtdue.value);
    paidamt = parseFloat(amtdue.value);
    checkno = parseFloat(amtdue.value);
    pd = parseFloat(pd.value);

    amtdue = amtdue + prevbal + shipamt - paidamt;

    var sum = amtdue - paidamt;
    if (sum==0)
    { pd = "P"; prevbal = 0; shipamt = 0;}
    prevbal = sum; pd = "N";
    checkno = checkno + 1;
    }

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    You define a function
    Code:
    function $_(IDS) { return document.getElementById(IDS); }
    but you do not call it anywhere.

    Code:
    amtdue = parseFloat(amtdue.value);
     paidamt = parseFloat(amtdue.value);
     checkno = parseFloat(amtdue.value);
     pd = parseFloat(pd.value);
    This block of code is redundant.


    One more time: It is a bad idea to assign the same names/ids to HTML elements and Javascript variables. That just (as here) causes confusion.
    Last edited by Philip M; 04-16-2014 at 05:25 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    12Strings (04-16-2014)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •