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
    Regular Coder
    Join Date
    Oct 2011
    Posts
    116
    Thanks
    19
    Thanked 1 Time in 1 Post

    Current Date Time

    Hello Experts

    I have following codes

    Code:
    <html>
    <head>
    <script language="Javascript">
    function showdate()
    {
    var currentTime = new Date() 
    var month = currentTime.getMonth() +1
    
    var day = currentTime.getDate() 
    var year = currentTime.getFullYear() 
    var hours = currentTime.getHours() 
    var minutes = currentTime.getMinutes() 
    var seconds = currentTime.getSeconds() 
     
    alert(month + "/" + day + "/" + year  +"\n" + hours+ ":"+ minutes+ ":"+ seconds) 
    }
    </script>
    </head>
    <body onload="showdate()">
    </body>
    </html>
    the output is as

    03/06/2012
    14:41:46


    But I want to get following result

    Tuesday March 06, 2012 02:41:46 PM

    Please modify my codes or suggest some others.

    Thanks in advance

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,391
    Thanks
    11
    Thanked 568 Times in 561 Posts
    This one worked for me (but it HIGHLY depends on your system's locale settings!)
    Code:
    alert((new Date()).toLocaleString());
    No other extraction of date/time parts necessary!

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,592
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Another alternative is to use an extended version of the Date object that implements the equivalent of the PHP format command - such as the one at http://javascriptexample.net/dollarD.php
    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.


  •  

    Posting Permissions

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