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 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Apr 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    populating new Date() with db query

    I need to pull a date from a database that is already in the correct format of YYYYMMDDHHMMSS and create a javascript new Date() so that it can be used for comparing time elapsed. I have no issue with getting the date from the db and formatting it correctly but when inserted into new Date() it doesn't work.

    I have it working hard coded in the javascript but would like to replace the date with a value from a database instead of having it hard coded.

    current javascript is:
    var today=new Date();
    var then=(new Date(2012,03,09,12,00,00,00);
    var mSeconds=today.getTime()-then.getTime();

    what I would like to do:
    var today=new Date();
    var then=(new Date(<?php echo $tou; ?>);
    var mSeconds=today.getTime()-then.getTime();

    $tou is already formatted as 2012,03,09,12,00,00,00 with commas
    all other variables being pulled from the db work correctly.

  • #2
    New to the CF scene
    Join Date
    Apr 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I ended up splitting the string to make it work.

    var x = '<?php echo $tou; ?>'.split(/[,]/);
    var then = new Date(x[0], x[1]-1, x[2], x[3], x[4], x[5]);

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,596
    Thanks
    80
    Thanked 4,630 Times in 4,593 Posts
    Should have worked as is.

    Did you ever look at the code IN THE BROWSER using your first method??
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    The database should be returning the date in a format the JavaScript can read directly without needing to split it up. JavaScript will accept a string as the only parameter when creating a date and can parse it to extract the date from it. The only rerason it might not work is if you stripped the hypens and colons out of what the database should be returning by default for a date field 'ccyy-mm-dd hh:mm:ss'
    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.

  • #5
    New to the CF scene
    Join Date
    Apr 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The date comes out comma delimited and not in typical mysql format.
    I did some document.write statements prior to splitting the string to try and see what was going on and the following happened.

    the date is passed from PHP to Javascipt in the correct format, that part works.
    document.write(tou); - prints '2012,03,09,12,00,00,00' which is correct
    document.write(new Date(tou)); - prints 'Invalid Date'

    here's a test to run which delivers the same result:
    <script type="text/javascript">
    var foo ='2012,03,09,12,00,00,00';
    document.write(foo);
    var then=(new Date(foo));
    document.write(then);
    </script>
    Last edited by mozilla; 05-01-2012 at 10:37 AM. Reason: added content

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,596
    Thanks
    80
    Thanked 4,630 Times in 4,593 Posts
    No no no...that last code will never work.

    Because *NOW* the stuff coming in from PHP *will* be a string. And JS won't understand a *STRING* with those contents as the argument to the Date constructor.

    What SHOULD have worked is your original code:
    Code:
    var then=(new Date(<?php echo $tou; ?>);
    *EXCEPT* I just noticed that you have two ( and only one ) !!!

    Your first ( is extraneous. Zap it!

    Try
    Code:
    var then = new Date(<?php echo $tou; ?>);
    I'll bet it works!
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    New to the CF scene
    Join Date
    Apr 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I originally had this and it doesn't work.
    Code:
    var then = new Date(<?php echo $tou; ?>);
    I am fine with splitting the string I just thought the above code should have worked.

    I noticed my typo's with the extra "(", the original code doesn't have that.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,596
    Thanks
    80
    Thanked 4,630 Times in 4,593 Posts
    IF you want to pursue this (and I certainly understand if you don't), do this:

    Use the original code
    Code:
    var then = new Date(<?php echo $tou; ?>);
    Bring the page in question up in your browser.

    Click on the VIEW menu of your browser.

    Click on the SOURCE or PAGE SOURCE menu item.

    Look at the HTML *as the browser sees it* that will now be shown to you.

    Find that line in the HTML.

    Copy/paste that line here.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    LinkBacks (?)


    Posting Permissions

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