Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

parseDate function needs tightening up 1

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
I have wanged this parsedate function from a script, cargo cult style - woot!!!!

Code:
// Parse Date Function
function parseDate(value) {
  if ((typeof value == "object") && (value.constructor == Date)) {
    return value;
  }
  value += ""; // Force conversion to string
  var date = NaN;
  var dateArray = value.split("/");
  if (dateArray.length != 3) {
    dateArray = value.split("-");
  }
  if (dateArray.length == 3) {
    var month = parseInt(dateArray[1]);
    var day = parseInt(dateArray[0]);
    var year = parseInt(dateArray[2]);

   [b] // Rolling Y2K compliance:
    if (year < 100) {
      // Add current century to any 2-digit year
      var today = new Date();
      var todayYear = today.getFullYear();
      var century = parseInt(todayYear / 100);
      year = 100 * century + year;

      // If more than 30 years in future, subtract a century
      if ((year - todayYear) > 30) {
        year = year - 100;
      }

      // If more than 70 years in the past, add a century
      if ((todayYear - year) > 70) {
        year = year + 100;
      }
    }[/b]

    if (!(isNaN(day) || isNaN(month) || isNaN(year))) {
      date = new Date(year, month-1, day);

      // Verify that day, month and year are the same
      // as their original values:
      var newMonth = date.getMonth()+1;
      var newDay = date.getDate();
      var newYear = date.getFullYear();
      if ( (newYear != year) || (newMonth != month) ||
           (newDay != day) ) {
        date = NaN;
      }
    }
  }
  return date;
}

anyhow, I would like to tidy it up as it allows 212 as a year as well as 19234 as a year, now technically there can be these years, but not in my life time!!!! , so what's the best way to force 4 digit year?

I've highlighted the bit i beleive needs changing but not to sure what to change as I'm a little confused to what they are doing in the first place :)

also is there an easy way in JS to do a date compare so i can check that one date is not prior to another entered?

many thanks.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
also is there an easy way in JS to do a date compare so i can check that one date is not prior to another entered?

Assuming they are both date objects:
Code:
if (a.getTime() > b.getTime()) {
   [gray][i]//do something[/i][/gray]
}

I haven't got time to read the rest of your question yet....

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
that works a treat thanks :)

//sub -> comp date compare
if(subok && compok){

var date1 = parseDate(frm.subdate.value);
var date2 = parseDate(frm.compdate.value);

if(date1.getTime() > date2.getTime()){
msg += "Sub -> Comp date mismatch.\n";
}
}

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
I've also had a think and it would be simple to do a date comparison for my other request.

var dte1 = new Date(year,month,day);
var dte2 = new Date();

then check if dates are less than or equal to the first or greater than the second!

thanks again khat :)

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top