js 判断日期是否是当天

/**
                如果是当日的话显示hh:mm;
                不是当日的话显示MM-dd;
                传入的时间格式为'yyyy-MM-dd hh:mm:ss | yyyy-MM-dd hh:mm'
                **/

var reg = /^(\d{4})-(\d{1,2})-(\d{1,2}) (\d{2}):(\d{2})(?::\d{1,2})?$/;
                var standard = new Date();
                var getSentMessageTime = function(other){
                    // 通过正则得到["2010-02-25 12:23:01", "2010", "02", "25", "12", "23"]
                    var times =other.match(reg) ;  
                    if(isToDay(times[1],times[2],times[3])){
                        return times[4]+":"+times[5];
                    }else return times[2]+"-"+times[3];                   
                };           
                function isToDay(year,month,day){
                    return (parseInt(year) == standard.getFullYear()) && (parseInt(month) == standard.getMonth()+1) && (parseInt(day) == standard.getDate());
                };
               
                var getSentTimeByMill = function(other){
                    var time = new Date(other);                                       
                        var otherMonth = time.getMonth()+1;
                        otherMonth = otherMonth>9?otherMonth:'0'+otherMonth;
                        var otherDay = time.getDate();
                        otherDay = otherDay>9?otherDay:'0'+otherDay;
                        var otherYear = time.getFullYear();
                       
                        var otherDate = otherMonth+'-'+otherDay;   
                        var hours = time.getHours();
                        var minu = time.getMinutes()
                        var otherTime =(hours>9?hours:'0'+hours) +":"+ (minu>9?minu:'0'+minu);
                        if(isToDay(otherYear,otherMonth,otherDay)){
                            return otherTime;
                        }else return otherDate;       
                };

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值