0

<span>我有一个填充标签的信用卡字段,例如

<span>****-****-****-1111 (Expires 12/2012)</span>

我需要提取日期并确定它是否在过去。

目前我有下面的 jQuery,但我被困在 split() 点以仅提取日期。

var $selectedDate = $('.prev-card .chzn-container .chzn-single span').text().split();
var $now = new Date();
if ($selectedDate < $now) {
    alert('past')
}
else{
    alert('future')
}

我认为这涵盖了所有内容,但请随时询问更多信息

4

3 回答 3

2

尝试这个:

var selectedDate = $("...").text().match(/Expires (\d+)\/(\d+)/),
    expires = new Date(selectedDate[2],selectedDate[1]-1,1,0,0,0),
    now = new Date();
if( expires.getTime() < now.getTime()) alert("past");
else alert("future");
于 2012-07-26T15:32:49.103 回答
1

对 Kolink 的回答的小修复:

var selectedDate = $("...").text().match(/Expires (\d+)\/(\d+)/),
    expires = new Date(selectedDate[2],selectedDate[1]-1,1,0,0,0),
    now = new Date();
if( expires.getTime() < now.getTime()) alert("past");
else alert("future");

(正则表达式不需要引号)

于 2012-07-26T15:42:23.293 回答
0

我不会拆分它。我会使用正则表达式:

var value = $('.prev-card .chzn-container .chzn-single span').text();
/\d+\/\d+/.exec(value)   //["12/2012"]
于 2012-07-26T15:33:23.173 回答