Solved

Conditional Formatting of DateTime MVC

Posted on 2013-06-09
12
671 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
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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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 500 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
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 …

724 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