Solved

Conditional Formatting of DateTime MVC

Posted on 2013-06-09
12
625 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
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

810 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