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

前端 未结 16 974
暗喜
暗喜 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:51

    I am using this which works if you pass it a date obj or js timestamp:

    getHumanReadableDate: function(date) {
        if (date instanceof Date) {
             return date.getDate() + "/" + (date.getMonth() + 1) + "/" + date.getFullYear();
        } else if (isFinite(date)) {//timestamp
            var d = new Date();
            d.setTime(date);
            return this.getHumanReadableDate(d);
        }
    }
    
    0 讨论(0)
  • 2020-11-28 18:53
    var date = new Date().toLocaleDateString()
    "12/30/2009"
    
    0 讨论(0)
  • 2020-11-28 18:54
    var dt = new Date();
    
    dt.getFullYear() + "/" + (dt.getMonth() + 1) + "/" + dt.getDate();
    

    Since month index are 0 based you have to increment it by 1.

    Edit

    For a complete list of date object functions see

    Date

    getMonth()
    

    Returns the month (0-11) in the specified date according to local time.

    getUTCMonth()
    

    Returns the month (0-11) in the specified date according to universal time.

    0 讨论(0)
  • 2020-11-28 18:54

    EUROPE (ENGLISH/SPANISH) FORMAT
    I you need to get the current day too, you can use this one.

    function getFormattedDate(today) 
    {
        var week = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
        var day  = week[today.getDay()];
        var dd   = today.getDate();
        var mm   = today.getMonth()+1; //January is 0!
        var yyyy = today.getFullYear();
        var hour = today.getHours();
        var minu = today.getMinutes();
    
        if(dd<10)  { dd='0'+dd } 
        if(mm<10)  { mm='0'+mm } 
        if(minu<10){ minu='0'+minu } 
    
        return day+' - '+dd+'/'+mm+'/'+yyyy+' '+hour+':'+minu;
    }
    
    var date = new Date();
    var text = getFormattedDate(date);
    


    *For Spanish format, just translate the WEEK variable.

    var week = new Array('Domingo', 'Lunes', 'Martes', 'Miércoles', 'Jueves', 'Viernes', 'Sábado');
    


    Output: Monday - 16/11/2015 14:24

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