找回密碼
 註冊
搜索
查看: 5486|回復: 0

判斷時間格式是否正確的代碼(C#,VB,C++)

[複製鏈接]
發表於 2007-9-14 22:17:32 | 顯示全部樓層 |閱讀模式
c#
using System;
using System.Collections.Generic;
using System.Text;
namespace isdata
{
    class dataa
    {
        public static bool IsData(int y, int m, int d)
        {
            if (y <= 1000 || y >= 10000) return false;
            if (m < 1 || m > 12) return false;
            int[] month =new int[12]  {31,0,31,30,31,30,31,31,30,31,30,31};
            if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0)
                month[1] = 29;
            else
                month[1] = 28;
           return   (d>=1)&&(d<=month[m-1]);  
        }
    }
}

VB
Imports System
Imports System.Collections.Generic
Imports System.Text

Namespace isdata
    Class dataa
        Public Shared Function IsData(ByVal y As Integer, ByVal m As Integer, ByVal d As Integer) As Boolean
            If y <= 1000 Or y >= 10000 Then
              Return False
            End If
            If m < 1 Or m > 12 Then
              Return False
            End If
            Dim month() As Integer = New Integer(12) {31,0,31,30,31,30,31,31,30,31,30,31}
            If y % 4 = Decimal.Remainder( 0 And y ,  100 )<> 0 Or y % 400 = 0 Then
                month(1) = 29
            Else
                month(1) = 28
            End If
           Return   (d>=1)&&(d<=month(m-1))
        End Function

    End Class
End Namespace

c++

bool   legalday(int   y,int   m,int   d)   
   {     if(y<=0)   return   false;   
   if(m<1||m>12)   return   false;   
   int   month[]=   
   {31,0,31,30,31,30,31,31,30,31,30,31};   
   
   if(y%4==0&&y%100!=0   ||   y%400==0)   
      month[1]=29;   
   else   month[1]=28;   
   return   (d>=1)&&(d<=month[m-1]);   
   }

Reference: http://www.cnblogs.com/mz121star/archive/2007/09/14/893415.html
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

Archiver|手機版|彩色筆小沛的知識庫

GMT+8, 2024-5-3 04:59 , Processed in 0.016145 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回復 返回頂部 返回列表