murday1983
murday1983

Reputation: 4016

Display a link when date equals today's

I have a table with various columns with one being of type DateTime and one being an action link. I want the link to be displayed only if the date equals today. I can't seem to find out any information on how to do this.

The link I want to display is:

@Html.ActionLink("Show Details", "Details", new { id = item.Id })

If the date in the DateTime column does not equal today, then the link shouldn't be displayed.

Is there a way to do this?

Upvotes: 1

Views: 79

Answers (2)

Maarten
Maarten

Reputation: 22945

You can compare on the Date property of a DateTime.

@if (item.MyDateTimeColumn.Date == DateTime.Now.Date) {
    // Your link
}

Upvotes: 2

Adween
Adween

Reputation: 2820

Time zones aside you could go

@if(item.DateField.ToShortDateString() == DateTime.Now.ToShortDateString())
{
    //pop stuff in here
}

EDIT

@Maarten is also correct you could just compare the date part! :D

Upvotes: 0

Related Questions