首页 > 其他 > 详细

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

时间:2017-07-14 21:04:12      阅读:280      评论:0      收藏:0      [点我收藏+]
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)

技术分享

 

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

原文:http://www.cnblogs.com/zczhangcui/p/7172120.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!