How to get current time with jQuery

后端 未结 15 802
青春惊慌失措
青春惊慌失措 2020-12-07 08:10

The following returns time in microseconds, for example 4565212462.

alert( $.now() );

How do I convert it to a human readable time format,

相关标签:
15条回答
  • 2020-12-07 08:56
    <html>
    <head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
    </script>
    <script>
        function ShowLocalDate()
        {
        var dNow = new Date();
        var localdate= (dNow.getMonth()+1) + '/' + dNow.getDate() + '/' + dNow.getFullYear() + ' ' + dNow.getHours() + ':' + dNow.getMinutes();
        $('#currentDate').text(localdate)
        }
    </script>
    
    </head>
    <body>
        enter code here
        <h1>Get current local enter code here Date in JQuery</h1>
        <label id="currentDate">This is current local Date Time in JQuery</p>
        <button type="`enter code here button onclick="ShowLocalDate()">Show Local DateTime</button>
    
    </body>
    </html> 
    

    you can get more information from below link

    http://www.morgantechspace.com/2013/11/Get-current-Date-time-in-JQuery.html#GetLocalDateTimeinJQuery

    0 讨论(0)
  • 2020-12-07 08:59

    You need to fetch all "numbers" manually

    like this:

    var currentdate = new Date(); 
        var datetime = "Now: " + currentdate.getDate() + "/"
                    + (currentdate.getMonth()+1)  + "/" 
                    + currentdate.getFullYear() + " @ "  
                    + currentdate.getHours() + ":"  
                    + currentdate.getMinutes() + ":" 
                    + currentdate.getSeconds();
    
    document.write(datetime);

    0 讨论(0)
  • 2020-12-07 09:00

    You don't need to use jQuery for this!

    The native JavaScript implementation is Date.now().

    Date.now() and $.now() return the same value:

    Date.now(); // 1421715573651
    $.now();    // 1421715573651
    
    new Date(Date.now())   // Mon Jan 19 2015 20:02:55 GMT-0500 (Eastern Standard Time)
    new Date($.now());     // Mon Jan 19 2015 20:02:55 GMT-0500 (Eastern Standard Time)
    

    ..and if you want the time formatted in hh-mm-ss:

    var now = new Date(Date.now());
    var formatted = now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds();
    // 20:10:58
    
    0 讨论(0)
  • 2020-12-07 09:02

    I use moment for all my time manipulation/display needs (both client side, and node.js if you use it), if you just need a simple format the answers above will do, if you are looking for something a bit more complex, moment is the way to go IMO.

    0 讨论(0)
  • 2020-12-07 09:03

    <p id="date"></p>
    
    <script>
    var d = new Date();
    document.getElementById("date").innerHTML = d.toTimeString();
    </script>

    You can use Date() in JS.

    0 讨论(0)
  • 2020-12-07 09:04

    .clock {
    width: 260px;
    margin: 0 auto;
    padding: 30px;
    color: #FFF;background:#333;
    }
    .clock ul {
    width: 250px;
    margin: 0 auto;
    padding: 0;
    list-style: none;
    text-align: center
    }
    
    .clock ul li {
    display: inline;
    font-size: 3em;
    text-align: center;
    font-family: "Arial", Helvetica, sans-serif;
    text-shadow: 0 2px 5px #55c6ff, 0 3px 6px #55c6ff, 0 4px 7px #55c6ff
    }
    #Date { 
    font-family: 'Arial', Helvetica, sans-serif;
    font-size: 26px;
    text-align: center;
    text-shadow: 0 2px 5px #55c6ff, 0 3px 6px #55c6ff;
    padding-bottom: 40px;
    }
    
    #point {
    position: relative;
    -moz-animation: mymove 1s ease infinite;
    -webkit-animation: mymove 1s ease infinite;
    padding-left: 10px;
    padding-right: 10px
    }
    
    /* Animasi Detik Kedap - Kedip */
    @-webkit-keyframes mymove 
    {
    0% {opacity:1.0; text-shadow:0 0 20px #00c6ff;}
    50% {opacity:0; text-shadow:none; }
    100% {opacity:1.0; text-shadow:0 0 20px #00c6ff; } 
    }
    
    @-moz-keyframes mymove 
    {
    0% {opacity:1.0; text-shadow:0 0 20px #00c6ff;}
    50% {opacity:0; text-shadow:none; }
    100% {opacity:1.0; text-shadow:0 0 20px #00c6ff; } 
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function() {
    // Making 2 variable month and day
    var monthNames = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]; 
    var dayNames= ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
    
    // make single object
    var newDate = new Date();
    // make current time
    newDate.setDate(newDate.getDate());
    // setting date and time
    $('#Date').html(dayNames[newDate.getDay()] + " " + newDate.getDate() + ' ' + monthNames[newDate.getMonth()] + ' ' + newDate.getFullYear());
    
    setInterval( function() {
    // Create a newDate() object and extract the seconds of the current time on the visitor's
    var seconds = new Date().getSeconds();
    // Add a leading zero to seconds value
    $("#sec").html(( seconds < 10 ? "0" : "" ) + seconds);
    },1000);
    
    setInterval( function() {
    // Create a newDate() object and extract the minutes of the current time on the visitor's
    var minutes = new Date().getMinutes();
    // Add a leading zero to the minutes value
    $("#min").html(( minutes < 10 ? "0" : "" ) + minutes);
    },1000);
    
    setInterval( function() {
    // Create a newDate() object and extract the hours of the current time on the visitor's
    var hours = new Date().getHours();
    // Add a leading zero to the hours value
    $("#hours").html(( hours < 10 ? "0" : "" ) + hours);
    }, 1000); 
    });
    </script>
    <div class="clock">
    <div id="Date"></div>
    <ul>
    <li id="hours"></li>
    <li id="point">:</li>
    <li id="min"></li>
    <li id="point">:</li>
    <li id="sec"></li>
    </ul>
    </div>

    0 讨论(0)
提交回复
热议问题