js日期处理总结

//string转为Date
var date = new Date("2017-12-01");

//Date转string
var year = date.getFullYear(); //
var month = date.getMonth() + 1; //
var day = date.getDate(); //
var weekday = date.getDay(); //周几
var hour = date.getHours(); //时,默认为8
var minute = date.getMinutes(); //分,默认为0
var second = date.getSeconds(); //秒, 默认为0

//函数
function getDateString(date) {
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();

    if (month < 10) {
        month = "0" + month;
    }
    if (day < 10) {
        day = "0" + day;
    }
    return year + "-" + month + "-" + day;
}

//日期计算
var days = 44;
date.setDate(date.getDate() + days);

 

posted @ 2017-12-14 10:13  zxs2016  阅读(105)  评论(0编辑  收藏  举报