Solved

DateTime 01/01/0001

Posted on 2016-08-09
7
71 Views
Last Modified: 2016-08-09
hi experts

this variable model.fec_obs_concal contains the value: 01/01/0001 when not selected date,
the value could be null or empty


public DateTime fec_obs_concal { get; set; }

public DateTime concal_fecha { get; set; }
fecha01010001.png
0
Comment
Question by:enrique_aeo
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 41749004
and your question is?
0
 
LVL 26

Accepted Solution

by:
Shaun Kline earned 500 total points
ID: 41749006
If you need the variable to allow for null, use a Nullable DateTime declaration:

public DateTime? fec_obs_concal { get; set; }
0
 

Author Comment

by:enrique_aeo
ID: 41749015
i do
public DateTime? fec_obs_concal { get; set; }

i have this error:
CS0266 C# Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'. An explicit conversion exists (are you missing a cast?)
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 69

Expert Comment

by:Éric Moreau
ID: 41749022
have you tried Shaun comment?
0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 41749025
Did you also change the definition of concal_fecha to be a Nullable DateTime?

You will need to look through your code for other places where dates will need to allow nulls and make the needed changes.
0
 

Author Closing Comment

by:enrique_aeo
ID: 41749039
thanks

I made the change throughout the project
0
 
LVL 33

Expert Comment

by:it_saige
ID: 41749058
Date's cannot be empty.  The default value (which equates to the DateTime.Min value) is '01/01/0001 12:00 AM'.  Proof of concept -
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EE_Q28962384
{
	class Program
	{
		static DateTime date1;

		static void Main(string[] args)
		{
			Console.WriteLine("Date1 = {0}", date1);
			Console.WriteLine("Default DateTime = {0}", default(DateTime));
			Console.WriteLine("DateTime.Min = {0}", DateTime.MinValue);
			Console.ReadLine();
		}
	}
}

Open in new window

Produces the following output -Capture.JPGIf you try to parse an empty string you end up with -Capture.JPGWhich means you are left with using a try...catch or DateTime.TryParse.  If the parse fails, the output is still the MinValue.  Proof of concept:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EE_Q28962384
{
	class Program
	{
		static DateTime date1;

		static void Main(string[] args)
		{
			Console.WriteLine("Date1 = {0}", date1);
			Console.WriteLine("Default DateTime = {0}", default(DateTime));
			Console.WriteLine("DateTime.Min = {0}", DateTime.MinValue);
			if (DateTime.TryParse("", out date1))
				Console.WriteLine("Date1 has changed successfully - {0}", date1);
			else
				Console.WriteLine("Date1 failed to changed, MinValue assigned - {0}", date1);
			Console.ReadLine();
		}
	}
}

Open in new window

Which produces the following output -Capture.JPG
Just an FYI, trying to parse a null value results in the MinValue being set for the date.

That being said, as the others have stated you can add Null support to the DateTime variable by specifying a Nullable<DateTime> or DateTime?.  This will allow for you to support null values for Dates, but remember, that DateTime's cannot be empty.

-saige-
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now