js日期格式处理

//获取当前时间
function getCurrentDate()
{
    var timeStr = '-';
    var curDate = new Date();
    var curYear =curDate.getFullYear();  //获取完整的年份(4位,1970-????)
    var curMonth = curDate.getMonth()+1;  //获取当前月份(0-11,0代表1月)
    var curDay = curDate.getDate();       //获取当前日(1-31)
    var curWeekDay = curDate.getDay();    //获取当前星期X(0-6,0代表星期天)
    var curHour = curDate.getHours();
    if(curHour>=0 && curHour<=9){
      curHour = '0'+curHour;
}     //获取当前小时数(0-23)
    var curMinute = curDate.getMinutes();   // 获取当前分钟数(0-59)

    var curSec =curDate.getSeconds();      //获取当前秒数(0-59)

    var Current= curYear+timeStr+curMonth.toString().padStart(2,0)+timeStr+curDay.toString().padStart(2,0)+' '+curHour.toString().padStart(2,0)+':'+curMinute.toString().padStart(2,0)+':'+curSec.toString().padStart(2,0);
    console.log(Current);
    // this.datetime=Current;
    return Current;
}

//年-月-日 时-分-秒

function getTime(obj){


    const d = new Date(obj);
    const resDate = d.getFullYear() + '-' + p((d.getMonth() + 1)) + '-' + p(d.getDate())
    const resTime = p(d.getHours()) + ':' + p(d.getMinutes()) + ':' + p(d.getSeconds())

    return resDate+''+resTime;
}
//中国标准时间格式转换
function timeFormat(d) {
    if(d==null||d==""){
        return "";
    }
    var a = d.toString();
    var stu = a.endsWith(")");
    if(stu === false){
        return d ;
    }
    var datetime=d.getFullYear() + '-' + (d.getMonth() + 1).toString().padStart(2,0) + '-' + d.getDate().toString().padStart(2,0) + ' '
        + d.getHours().toString().padStart(2,0) + ':' + d.getMinutes().toString().padStart(2,0) + ':' + d.getSeconds().toString().padStart(2,0);
    return datetime;
}

你可能感兴趣的:(js日期格式处理)