Following Javascript code snippet can be used to convert m/d/yy date to mm/dd/yyyy
var objtxtDate = document.getElementById("textboxDate");
var regex = /^\d{1,2}\/\d{1,2}\/\d{1,2}$/;
if (objtxtShipDate.value.match(regex))
{
var dateArr = objtxtShipDate.value.split("/");
var regex1 = /^(\d)$/; // for dd and mm
var regex2 = /^(\d\d)$/; // for yy
dateArr[0] = dateArr[0].replace(regex1, "0$1");
dateArr[1] = dateArr[1].replace(regex1, "0$1");
var now = new Date();
var year = now.getYear();
if(year < 2000) { year = year + 1900; }
var currentYear = year + '';
var regexYear = /^(\d\d)\d\d$/;
currentYear = currentYear.replace(regexYear, "$1");
dateArr[2] = dateArr[2].replace(regex2, currentYear + "$1");
objtxtDate.value = dateArr.join("/");
}
1 comment:
var objtxtShipDate = document.getElementById("txtShipDate");
var regex = /^\d{1,2}\/\d{1,2}\/\d{1,2}$/;
if (objtxtShipDate.value.match(regex))
{
var dateArr = objtxtShipDate.value.split("/");
var regex1 = /^(\d)$/; // for dd and mm
var regex2 = /^(\d\d)$/; // for yy
dateArr[0] = dateArr[0].replace(regex1, "0$1");
dateArr[1] = dateArr[1].replace(regex1, "0$1");
dateArr[2] = dateArr[2].replace(regex1, "0$1");
if (dateArr[2] >= 30)
dateArr[2] = "19" + dateArr[2];
else
dateArr[2] = "20" + dateArr[2];
objtxtShipDate.value = dateArr.join("/");
}
Post a Comment