Solved

Conditional Formatting of DateTime MVC

Posted on 2013-06-09
12
602 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
  • 6
  • 5
12 Comments
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
<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
Comment Utility
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
Comment Utility
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
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
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
Comment Utility
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
Comment Utility
what is LastClosed type?
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:RBS
Comment Utility
LastClosed type is DateTime
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Great, thanks - worked like a charm!  Thanks for your persistence sedgewick!

RBS
0
 

Author Comment

by:RBS
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now