How to get current date in jquery?

前端 未结 30 1745
春和景丽
春和景丽 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 09:58

    I know I am Late But This Is All You Need

    var date = (new Date()).toISOString().split('T')[0];
    

    toISOString() use built function of javascript.

    cd = (new Date()).toISOString().split('T')[0];
    console.log(cd);
    alert(cd);

    0 讨论(0)
  • 2020-11-27 09:58

    FYI - getDay() will give you the day of the week... ie: if today is Thursday, it will return the number 4 (being the 4th day of the week).

    To get a proper day of the month, use getDate().

    My example below... (also a string padding function to give a leading 0 on single time elements. (eg: 10:4:34 => 10:04:35)

    function strpad00(s)
    {
        s = s + '';
        if (s.length === 1) s = '0'+s;
        return s;
    }
    
    var currentdate = new Date();
    var datetime = currentdate.getDate() 
        + "/" + strpad00((currentdate.getMonth()+1)) 
        + "/" + currentdate.getFullYear() 
        + " @ " 
        + currentdate.getHours() + ":" 
        + strpad00(currentdate.getMinutes()) + ":" 
        + strpad00(currentdate.getSeconds());
    

    Example output: 31/12/2013 @ 10:07:49
    If using getDay(), the output would be 4/12/2013 @ 10:07:49

    0 讨论(0)
  • 2020-11-27 09:59

    You can add an extension method to javascript.

    Date.prototype.today = function () {
        return ((this.getDate() < 10) ? "0" : "") + this.getDate() + "/" + (((this.getMonth() + 1) < 10) ? "0" : "") + (this.getMonth() + 1) + "/" + this.getFullYear();
    }
    
    0 讨论(0)
  • 2020-11-27 10:02

    you can use this code:

    var nowDate     = new Date();
    var nowDay      = ((nowDate.getDate().toString().length) == 1) ? '0'+(nowDate.getDate()) : (nowDate.getDate());
    var nowMonth    = ((nowDate.getMonth().toString().length) == 1) ? '0'+(nowDate.getMonth()+1) : (nowDate.getMonth()+1);
    var nowYear     = nowDate.getFullYear();
    var formatDate  = nowDay + "." + nowMonth + "." + nowYear;
    

    you can find a working demo here

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

    This is what I came up with using only jQuery. It's just a matter of putting the pieces together.

            //Gather date information from local system
            var ThisMonth = new Date().getMonth() + 1;
            var ThisDay = new Date().getDate();
            var ThisYear = new Date().getFullYear();
            var ThisDate = ThisMonth.toString() + "/" + ThisDay.toString() + "/" + ThisYear.toString();
    
            //Gather time information from local system
            var ThisHour = new Date().getHours();
            var ThisMinute = new Date().getMinutes();
            var ThisTime = ThisHour.toString() + ":" + ThisMinute.toString();
    
            //Concatenate date and time for date-time stamp
            var ThisDateTime = ThisDate  + " " + ThisTime;
    
    0 讨论(0)
  • 2020-11-27 10:03

    This will give you current date string

    var today = new Date().toISOString().split('T')[0];
    
    0 讨论(0)
提交回复
热议问题