Delphi中输入年、月、日及润年平年的判断

来源:互联网 发布:caffe傻瓜系列 编辑:程序博客网 时间:2024/06/01 07:33

procedure TfrmMain.checkDateEX(edt: Tmaskedit);
var
  iyear, imonth, iday: word;
begin
  if edt.Text <> '    -  -  ' then
  begin
    iyear := strToint(copy(edt.Text, 1, 4));
    imonth := strToint(copy(edt.Text, 6, 2));
    iday := strToInt(copy(edt.Text, 9, 2));
    if (iyear < 1930) then
    begin
      showmessage('非法日期,请重新输入!');
      edt.SetFocus;
      exit;
    end
    else
    begin
      if (imonth < 1) or (imonth > 12) then
      begin
        showmessage('非法日期,请重新输入!');
        edt.SetFocus;
        exit;
      end;
      if imonth = 2 then
      begin
        if ((iyear mod 4) = 0) or ((iyear mod 100) = 0) then
        begin
          if (iday > 29) or (iday < 1) then
          begin
            showmessage('非法日期,请重新输入!');
            edt.SetFocus;
            exit;
          end;
        end
        else
        begin
          if (iday > 28) or (iday < 1) then
          begin
            showmessage('非法日期,请重新输入!');
            edt.SetFocus;
            exit;
          end;
        end;
      end
      else if (imonth = 1) or (imonth = 3) or (imonth = 5) or (imonth = 7) or
        (imonth = 8) or (imonth = 10) or (imonth = 12) then
      begin
        if (iday > 31) or (iday < 1) then
        begin
          showmessage('非法日期,请重新输入!');
          edt.SetFocus;
          exit;
        end;
      end
      else
      begin
        if (iday > 30) or (iday < 1) then
        begin
          showmessage('非法日期,请重新输入!');
          edt.SetFocus;
          exit;
        end;
      end;
    end;
  end;

end;

原创粉丝点击