diff --git a/README.md b/README.md index d3f07db..5685003 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,16 @@ Example usages: 992012 -> 9/9/2012 9122012 -> 9/12/2012 +FormatTime uses interpret_year to decide whether to go with date in 1900 or in 2000 for a two digit year. +For Example, Should 91 be interpreted as 1991 or 2091. +Default is 20, meaning any year within 20 years from current year going forward will be in 2000, or else they will be in 1900. +By default, 31 is changed to 2031 and 34 is 1934, unless you set a different number. + +Example usages: +$(".date").formatDate({interpret_year:10}); -> 092322 -> 09/23/2022 +$(".date").formatDate({interpret_year:10}); -> 092325 -> 09/23/1925 + + Format Time: Transforms time into a representable time format (00:00 AM/PM) $(".time").formatTime(); diff --git a/datetimeformatter.js b/datetimeformatter.js index 3054f90..a8ec569 100644 --- a/datetimeformatter.js +++ b/datetimeformatter.js @@ -59,7 +59,11 @@ $.fn.formatTime = function () { $(this).val(formatted_time); }; -$.fn.formatDate = function () { +$.fn.formatDate = function (opts) { + opts = jQuery.extend({ + interpret_year:20 + }, opts); + var date = $(this).val(); var dateObj = {}; @@ -96,12 +100,19 @@ $.fn.formatDate = function () { var month = date.slice(0, i); var day = date.slice(i, j); var year = date.slice(j); + var displayYear = year; + + if (year.length == 2) { + var currentYear = new Date().getFullYear(); + currentYear = currentYear % 100; + displayYear = Number(year) > currentYear + opts.interpret_year ? "19" + year : "20" + year + } return { month:Number(month), day:Number(day), year:Number(year), - display:month + "/" + day + "/" + (year.length == 2 ? "20" + year : year) + display:month + "/" + day + "/" + displayYear }; } }; \ No newline at end of file