Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting (formatting) 2-digit date to a 4-digit date

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?

like image 490
Joel Kinzel Avatar asked Oct 21 '22 18:10

Joel Kinzel


1 Answers

Using moment.js:

const moment = require("moment")
const date = moment("21", "YY")
const year = date.format("YYYY")
like image 172
blaze Avatar answered Oct 24 '22 12:10

blaze