Date.parse()行为不一致

时间:2014-11-12 01:44:08

标签: javascript parsing datetime web-scraping

我目前正在使用节点创建一个显示冒险时间下一集播出天数的网站。 MVP的方法是刮取AT episodes wikipedia page日期,并显示当天后最快的时间。

现在,当我尝试对第6列的文本使用Date.parse()时,只有特定的格式有效。这对我来说很有意义,但是我发现有时解析有效,有时它不会出现出现的字符串格式完全相同。我将在下面解释下面显示刮擦的代码 - >解析:

router.get('/adventure', function(req, res) {
    var url = 'http://en.wikipedia.org/wiki/List_of_Adventure_Time_episodes';

    request(url, function(error, response, html){
        if(!error){
            var $ = cheerio.load(html);

            $('.vevent > td:nth-child(6)').filter(function(){
                var rough = $(this).text(),
                    data;
                console.log('rough '+ JSON.stringify(rough));
                // var data = (rough.indexOf('(') == -1)? rough : rough.split('(')[0].slice(0,-1)
                if (rough.indexOf('(') > 0) {
                    data = rough.split('(')[0].slice(0,-1);
                    console.log(JSON.stringify(data));
                    console.log(Date.parse(data));
                } else {
                    data = rough
                    console.log(JSON.stringify(data));
                    console.log(Date.parse(data));
                }
                console.log(typeof JSON.stringify(data));
                if (data == 'October 28, 2014') {
                    res.json({woof:data})
                }
            });
        }
    });
});

*如果您觉得有必要指出与Date.parse()无关的内容,请随意!我在耳朵后面湿了,渴望学习。

我使用JSON.stringify()试图暴露任何特殊字符,但它们似乎不是问题所在。以下是服务器的日志:

rough "July 11, 2011"
"July 11, 2011"
1310356800000
string
rough "July 18, 2011"
"July 18, 2011"
1310961600000
string
...
rough "October 15, 2012 (2012-10-15)"
"October 15, 2012"
NaN
string
rough "October 22, 2012 (October 22, 2012)"
"October 22, 2012"
1350878400000
string
rough "November 12, 2012 (2012-11-12)"
"November 12, 2012"
NaN
string
...
rough "\nTBA\n"
"\nTBA\n"
NaN

所以真正令我感到奇怪的是,据说被切断的信息会影响它是否被正确解析。是的,我也因为不一致的日期格式化而感到困惑哈哈

还有其他人遇到过这样的事吗?我只是完全错过了什么?谢谢你的时间!

0 个答案:

没有答案