How to get current date in jquery?

前端 未结 30 1910
春和景丽
春和景丽 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条回答
  •  旧时难觅i
    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

提交回复
热议问题