日期:2014-05-18 浏览次数:20584 次
校验日期的函数 1.校验日期 <script language=javascript> function isdate(str) { var r = str.match(/^(\d{1,4})(-\/)(\d{1,2})\2(\d{1,2})$/); if(r==null)return false; var d = new date(r[1], r[3]-1, r[4]); return (d.getfullyear()==r[1]&&(d.getmonth()+1)==r[3]&&d.getdate()==r[4]); } alert(isdate("2002-01-31")); alert(isdate("2002-01-41")); </script> 2.校验日期加时间 <script language=javascript> function istime(str) { var r = str.match(/^(\d{1,4})(-\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/); if(r==null)return false; var d = new date(r[1], r[3]-1,r[4],r[5],r[6],r[7]); return (d.getfullyear()==r[1]&&(d.getmonth()+1)==r[3]&&d.getdate()==r[4]&&d.gethours()==r[5]&&d.getminutes()==r[6]&&d.getseconds()==r[7]); } alert(istime("2002-1-31 12:34:56")); alert(istime("2001-2-29 12:54:56")); alert(istime("2002-1-41 12:00:00")); </script>
------解决方案--------------------
正则:
string s = "1986/4/11 0:00:00"; Match match = Regex.Match(s, @"\d{4}/(?<month>\d{1,2})/(?<day>\d{1,2})"); if (match.Groups["month"].Value == DateTime.Now.Month.ToString() && match.Groups["day"].Value == DateTime.Now.Day.ToString()) Response.Write("生日快乐!");
------解决方案--------------------
Regex re = new Regex(@"/[\d]{1,2}/[\d]{1,2}"); string sr = "1986/4/11 0:00:00"; string a = re.Matches(sr)[0].ToString(); string b = re.Matches(DateTime.Now.ToShortDateString())[0].ToString(); if (a==b) { Console.WriteLine("Happy"); }
------解决方案--------------------
[code=Java][/code]
String sr = "(\\d{4})([09]|[9])[27]";
Pattern p = Pattern.compile(sr);
Matcher m = p.matcher("20100927");
if(m.find()){
System.out.println("OK");
}else{
System.out.println("NO");
}
------解决方案--------------------
[code=Java][/code]
String sr = "(\\d{4})([09]|[9])[27]";
Pattern p = Pattern.compile(sr);
Matcher m = p.matcher("20100927");
if(m.find()){
System.out.println("OK");
}else{
System.out.println("NO");
}
------解决方案--------------------
string time="1986/9/27 0:00:00"; Regex reg=new Regex(@"\d{4}/(\d{1,2})/(\d{1,2})"); if(reg.IsMatch(time) && reg.Match(time).Groups[1].Value==DateTime.Now.Month.ToString() && reg.Match(time).Groups[2].Value==DateTime.Now.Day.ToString() ) { Console.WriteLine("Happy Birthday"); }
------解决方案--------------------
[code=Java][/code] // 1986/9/27 0:00:00
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm");
String s="1986/9/27 0:00:00";
String sraaa = sdf.format(new Date(s));
String sr = "(\\d{4})([" + sraaa.substring(4,6) + "])[" + sraaa.substring(6,8) + "]";
System.out.println(sr);
//String sr = "(\\d{4})([09]|[9])[27]";