插入数据库之前的验证日期(闰年) [英] Validating Date (Leap year or not) before inserting into Database

查看:174
本文介绍了插入数据库之前的验证日期(闰年)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我需要在数据库中插入日期时,我需要检查2件事情:


  1. 如果这个月有31天的情况下31被选中

  2. 如果选择了二月份,我需要检查一下这个闰年,以防用户选择29

目前,我正在使用一个填充有的长函数,如果是 else's / p>

在code> C#中是否有任何方法可以在插入之前检查日期是否有效? p>

解决方案

  DateTime temp; 
if(DateTime.TryParse(yourString,out temp))
{
//有效,使用temp插入到DB中。
}
else
{
//无效。
}


When I need to insert date into the database I need to check 2 things:

  1. If this month has 31 days in case day 31 was selected
  2. If February is selected, I need to check if it's a leap year in case the user selected 29

Currently I'm checking this using a long function filled with if's and else's.

Is there any method in C# that can check a date if it's valid or not before I insert it?

解决方案

DateTime temp;
if (DateTime.TryParse(yourString, out temp))
{
    //valid, use temp to insert into DB.
}
else
{
    //not valid.
}

这篇关于插入数据库之前的验证日期(闰年)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆