?
Solved

Conditional Formatting of DateTime MVC

Posted on 2013-06-09
12
Medium Priority
?
689 Views
Last Modified: 2013-06-26
Hi I have a DateTime variable - LastClosed that I would like to format in a time or date format depending on whether LastClosed is today:

Following is from my code:

<td>
                        @if (item.LastClosed == DateTime.Today)
                        {
                            //Format as time e.g.  09:44 am
                            @Html.DisplayFor(modelItem => item.LastClosed)
                        }
                        else
                        {
                            //Format as date i.e.  Jun 14, 2013
                            @Html.DisplayFor(modelItem => item.LastClosed)
                        }
                        
                    </td>

Open in new window


Any help dong this greatly appreciated.

RBS
0
Comment
Question by:RBS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
12 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 39233872
<td>
                        @if (item.LastClosed == DateTime.Today)
                        {
                            //Format as time e.g.  09:44 am
                            @Html.DisplayFor(modelItem =>LastClosed.ToString("hh:mm tt"))
                        }
                        else
                        {
                            //Format as date i.e.  Jun 14, 2013
                            @Html.DisplayFor(modelItem => item.LastClosed.ToString("MMM dd, yyyyy"))
                        }
                        
                    </td>

Open in new window

0
 
LVL 27

Expert Comment

by:Sammy Ageil
ID: 39236022
The best approach is to use a template
use sedgwick solution to get the logic correct and create a template based on that.
http://stackoverflow.com/questions/6001654/how-to-render-a-datetime-in-a-specific-format-in-asp-net-mvc-3
0
 

Author Comment

by:RBS
ID: 39276456
Hi:

When I try the approach by sedgewick, I get the error returned,:

Method 'ToString' has 0 parameters but is invoked with 1 argument.

I looked at the posting by sammy1971 but am not clear how this fits in with what I am trying to do.  

Thanks for your efforts to date - I had to drop this to go onto some other work but would like to return now.

Any suggestions greatly appreciated.

RBS
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 42

Expert Comment

by:sedgwick
ID: 39277007
I made a mistake try this:
<td>
                        @if (item.LastClosed == DateTime.Today)
                        {
                            //Format as time e.g.  09:44 am
                            @Html.DisplayFor(modelItem =>item.LastClosed.ToString("hh:mm tt"))
                        }
                        else
                        {
                            //Format as date i.e.  Jun 14, 2013
                            @Html.DisplayFor(modelItem => item.LastClosed.ToString("MMM dd, yyyyy"))
                        }
                       
                    </td>
0
 

Author Comment

by:RBS
ID: 39277055
Adding the item fixed a mistake but did not address the main error that .ToString() does not take any parameters.

RBS
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39277122
what is LastClosed type?
0
 

Author Comment

by:RBS
ID: 39277124
LastClosed type is DateTime
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39277137
try:
                 @if (item.LastClosed == DateTime.Today)
                        {
                            //Format as time e.g.  09:44 am
                            @Html.DisplayFor(modelItem =>((DateTime)item.LastClosed).ToString("hh:mm tt"))
                        }
                        else
                        {
                            //Format as date i.e.  Jun 14, 2013
                            @Html.DisplayFor(modelItem => ((DateTime)item.LastClosed).ToString("MMM dd, yyyyy"))
                        }
                        
                    </td>

Open in new window

0
 

Author Comment

by:RBS
ID: 39277179
Thanks

The item.LastClosed and DateTime.Today need to be be converted to just dates for the equality to work.  I am working on that.

However, that aside, when I try your latest suggestion, get following error:

Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 2000 total points
ID: 39277299
different approach:
        <td>
                 @if (item.LastClosed == DateTime.Today)
                        {
                            //Format as time e.g.  09:44 am
@String.Format("{0:hh:mm tt}", item.LastClosed)
                        }
                        else
                        {
                            //Format as date i.e.  Jun 14, 2013
@String.Format("{0:MMM dd, yyyy}", item.LastClosed)
                        }                        
                    </td>

Open in new window

0
 

Author Closing Comment

by:RBS
ID: 39278118
Great, thanks - worked like a charm!  Thanks for your persistence sedgewick!

RBS
0
 

Author Comment

by:RBS
ID: 39278313
FYI - I changed my logic for showing whether the dates were equal as a criterion for showing the time only to whether the event occurred in the last 24 hours.  Here's my final code - thanks again sedgwick.

 @if (item.LastClosed >= DateTime.Today.AddDays(-1))
                        {
                            //Format as time i.e.  09:44 am
                            @String.Format("{0:hh:mm tt}", item.LastClosed)
                        }
                        else 
                        {
                            
                            //Format as date i.e.  Jun 14, 2013
                            @Html.DisplayFor(modelItem => item.LastClosed)
                        }

Open in new window

0

Featured Post

DFW AZURE MEETUP TONIGHT FRI 6PM

We will be discussing what Azure Stack is, how does it fit into the suit of offerings that Azure has currently, and where can it fit into your organizations technology stack. We will also be discussing limitations of the platform while covering various applicable scenarios.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question