Skip to content

Commit

Permalink
added interpret_year to decide whether to interpret two digit year to…
Browse files Browse the repository at this point in the history
… start with 1900 or 2000
  • Loading branch information
Shashank Shetty committed Oct 31, 2012
1 parent 36587f1 commit 1e680d5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -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();

Expand Down
15 changes: 13 additions & 2 deletions datetimeformatter.js
Expand Up @@ -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 = {};

Expand Down Expand Up @@ -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
};
}
};

0 comments on commit 1e680d5

Please sign in to comment.