16

i came across a situation that, i need to get only the Date out from DateTime.

i am having a DateTime? StartDate property (Nullable) used to hold the date value

i tried below,

var d = Convert.ToDateTime(StartDate).Date;

but its returning me d as eg. 6/22/2006 12:00:00AM

after doing var d = Convert.ToDateTime(StartDate).Date.ToString("d");

i'm able to get d as 6/22/2006..but i dont want to convert my DateTime? to String

is their any way to get only the Date without using the ToString("d")?

user3085995
  • 403
  • 1
  • 6
  • 19
  • 3
    A `DateTime` has always a time component, even the `Date`-property returns a `Date` with a time set to 12:00:00 midnight. So no, there is no way to get a `Date`-only without converting it to string. But why are you missing it? – Tim Schmelter Mar 11 '14 at 08:10
  • Instead of `Convert.ToDateTime(StartDate).Date`, use `StartDate.Value.Date`. Both will throw an exception if the `StartDate` is null, so you should make sure it is not. – Jeppe Stig Nielsen Mar 11 '14 at 08:40
  • 1
    Can you explain why this is actually a problem? Is there any specific reason why you can't just use a `DateTime` and simply ignore the time-part? – Kjartan Mar 11 '14 at 10:09

4 Answers4

25

Use the Date property to get the Date component of DateTime instance:

var dateAndTime = DateTime.Now;
var date = dateAndTime.Date;

With this approach, Date property will return the date at midnight. So the time part will be 00:00:00 in this case.

There are couple of alternate ways to get the just the Date part, but the return type of it will be a string:

1.) var date=DateTime.Now.ToString("dd/MM/yyyy"); //Can also use .ToString("dd-MM-yyyy");

2.) var dateOnlyString = dateTimeNow.ToShortDateString(); //Return 00/00/0000

Reference: here.

Saket
  • 3,043
  • 3
  • 24
  • 46
12

try this: string x = DateTime.Now.ToShortDateString().

this will get the date dd/mm/yy given to the string x.

Max Al Farakh
  • 4,046
  • 4
  • 27
  • 52
Ralph De Guzman
  • 169
  • 3
  • 11
2

I think you question is sort of... moot.

You ask for a date without a time, but get a DateTime, which has both. I really don't think that should be a problem in most cases though:

If you create a DateTime with a certain date, and compare it to another date, and both of these have their time set to midnight, your comparisons will be valid and correct. Eg:

var yesterday = new DateTime(2014, 3, 10);
var today = new DateTime(2014, 3, 11);
var tomorrow = new DateTime(2014, 3, 12);

Comparing and sorting these will work as you expect, and so will the following:

if(today == DateTime.Today){
    Console.WriteLine("Today is the day!");
}

In other words, you should be perfectly fine just pretending like the time-part does not exist.

Also, as you touched upon yourself in the OP, you can use the property Date if you want to make sure to avoid any time-component:

// Note the addition of hours, minutes and seconds:
var today = new DateTime(2014, 3, 11, 14, 35, 33); 

if(today == DateTime.Today){
    Console.WriteLine("This never happened...");    
}

if(today.Date == DateTime.Today){
    Console.WriteLine("...But today is still the day!");    
}
Kjartan
  • 17,127
  • 14
  • 67
  • 84
-1

A DateTime does have both a date and a time. You can decide with yourself that in a specific property you well never use the date part. It will just be 12:00 AM, but you won't use it.

In some situations it can be useful to write your own type that can never hold a time-of-day component. Here is a start:

struct Date : IFormattable
{
    readonly DateTime value;
    public Date(DateTime dateAndTime)
    {
        value = dateAndTime.Date;
    }

    public string ToString(string format, IFormatProvider formatProvider)
    {
        return value.ToString(format ?? "d", formatProvider);
    }
    public string ToString(string format)
    {
        return ToString(format, null);
    }
    public string ToString(IFormatProvider formatProvider)
    {
        return ToString(null, formatProvider);
    }
    public override string ToString()
    {
        return ToString(null, null);
    }

    public static implicit operator DateTime(Date date)
    {
        return date.value;
    }
}

The field value does hold the 12 AM thing, but it is private and is not seen from the outside. The overloads of ToString() make sure that unless something else is requested, the Date is written out with the short date format of the current culture.

Jeppe Stig Nielsen
  • 54,796
  • 9
  • 96
  • 154