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

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)

 

posted @ 2017-07-14 20:17  ZhangCui  阅读(1311)  评论(0编辑  收藏  举报