Answered by:
date time checking in c#.net

Question
-
User1432265183 posted
this is minimum and maximum date in ms sql - January 1, 1753, through December 31, 9999
I want to check this in c#.net code. Is there any efficient way to check ?
Monday, July 29, 2013 7:02 AM
Answers
-
User281315223 posted
Oops.
I was quite sure that the above would work (as I am not noticing any syntax issues on my machine), you may actually need to access the Value property of the SqlDateTime object to perform the comparison :
//The Value property actually yields a DateTime object that can be used for comparisons SqlDateTime.MinValue.Value
so for a more complete example :
using System.Data.SqlTypes; protected void Page_Load(object sender, EventArgs e) { //If a PostBack occurred if(IsPostBack) { //Check if your Date is valid against the SqlRange if (YourDateTime >= SqlDateTime.MinValue.Value && YourDateTime <= SqlDateTime.MaxValue.Value) { } } }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, July 29, 2013 8:57 AM
All replies
-
User1428336426 posted
DateTime minDate = DateTime.MaxValue;
DateTime maxDate = DateTime.MinValue;Monday, July 29, 2013 7:14 AM -
User-18289217 posted
Response.Write("Minimum " + DateTime.MinValue.ToLongDateString() + "<br />" + "Maximum " + DateTime.MaxValue.ToLongDateString());
to check a certain date if it's in the given range you can do the following:
if (tobechecked > DateTime.MinValue && tobechecked < DateTime.MaxValue) { // Do Something } else { // Do Something }
Monday, July 29, 2013 7:16 AM -
User1432265183 posted
this will give minDate as 1/1/0001 12:00:00 AM ..
But in ms sql, it won't allow this for the datatype datetime..
for ms sql - I've mentioned the minimum value...
Monday, July 29, 2013 7:41 AM -
User1432265183 posted
DateTime minDate = DateTime.MaxValue; DateTime maxDate = DateTime.MinValue;
In c#.net, minDate will give as 1/1/0001 12:00:00 AM ..
In Ms sql min and max date will be for datetime type January 1, 1753, through December 31, 9999..
Ms sql won't allow less than that min value and greater than that maximum value.
Monday, July 29, 2013 8:08 AM -
User-2001332922 posted
Vague to say that SqlClient in Ado.net object that must be one looking for.
Monday, July 29, 2013 8:14 AM -
User281315223 posted
You can actually check the minimum available DateTime within SQL using the SqlDateTime.MinValue property and SqlDateTime.MaxValue respectively :
using System.Data.SqlTypes; protected void Page_Load(object sender, EventArgs e) { //If a PostBack occurred if(IsPostBack) { //Check if your Date is valid against the SqlRange if (YourDateTime >= SqlDateTime.MinValue && YourDateTime <= SqlDateTime.MaxValue) { } } }
Monday, July 29, 2013 8:18 AM -
User-18289217 posted
In that case you may want to check against
Monday, July 29, 2013 8:22 AM -
User1432265183 posted
You can actually check the minimum available DateTime within SQL using the SqlDateTime.MinValue property and SqlDateTime.MaxValue respectively : using System.Data.SqlTypes; protected void Page_Load(object sender, EventArgs e) { //If a PostBack occurred if(IsPostBack) { //Check if your Date is valid against the SqlRange if (YourDateTime >= SqlDateTime.MinValue && YourDateTime <= SqlDateTime.MaxValue) { } } }
Cannot check C#.net date time to sqlDateTime...
Monday, July 29, 2013 8:37 AM -
User281315223 posted
Oops.
I was quite sure that the above would work (as I am not noticing any syntax issues on my machine), you may actually need to access the Value property of the SqlDateTime object to perform the comparison :
//The Value property actually yields a DateTime object that can be used for comparisons SqlDateTime.MinValue.Value
so for a more complete example :
using System.Data.SqlTypes; protected void Page_Load(object sender, EventArgs e) { //If a PostBack occurred if(IsPostBack) { //Check if your Date is valid against the SqlRange if (YourDateTime >= SqlDateTime.MinValue.Value && YourDateTime <= SqlDateTime.MaxValue.Value) { } } }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, July 29, 2013 8:57 AM -
User-18289217 posted
Not necessarily ... the following code works just fine without using Value:
DateTime tobechecked = DateTime.Now; if (tobechecked > SqlDateTime.MinValue && tobechecked < SqlDateTime.MaxValue) { Response.Write("It's in the MSSQL dates range!!!"); } else { Response.Write("It's NOT in the MSSQL dates range!!!"); }
Monday, July 29, 2013 9:13 AM -
User1432265183 posted
Oops. I was quite sure that the above would work (as I am not noticing any syntax issues on my machine), you may actually need to access the Value property of the SqlDateTime object to perform the comparison : //The Value property actually yields a DateTime object that can be used for comparisons SqlDateTime.MinValue.Value
so for a more complete example :
using System.Data.SqlTypes; protected void Page_Load(object sender, EventArgs e) { //If a PostBack occurred if(IsPostBack) { //Check if your Date is valid against the SqlRange if (YourDateTime >= SqlDateTime.MinValue.Value && YourDateTime <= SqlDateTime.MaxValue.Value) { } } }
Thank you. Great.
Tuesday, July 30, 2013 12:52 AM