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
}

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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、正则表达式的概念 正则表达式(英语:Regular Expression,在代码中常简写为regex)。正则表...
    圣贤与无赖阅读 4,371评论 0 3
  • 导读 正则表达式是什么?有什么用?正则表达式(Regular Expression)是一种文本规则,可以用来校验、...
    静默虚空阅读 3,813评论 0 21
  • 正则表达式全部符号解释(转) 常用正则表达式大全 (转) 不错的正则博客 正则的方法 var reg = /[1-...
    me__me11阅读 4,617评论 0 0
  • PHP常用正则表达式汇总 正则表达式在 PHP 中的应用在 PHP 应用中,正则表达式主要用于: 正则匹配:根据正...
    DragonRat阅读 5,291评论 0 4
  • 正则表达式到底是什么东西?字符是计算机软件处理文字时最基本的单位,可能是字母,数字,标点符号,空格,换行符,汉字等...
    狮子挽歌阅读 6,495评论 0 9