Solved

Conditional Formatting of DateTime MVC

Posted on 2013-06-09
12
613 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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

920 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

17 Experts available now in Live!

Get 1:1 Help Now