How can I display the Date portion only of DateTime values?

I've got this class:

public class ScheduledReports
{
    public string Unit { get; set; }
    public string ReportName { get; set; }
    public DateTime NextExecutionCalcd { get; set; }
    public string AllEmailAddresses { get; set; }
    public DateTime DataRangeBeginDateCalcd { get; set; }
    public DateTime DataRangeEndDateCalcd { get; set; }
}

The underlying affiliated SQL Server table uses DateTime fields.

I populate instances of the class like so:

while (sqlReader.Read())
{
    ScheduledReports sr = new ScheduledReports();
    . . . // Other class member assignments elided for brevity
    sr.DataRangeBeginDateCalcd = GetNextDataRangeBeginDateAfterQueued(unit, rptName, Convert.ToDateTime(sqlReader["NextExecutionsBeginDateArg"]).Date); 
    sr.DataRangeEndDateCalcd = GetNextDataRangeEndDateAfterQueued(unit, rptName, Convert.ToDateTime(sqlReader["NextExecutionsEndDateArg"]).Date); 
    scheduledRpts.Add(sr);
}

As you can see, I'm using "Convert.ToDateTime(bla).Date" instead of simply "Convert.ToDateTime(bla)" in hopes of thus truncating the time portion away, but it doesnt' work; I assign what's returned from the method whose snippet is shown above thus:

private void LoadScheduledTab()
{
    List<ScheduledReports> scheduledRpts = RoboReporterSQL.GetScheduledReports();
    dataGridViewScheduled.DataSource = scheduledRpts;
}

...yet the grid shows the DateTime values wiht the time values, which are useless to me; I just want the Date and nothing but the date.

How can I convince the the display to cease from divulging TMI?

UPDATE

I tried to implement user 1666620's answer like so (after adding System.ComponentModel.DataAnnotations) as a reference and resolving "DisplayFormat"):

public class ScheduledReports
{
    public string Unit { get; set; }
    public string ReportName { get; set; }
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MMM/yyyy}")]
    public DateTime NextExecutionCalcd { get; set; }
    public string AllEmailAddresses { get; set; }
    public DateTime DataRangeBeginDateCalcd { get; set; }
    public DateTime DataRangeEndDateCalcd { get; set; }
}

...but it still fails; it compiles, but still displays the values in the DataGridView with both the date and the time.

Upvotes: 0

Views: 149

Answers (4)

I needed to move the call to ".Date" over past the last paren, so instead of this:

sr.DataRangeBeginDateCalcd = GetNextDataRangeBeginDateAfterQueued(unit, rptName, Convert.ToDateTime(sqlReader["NextExecutionsBeginDateArg"]).Date);
sr.DataRangeEndDateCalcd = GetNextDataRangeEndDateAfterQueued(unit, rptName, Convert.ToDateTime(sqlReader["NextExecutionsEndDateArg"]).Date);

...it's this:

sr.DataRangeBeginDateCalcd = GetNextDataRangeBeginDateAfterQueued(unit, rptName, Convert.ToDateTime(sqlReader["NextExecutionsBeginDateArg"])).Date;
sr.DataRangeEndDateCalcd = GetNextDataRangeEndDateAfterQueued(unit, rptName, Convert.ToDateTime(sqlReader["NextExecutionsEndDateArg"])).Date;

Upvotes: 1

Tamwe
Tamwe

Reputation: 359

I had the same problem. I don't know if it's the best solution, but I cut off the time-property with regex like this:

Regex.Match(Convert.ToDateTime("01.01.2014 00:00:00").Date.ToString(), @"\d{2}\.\d{2}\.\d{4}").ToString();

This line of code returns only the date (in this case 01.01.2014) as a string.

Regex.Match() compares a string with a pattern and returns a match: in this case it searches for two digits(\d{2}), a point(.), another two digits(\d{2} another point (.) and 4 digits (\d{4}).

To use Regex you need the using-directive using System.Text.RegularExpressions;

Upvotes: 1

MichaC
MichaC

Reputation: 13381

The Date property is still of type DateTime which always has the time properties and therefore your UI will pick it up I guess.

To display only the date, use .ToString("d")

Upvotes: 3

user1666620
user1666620

Reputation: 4808

in your class, use the DisplayFormat data annotation.

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString="{0:dd/MMM/yyyy}")]
public DateTime NextExecutionCalcd { get; set; }

https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.displayformatattribute.dataformatstring(v=vs.110).aspx

Upvotes: 2

Related Questions