设置和比较日期以检查信用卡有效性
Posted
技术标签:
【中文标题】设置和比较日期以检查信用卡有效性【英文标题】:Set and compare dates to check credit card validity 【发布时间】:2018-12-23 22:21:17 【问题描述】:我有客户注册了 12 个月的分期付款选项。我需要知道他们的信用卡是否至少在分期付款选项的期限内有效 12 个月。
使用下面收到的对象中的card.exp_year
和card.exp_month
,我将如何计算这张信用卡在一年后是否有效。我假设我必须使用 Date()
函数。
"id": "tok_xxxxxxxx",
"object": "token",
"card":
"id": "card_xxxxxxxx",
"object": "card",
"address_city": null,
"address_country": "BE",
"address_line1": null,
"address_line1_check": null,
"address_line2": null,
"address_state": null,
"address_zip": null,
"address_zip_check": null,
"brand": "Visa",
"country": "CA",
"cvc_check": "unchecked",
"dynamic_last4": null,
"exp_month": 7,
"exp_year": 2019,
"funding": "credit",
"last4": "3086",
"metadata": ,
"name": "John Doe ",
"tokenization_method": null
,
"client_ip": "149.xxx.xxx.xxx",
"created": 1530xxxxxx,
"livemode": true,
"type": "card",
"used": false
【问题讨论】:
你试过什么?如果你应该在纸上这样做,你会在现实生活中怎么做? How do I get the current date in javascript?的可能重复 取一个日期,即当前日期,年份递增一次,然后比较卡到期日的月份和年份 @Luca 好主意,感谢指导,可能为我节省了一些时间 【参考方案1】:谢谢,卢卡!最终的工作解决方案!
var cardYear = new Date();
cardYear.setMonth(12-1); // jan = 0 so minus 1
cardYear.setYear(2019);
console.log(cardYear);
var nextYearFromNow = new Date(new Date().setFullYear(new Date().getFullYear() + 1));
console.log(nextYearFromNow);
console.log(compareTime(cardYear, nextYearFromNow));
function compareTime(time1, time2)
return new Date(time1) > new Date(time2); // true if time1 is later
或
var year = user.attributes.token.card.exp_year;
var month = user.attributes.token.card.exp_month;
var cardYear = new Date(year, month, 0); // returns date according to card and sets day to last day of month becuase credit cards are set to expire on the last day of a month
var nextYearFromNow = new Date(new Date().setFullYear(new Date().getFullYear() + 1));
var expiry = compareTime(cardYear, nextYearFromNow); // if false card is too short
function compareTime(time1, time2)
return new Date(time1) > new Date(time2); // true if time1 is later
【讨论】:
以上是关于设置和比较日期以检查信用卡有效性的主要内容,如果未能解决你的问题,请参考以下文章