views:

277

answers:

5

I can't believe how long it's taken me to fail at finding the answer to this seemingly obvious question.

Date SomeRandomMadeUpDate = DateTime.Now.AddMonths(randomMonths).Date;

Cannot implicitly convert type 'System.DateTime' to 'System.Date' I can even call:

Date.Now

but calling .AddDays on it returns a DateTime.

Again, the question is: How do I turn a C# DateTime into a C# Date

+11  A: 

There is no "Date" type as such. It's all DateTimes. It's just that when you call .Date, you are getting that DateTime as of midnight on that day.

If you're talking about the "Date" data type in VB.NET, that's the same thing as DateTime, believe it or not.

Dave Markle
Then why do I get the error I'm getting?
thepaulpage
No idea, I just tried it in VB and it works without any problem...
Meta-Knight
I tried it in C# and there is no System.Date type...
Meta-Knight
@tehp: you cannot obtain such a error, because the "Date" from DateTime.Now.AddMonths(randomMonths).**Date**; is a **DateTime**.
serhio
@ Meta-Knight in VB both type are in fact DateTime, in vb.NET date is used for compatibility
serhio
Perhaps he has a Date class defined somewhere. Nothing in the question suggests the Date class exists in the System namespace.
yodaj007
@serhio: Yes, I assumed it was the same in C# but I see that I was mistaken. If it's not the same type then it explains the error...
Meta-Knight
@yodaj007: Are you sure? -> Cannot implicitly convert type 'System.DateTime' to 'System.Date'
Meta-Knight
@yoda007: Yes it does -- in the error message. It looks like someone has defined a System.Date somewhere. @tehp: Right click and select "Go to Definition" on your Date class. That should give you a clue.
Dave Markle
+10  A: 

.NET doesn't have a type representing just a date - it's problems like this that made me start Noda Time (which isn't even nearly ready for production yet).

DateTime.Date returns another DateTime with the same date as the original, but at midnight. That's basically the closest there is to a Date type in .NET :( Note that if you want today's date then DateTime.Today is a simpler way of calling DateTime.Now.Date.

I've no idea why the compiler is giving an error message that suggests there is a System.Date type - it doesn't on my machine:

using System;

class Test
{
    static void Main()
    {
        Date today = DateTime.Now.Date;
    }
}

Gives this error:

Test.cs(7,9): error CS0246: The type or namespace name 'Date' could not be found (are you missing a using directive or an assembly reference?)

Can you give a similar short but complete program which shows the error message you're getting? Do you have some other library referenced which does have a System.Date type? (As Tommy Carlier suggests, perhaps System.Dataflow?)

Jon Skeet
System.Dataflow.dll (part of SQL Server Modeling) has a type System.Date.
Tommy Carlier
@Tommy: That was my suspicion (see edit :) - that's frankly horrible. Something that's SQL Server specific shouldn't be contributing to the `System` namespace :(
Jon Skeet
@Tommy: say it isn't so.
Dave Markle
@Dave: it is so. :-)
Tommy Carlier
There. I have a reference to System.Dataflow. I guess it should be a DateTime
thepaulpage
+3  A: 

You can construct a new Date object from a DateTime object:

Date d = new Date(DateTime.Now);

See http://msdn.microsoft.com/en-us/library/system.date.date(VS.85).aspx

Loadmaster
+3  A: 

I had to look it up, because I didn't know there was a Date-type in .NET 4.0, but apparently there is one in System.Dataflow.dll. Just use the constructor, with the DateTime as the argument:

Date d = new Date(dt);
Tommy Carlier
Yep. Good dective work, and thanks a lot. Now I know more!
thepaulpage
+1  A: 

Try doing the following

string onlyDate = DateTime.Now.ToString("dd/MM/yyyy");   

this will return Only the date as a string so i think you can use it as

 Date dateNow = new Date(onlyDate );
Madi D.