javascript - JQ时间比较,为什么不相等?
伊谢尔伦
伊谢尔伦 2017-04-10 18:06:20
[JavaScript讨论组]
    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);
        }

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

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

全部回复(2)
迷茫
var D = now.getDate() + ' ';

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

怪我咯

var D = now.getDate()+''; 这里,你的后边是一个空格吧,那么下边的if语句,两个字符串比较,createtime_Y末尾多了一个空格,当然不等了。你去掉那个空格试下看看

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号