js获取指定yyyy-mm格式的时间

2019-10-11  本文已影响0人  Wo信你个鬼

1.获取当月时间格式: yyyy-MM

getNowFormatDate() {//获取当月时间 yyyy-MM
            var date = new Date();
            var year = date.getFullYear();
            var month = date.getMonth() + 1;
            if (month >= 1 && month <= 9) {
                month = '0' + month;
            }
            var currentdate = year + '-' + month
            return currentdate;
}

2.获取当月时间格式: yyyy-MM-dd

getNowFormatDate() {//获取当月时间 yyyy-MM-dd
            var date = new Date();
            var year = date.getFullYear();
            var month = date.getMonth() + 1;
            var strDate = date.getDate();
            if (month >= 1 && month <= 9) {
                month = '0' + month;
            }
            if (strDate >= 1 && strDate <= 9) {
                strDate = '0' + strDate ;
            }
            var currentdate = year + '-' + month + '-' + strDate 
            return currentdate;
}

上一篇下一篇

猜你喜欢

热点阅读