开发者社区> 问答> 正文

JQ时间比较,为什么不相等?

 var time = new Date();
   var nowDay = time.toLocaleDateString().replace(/\//g, '-');
   var createtime_Y = unix_to_datetime(1459407384, 1);
   function unix_to_datetime(unix, type) {
       var now = new Date(parseInt(unix) * 1000);
       var Y = now.getFullYear() + '-';
       //0-11,0代表1月
       var M = now.getMonth()+1 + '-';
       var D = now.getDate() + ' ';
       var h = (now.getHours() < 10 ? '0'+(now.getHours()) : now.getHours()) + ':';
       var m = (now.getMinutes() < 10 ? '0'+(now.getMinutes()) : now.getMinutes()) + ':';
       var s = (now.getMinutes() < 10 ? '0'+(now.getMinutes()) : now.getMinutes());
       switch(type) {
           case 1:
               return Y+M+D;
           case 2:
               return h+m+s;
           case 3:
               var M = (now.getMonth()+1 < 10 ? '0'+(now.getMonth()+1) : now.getMonth()+1) + '月';
               return M+D+h+m+s;
       }
   }
       console.log(nowDay);
       console.log(typeof(nowDay));
       console.log(createtime_Y);
       console.log(typeof(createtime_Y));
       if (nowDay == createtime_Y) {
           console.log(1);
       } else {
           console.log(2);
       }

screenshot
如图?why?
为什么输出的是2???求解

展开
收起
杨冬芳 2016-06-21 17:54:45 2047 0
1 条回答
写回答
取消 提交回答
  • IT从业

    var D = now.getDate() + ' ';

    nowDay 31后面有空格。去掉空格试试吧,不对的话我就不知道了。另外==等号不是严格相等,不用查看类型,类型不一样也相等。

    2019-07-17 19:45:10
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载