I'm having trouble properly formatting 2-digit dates as 4-digit dates.
I have a text input field:
<input type="text" value="" class="date"/>
And then I want to format it in the "mm/dd/yyyy" format after the user enters a date. So I have an onChange event:
$('.date').on('change',function(){
var date = new Date($(this).val());
$(this).val((date.getMonth()+1)+'/'+date.getDate()+'/'+date.getFullYear());
});
Okay, so now comes the oddities (I'll be using 12/12/12 as an example date):
1) getFullYear() returns 1912 in all IE, FF. Chrome returns 2012.
2) getYear() returns 112 in IE, Chrome. FF returns 12.
So it seems like the only option at this point is to sniff the user agent, and use that accordingly. Is there anyway around sniffing the UA?
Using moment.js:
const moment = require("moment")
const date = moment("21", "YY")
const year = date.format("YYYY")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With