jessepollak / card

:credit_card: make your credit card form better in one line of code

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to get Expiry date as MMYY instead of MM / YY

undercontr opened this issue · comments

Hi everyone.

I wonder if it is safe to do

$("#expiry").val().replace(" / ", "") to get card information and post it to bank

or is there any method to get expiry date in "MMYY" format?

Hi Undercontr,

as you work with date, i would use the Date object.

let expiryDiv = document.querySelector('#expiry')
let expiryContent = expiryDiv.innerHTML
let expiryParts = expiryContent.split('/')
let expiryDate = new Date(expiryParts[1],expiryParts[0]) // Date object format is YY/MM
let formatedExpiryDate = expiryDate.getMonth().toString() + expiryDate.getYear().toString()
console.log(formatedExpiryDate)

it will be more easy if you need to make calculation with expiry date.