How to get current date in jquery?

前端 未结 30 1749
春和景丽
春和景丽 2020-11-27 09:51

I want to know how to use the Date() function in jQuery to get the current date in a yyyy/mm/dd format.

相关标签:
30条回答
  • 2020-11-27 10:21

    See this.
    The $.now() method is a shorthand for the number returned by the expression (new Date).getTime().

    0 讨论(0)
  • 2020-11-27 10:21

    Moment.js makes it quite easy:

    moment().format("YYYY/MM/DD")
    
    0 讨论(0)
  • 2020-11-27 10:21
    var d = new Date();
    
    var today = d.getFullYear() + '/' + ('0'+(d.getMonth()+1)).slice(-2) + '/' + ('0'+d.getDate()).slice(-2);
    
    0 讨论(0)
  • 2020-11-27 10:22

    this object set zero, when element has only one symbol:

    function addZero(i) {
        if (i < 10) {
            i = "0" + i;
        }
        return i;
    }
    

    This object set actual full time, hour and date:

    function getActualFullDate() {
        var d = new Date();
        var day = addZero(d.getDate());
        var month = addZero(d.getMonth()+1);
        var year = addZero(d.getFullYear());
        var h = addZero(d.getHours());
        var m = addZero(d.getMinutes());
        var s = addZero(d.getSeconds());
        return day + ". " + month + ". " + year + " (" + h + ":" + m + ")";
    }
    
    function getActualHour() {
        var d = new Date();
        var h = addZero(d.getHours());
        var m = addZero(d.getMinutes());
        var s = addZero(d.getSeconds());
        return h + ":" + m + ":" + s;
    }
    
    function getActualDate() {
        var d = new Date();
        var day = addZero(d.getDate());
        var month = addZero(d.getMonth()+1);
        var year = addZero(d.getFullYear());
        return day + ". " + month + ". " + year;
    }
    

    HTML:

    <span id='full'>a</span>
    <br>
    <span id='hour'>b</span>
    <br>    
    <span id='date'>c</span>
    

    JQUERY VIEW:

    $(document).ready(function(){
        $("#full").html(getActualFullDate());
        $("#hour").html(getActualHour());
        $("#date").html(getActualDate());
    });
    

    EXAMPLE

    0 讨论(0)
  • 2020-11-27 10:23

    jQuery is JavaScript. Use the Javascript Date Object.

    var d = new Date();
    var strDate = d.getFullYear() + "/" + (d.getMonth()+1) + "/" + d.getDate();
    
    0 讨论(0)
  • 2020-11-27 10:24

    Since the question is tagged as JQuery:

    If you are also using JQuery UI you can use $.datepicker.formatDate():

    $.datepicker.formatDate('yy/mm/dd', new Date());
    

    See this demo.

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