//------------------------------------------------------------
//適当な日付をMM/DDの形式に変換
// 引数: 適当な日付
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
// 返値: MM/DDの形式の日付
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
function convFazyDatemd(sdate)
{
var retDate = "";
var mmdd;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(isNaN(sdate))
{
retDate = sdate.replace(/\D/g, "/");
var tmp = retDate.split("/");
if(tmp.length < 2) return "";
mmdd = tmp.slice(0,2);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
}else
{
var len = sdate.length;
if(len < 3) return "";
mmdd = new Array(
sdate.substr(0, len - 2),
sdate.substr(len - 2, 2)
);
}
if((mmdd[0].length == 0) || (mmdd[1].length == 0)) return "";
if(mmdd[0].length == 1) mmdd[0] = "0" + mmdd[0];
if(mmdd[1].length == 1) mmdd[1] = "0" + mmdd[1];
return mmdd.join("/");
}
//日付入力フィールドのフォーカスが離れたら、正しい形の日付に変換する
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
function txtDate_onBlurmd(ctrl)
{
var ymd = convFazyDatemd(trim(ctrl.value));
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(ymd.length > 0)
{
ctrl.value = ymd;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//------------------------------------------------------------
//適当な日付をYYYY/MM/DDの形式に変換
// 引数: 適当な日付
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
// 返値: YYYY/MM/DDの形式の日付
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
function convFazyDate(sdate)
{
var retDate = "";
var ymd;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(isNaN(sdate))
{
retDate = sdate.replace(/\D/g, "/");
var tmp = retDate.split("/");
if(tmp.length < 3) return "";
ymd = tmp.slice(0,3);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
}else
{
var len = sdate.length;
if(len < 5) return "";
ymd = new Array(
sdate.substr(0, len - 4),
sdate.substr(len - 4, 2),
sdate.substr(len - 2, 2)
);
}
if((ymd[0].length == 0) || (ymd[1].length == 0) || (ymd[2].length == 0)) return "";
if(ymd[0].length == 1) ymd[0] = "200" + ymd[0];
if(ymd[0].length == 2) ymd[0] = "20" + ymd[0];
if(ymd[0].length == 3) ymd[0] = "2" + ymd[0];
if(ymd[1].length == 1) ymd[1] = "0" + ymd[1];
if(ymd[2].length == 1) ymd[2] = "0" + ymd[2];
return ymd.join("/");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//日付入力フィールドのフォーカスが離れたら、正しい形の日付に変換する
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
function txtDate_onBlur(ctrl)
{
var ymd = convFazyDate(trim(ctrl.value));
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(ymd.length > 0)
{
ctrl.value = ymd;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//金額をカンマ編集して右に詰める
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
function txtConmma_onBlur(ctrl)
{
var kin = convConmma(trim(ctrl.value));
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(kin.length > 0)
{
ctrl.value = kin;
}
ctrl.style.textAlign = "right";
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//年月入力フィールドのフォーカスが離れたら、正しい形の年月に変換する
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
function txtYearMonthCheck(sDate)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
var withDummyDay = sDate + "/01"; /**//* ダミーで01日を設定 */
var tempDate = convFazyDate(withDummyDay);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(tempDate == "")
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
withDummyDay = sDate + "01"; /**//* "/"の無い01日を設定 */
tempDate = convFazyDate(withDummyDay);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(tempDate == "")
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
return ""; /**//* あきらめる */
}
}
var result = txtYearMonthDayCheck(tempDate)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(result == "")
{
return "";
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
return result.substr(0,7); /**//* YYYY/MM/DDである前提 */
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//年月入力フィールドのフォーカスが離れたら、正しい形の年月に変換する
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
function txtYearMonth_onBlur(ctrl, msg)
{
if(ctrl.value.length == 0) return;
var ym = txtYearMonthCheck(trim(ctrl.value));
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(ym.length == 0)
{
alert(msg);
ctrl.focus();
ctrl.select();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else
{
ctrl.value = ym
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//月日入力フィールドのフォーカスが離れたら、正しい形の月日に変換する
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
function txtMonthDayCheck(sDate)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
var withDummyYear = "2000/" + sDate; /**//* 2000年なので2/29許容 */
var result = txtYearMonthDayCheck(withDummyYear)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(result == "")
{
return "";
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
return result.substr(5,5); /**//* YYYY/MM/DDである前提 */
}
}