Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 设置并比较日期以检查信用卡有效性_Javascript_Date_Time_Stripe Payments_Credit Card - Fatal编程技术网

Javascript 设置并比较日期以检查信用卡有效性

Javascript 设置并比较日期以检查信用卡有效性,javascript,date,time,stripe-payments,credit-card,Javascript,Date,Time,Stripe Payments,Credit Card,我有客户注册了12个月分期付款的选择。我需要知道他们的信用卡是否至少在分期付款的期限内有效,正好是12个月 使用下面收到的对象的card.exp_year和card.exp_month,我将如何计算此信用卡是否从现在起正好1年有效。我想我必须使用Date函数 { "id": "tok_xxxxxxxx", "object": "token", "card": { "id": "card_xxxxxxxx", "object": "card", "address

我有客户注册了12个月分期付款的选择。我需要知道他们的信用卡是否至少在分期付款的期限内有效,正好是12个月

使用下面收到的对象的card.exp_year和card.exp_month,我将如何计算此信用卡是否从现在起正好1年有效。我想我必须使用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
}

谢谢你,卢卡!最终工作方案

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
}


你试过什么?如果你在纸面上这样做,在现实生活中你会怎么做?可能的重复日期是当前日期,年递增一次,然后比较卡的到期月份和年份date@Luca好主意,谢谢你的指导,可能帮我节省了一些时间
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
}