How to get current date in jquery?

前端 未结 30 1748
春和景丽
春和景丽 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:14

    Get current Date format dd/mm/yyyy

    Here is the code:

    var fullDate = new Date();
    var twoDigitMonth = ((fullDate.getMonth().toString().length) == 1)? '0'+(fullDate.getMonth()+1) : (fullDate.getMonth()+1);
    var twoDigitDate = ((fullDate.getDate().toString().length) == 1)? '0'+(fullDate.getDate()) : (fullDate.getDate());
    var currentDate = twoDigitDate + "/" + twoDigitMonth + "/" + fullDate.getFullYear();
    alert(currentDate);
    
    0 讨论(0)
  • 2020-11-27 10:16
    function GetTodayDate() {
       var tdate = new Date();
       var dd = tdate.getDate(); //yields day
       var MM = tdate.getMonth(); //yields month
       var yyyy = tdate.getFullYear(); //yields year
       var currentDate= dd + "-" +( MM+1) + "-" + yyyy;
    
       return currentDate;
    }
    

    Very handy function to use it, Enjoy. You do not require any javascript framework. it just works in with plain javascript.

    0 讨论(0)
  • 2020-11-27 10:17
    function returnCurrentDate() {
                    var twoDigitMonth = ((fullDate.getMonth().toString().length) == 1) ? '0' + (fullDate.getMonth() + 1) : (fullDate.getMonth() + 1);
                    var twoDigitDate = ((fullDate.getDate().toString().length) == 1) ? '0' + (fullDate.getDate()) : (fullDate.getDate());
                    var currentDate = twoDigitDate + "/" + twoDigitMonth + "/" + fullDate.getFullYear();
                    return currentDate;
                }
    
    0 讨论(0)
  • 2020-11-27 10:19

    In JavaScript you can get the current date and time using the Date object;

    var now = new Date();
    

    This will get the local client machine time

    Example for jquery LINK

    If you are using jQuery DatePicker you can apply it on any textfield like this:

    $( "#datepicker" ).datepicker({dateFormat:"yy/mm/dd"}).datepicker("setDate",new Date());
    
    0 讨论(0)
  • 2020-11-27 10:19

    You can achieve this with moment.js as well. Include moment.js in your html.

    <script src="moment.js"></script>
    

    And use below code in script file to get formatted date.

    moment(new Date(),"YYYY-MM-DD").utcOffset(0, true).format();
    
    0 讨论(0)
  • 2020-11-27 10:20

    Here is method top get current Day, Year or Month

    new Date().getDate()          // Get the day as a number (1-31)
    new Date().getDay()           // Get the weekday as a number (0-6)
    new Date().getFullYear()      // Get the four digit year (yyyy)
    new Date().getHours()         // Get the hour (0-23)
    new Date().getMilliseconds()  // Get the milliseconds (0-999)
    new Date().getMinutes()       // Get the minutes (0-59)
    new Date().getMonth()         // Get the month (0-11)
    new Date().getSeconds()       // Get the seconds (0-59)
    new Date().getTime()          // Get the time (milliseconds since January 1, 1970)
    
    0 讨论(0)
提交回复
热议问题