Browse files

added interpret_year to decide whether to interpret two digit year to…

… start with 1900 or 2000
  • Loading branch information...
1 parent 36587f1 commit 1e680d5bdc75864f17eccde8217ef4d1ab5c88d3 @shashankshetty committed Oct 31, 2012
Showing with 23 additions and 2 deletions.
  1. +10 −0 README.md
  2. +13 −2 datetimeformatter.js
View
10 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();
View
15 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
};
}
};

0 comments on commit 1e680d5

Please sign in to comment.