I've recently noticed in my web application, but bizarrely, not for every user, that the date formats of my inputs which have the date
input type, has been somewhat wrong, the format that I need is: YYYY-MM-DD
, which for most of the time seems to work fine, or at least in my web browser in Safari and Chrome, I've tried to replicate the issue of the incorrect format on my smartphone, but can't figure out why the format is wrong.
The wrong format that I'm getting is: YYYY-DD-M
, for instance: 2021-22-2, which isn't right at all, I'm wondering if there's anything that I'm missing from my HTML input, or anything that might be causing this weird format:
<input type="date" name="income" required>
To the best of my knowledge, the date
input type will format dates correctly? So how would a user be able to bypass this?
Now, I do also have a few hidden
inputs that also contain dates, and it's here where I also see the incorrect format, so I have a few functions that essentially get the date, two dates, in the future, and but in my tests, they give the correct value...
function addDaysToDate (date = new Date(), days) {
const today = new Date(date)
today.setDate(today.getDate() + days)
return today
}
function reverseDateStr (str) {
return str.split('/').reverse().join('/')
}
function getPaydates (
now = new Date(),
nextDaysFromNow = 20,
followingDaysFromNow = 40
) {
const next = new Date(addDaysToDate(new Date(now), nextDaysFromNow)).toLocaleDateString()
const following = new Date(addDaysToDate(new Date(now), followingDaysFromNow)).toLocaleDateString()
return {
next: reverseDateStr(next).split('/').join('-'),
following: reverseDateStr(following).split('/').join('-')
}
}
// usage:
const paydates = getPaydates()
// returns...
// { next: '2021-03-02', following: '2021-03-22' }
So I have no idea if I'm missing something which would cause my dates to sometimes be: YYYY-DD-M, can anyone see anything obvious?