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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Time and Date indications in 2 Digits

    Hello,
    I've obtained the following code.

    Code:
    function makeDate(){
        var d = new Date();
        var strDate=d.getFullYear() + "/" + (d.getMonth()+1) + "/" + d.getDate() + " ";
        strDate += d.getHours() + ":" + d.getMinutes() +":"+ d.getSeconds() ;
        return strDate;
    and I think I'm going to get the time and date indications in 2 digits.

    I want to get it like this:
    2009/12/07 18:07:33

    NOT like:
    2009/12/7 18:7:33

    How should I alter the code?
    Can anyone help?

    Thanks in advance.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,906
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Here you are:-

    Code:
    <script type = "text/javascript">
    
    function makeDate() {
    var d = new Date();
    var mn = d.getMonth()+1; if (mn <10) {mn = "0" + mn}
    var dt = d.getDate(); if (dt <10) {dt = "0" + dt}
    var hr = d.getHours(); if (hr <10) {hr = "0" + hr}
    var mins = d.getMinutes(); if (mins < 10) {mins = "0" + mins}
    var secs = d.getSeconds(); if (secs <10) {secs = "0" + secs}
    var strDate = d.getFullYear() + "/" + mn + "/"  + dt + " " + hr + ":" + mins + ":" + secs
    return strDate;
    }
    
    </script>

    “I don't pretend we have all the answers. But the questions are certainly worth thinking about..” - Arthur C. Clarke quotes (English Writer of science fiction, b.1917

  • #3
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It works!
    Thank you very much for your quick and helpful assistance.


  •  

    Posting Permissions

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