1. 程式人生 > >生成“年-月-日”形式的日期字符串

生成“年-月-日”形式的日期字符串

obj log .get get bsp nth img new alt

function showDate(time){
        var date_obj = new Date(time),
            year,
            month,
            date;

        year = date_obj.getFullYear();
        month = String(date_obj.getMonth() + 1).length === 2 ? (date_obj.getMonth() + 1) : "0" + (date_obj.getMonth() + 1);
        date = String(date_obj.getDate()).length === 2 ? (date_obj.getDate()) : "0" + date_obj.getDate();
        
return (year+"/"+month+"/"+date); }

1、今日:

showDate(Date.now())

技術分享

2、本月1號:

showDate(Date.now()).replace(/\d{2}$/,"01")

技術分享

3、一周前:

showDate(Date.now() - 7*24*3600*1000)

技術分享

生成“年-月-日”形式的日期字符串