1

I have a date time object ,Whose time part I am extracting by this

var dateAndTime = Convert.ToDateTime(certificate.GetEffectiveDateString());
var date = dateAndTime.Date;

date is returning a value :10/23/2013 12:00:00 AM where is should only return 10/23/2013 Don't know why the time part is not removed.

After time part is removed, I need date to be converted in to this format October 23,2013.

Can we remove the time part and change it into this format by some formats defined.

Tilak
  • 30,108
  • 19
  • 83
  • 131
ankur
  • 4,565
  • 14
  • 64
  • 100
  • possible duplicate of [Display only date and no time](http://stackoverflow.com/questions/7124434/display-only-date-and-no-time) – CodeCaster Oct 23 '13 at 11:54

10 Answers10

7

There in no convertion in datetime. If you have a string to datetime, use a DateTime.TryParse. If you have a datetime and want a formatted string. You can use a format string:

dateAndTime.ToString("MMMM dd,yyyy");

will give you a formatted date string.

Peter
  • 27,590
  • 8
  • 64
  • 84
3

Use following to get format October 23, 2013

dt.ToString("MMMM dd, yyyy");  

Date Time formats

Tilak
  • 30,108
  • 19
  • 83
  • 131
2

The DateTime.Date is just a DateTime with the time portion set to zero, or midnight. If you are trying to output it as a string, then you can use either ToShortDateString() or one of the many various custom formats.

date.ToString("MMMM dd,yyyy")

should give you the output you are looking for.

For information on DateTime.Date

http://msdn.microsoft.com/en-us/library/system.datetime.date.aspx

For more information on format strings for DateTime

http://msdn.microsoft.com/en-us/library/az4se3k1.aspx

http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

pcreech
  • 334
  • 1
  • 9
1

Here are all the standard date formats.

http://msdn.microsoft.com/en-us/library/az4se3k1.aspx

Smeegs
  • 9,151
  • 5
  • 42
  • 78
1

Use the Date property:

OR

 var dateAndTime = DateTime.Now;
   dateAndTime.ToString("dd/MM/yyyy")

The date variable will contain the date, the time part will be 00:00:00.

Neel
  • 11,625
  • 3
  • 43
  • 61
1

You probably mean that after ToString the time is rendered along with the date. Use ToShortDateString method for example to get what you expect. There are many other possible ways to format a date as string, you can easily find them all here. The second format you mention can be achieved for example with:

date.ToString("MMMM dd,yyyy");

using this ToString overload.

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
1

If you want to retrieve date only string representation, you can use

dateTime.ToString("d")

format, according to MSDN

Dzmitry Martavoi
  • 6,867
  • 6
  • 38
  • 59
1

There are a few ways of doing this, the simplest and most effective, would be like so;

DateTime.Now.Date.ToString("MMMM dd,yyyy") //Return October 23,2013

See this URL and this URL for further reading on custom dates.

Hexie
  • 3,955
  • 6
  • 32
  • 55
0
// returns a date 'MM/dd/yyyy' string from a datetime string '2014-06-19 00:00:00.000'
public static string stripTime(string s) {
    string date = "";
    DateTime dt = DateTime.Parse(s);
    date = dt.Date.ToString("MM/dd/yyyy");
    return date;
}
// returns a date 'MM/dd/yyyy'string  from a datetime datetime 
public static string stripTime(DateTime d) {
    string date = "";
    date = d.Date.ToString("MM/dd/yyyy");
    return date;
}
spacemonkey
  • 133
  • 1
  • 11
0

Surely this would work:

var dateAndTime = Convert.ToDateTime(certificate.GetEffectiveDateString());
var date = new DateTime(dateTime.Year, dateTime.Month, dateTime.Day);

I haven't tried myself however, but it should give the result you want. You could even create an Extension Method for it:

public static class DateExtension
{
    public DateTime ToDateOnly(this DateTime d)
    {
       return new DateTime(d.Year, d.Month, d.Day);
    }
}
Luke T O'Brien
  • 2,565
  • 3
  • 26
  • 38