javascript常用时间格式化方法(星期、日期、时间)

export const getTime = function() {
    //获取当前签到时间  : 12:00  
    let date = new Date(),hour = date.getHours()>=10?date.getHours():'0'+date.getHours(),minute = date.getMinutes()>=10?date.getMinutes():'0'+date.getMinutes();
    return hour+':'+minute
};

export const getDate = function(y="年",m="月",d="日") {
    //获取当前签到日期  : 2019年10月31日
    let date = new Date(),year = date.getFullYear(),month = (date.getMonth()+1)>=10?date.getMonth()+1:'0'+(date.getMonth()+1),day=date.getDate()>=10?date.getDate():'0'+date.getDate();
    return year+y+month+m+day+d
};

export const getWeek = function() {
    //获取今天是星期几
    let arr = ["日", "一", "二", "三", "四", "五", "六"],week = new Date().getDay(); 
    return arr[week]
};

export const getYesterday = function(){
    // 获取昨天日期
    let day = new Date();
    day.setTime(day.getTime()-24*60*60*1000);
    return day.getFullYear()+"-" + ((day.getMonth()+1)>=10?(day.getMonth()+1):'0'+(day.getMonth()+1)) + "-" + (day.getDate()>=10?day.getDate():'0'+day.getDate())
}
export const timestampToTime =function(timestamp){
    //利用时间戳获取时间
    let date = new Date(timestamp)
    let h = date.getHours()>=10?date.getHours():'0'+date.getHours();
    let m = date.getMinutes()>=10?date.getMinutes():'0'+date.getMinutes();
    return h + ':' + m
}
export const timestampToDate = function(timestamp){
    //利用时间戳获取日期
    let date = new Date(timestamp)
    let year=date.getFullYear();
    let mon = (date.getMonth()+1)>=10?date.getMonth()+1:'0'+(date.getMonth()+1);
    let day = date.getDate()>=10?date.getDate():'0'+date.getDate();
    let h = date.getHours()>=10?date.getHours():'0'+date.getHours();
    let m = date.getMinutes()>=10?date.getMinutes():'0'+date.getMinutes();
    return year+'-'+mon+'-'+day +' '+ h + ':' + m
}

常用正则匹配,身份证,手机号域名

你可能感兴趣的:(javascript常用时间格式化方法(星期、日期、时间))