How to get year/month/day from a date object?

前端 未结 16 1089
暗喜
暗喜 2020-11-28 17:52

alert(dateObj) gives Wed Dec 30 2009 00:00:00 GMT+0800

How to get date in format 2009/12/30?

16条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-28 18:37

    let dateObj = new Date();
    
    let myDate = (dateObj.getUTCFullYear()) + "/" + (dateObj.getMonth() + 1)+ "/" + (dateObj.getUTCDate());
    

    For reference you can see the below details

    new Date().getDate()          // Return the day as a number (1-31)
    new Date().getDay()           // Return the weekday as a number (0-6)
    new Date().getFullYear()      // Return the four digit year (yyyy)
    new Date().getHours()         // Return the hour (0-23)
    new Date().getMilliseconds()  // Return the milliseconds (0-999)
    new Date().getMinutes()       // Return the minutes (0-59)
    new Date().getMonth()         // Return the month (0-11)
    new Date().getSeconds()       // Return the seconds (0-59)
    new Date().getTime()          // Return the time (milliseconds since January 1, 1970)
    

    let dateObj = new Date();
    
    let myDate = (dateObj.getUTCFullYear()) + "/" + (dateObj.getMonth() + 1)+ "/" + (dateObj.getUTCDate());
    
    console.log(myDate)

    // Return the minutes (0-59) new Date().getMonth() // Return the month (0-11) new Date().getSeconds() // Return the seconds (0-59) new Date().getTime() // Return the time (milliseconds since January 1, 1970)

提交回复
热议问题