Link to home
Start Free TrialLog in
Avatar of maqskywalker
maqskywalker

asked on

formatting a datetime field in a razor DisplayFor tag

Hi experts,

I'm using ASP.NET MVC 5 with C#

On my view I have a DisplayFor displaying a datetime field.

My date time field is stored in sql server as datetime format

This is my current razor for this label on my page.

@Html.DisplayFor(m => m.MyTestDate)

With this syntax the browser displays this on my page.

07/17/2017 07:30:00 AM


Question 1: How do I format the razor DisplayFor tag to display only the date like this?:

07/17/2017

Question 2: How do I format the razor DisplayFor tag to display only the time like this?:

07:30:00 AM
Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

Pls try ..

SELECT (CONVERT,yourcolumn,101)

SELECT CAST( yourcolumn AS TIME )
try

datetime1.ToString("MM/dd/yyyy")
datetime2.ToString("hh:mi:ss t")

Open in new window


Custom Date and Time Format Strings
https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings
SOLUTION
Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Dirk Strauss
Dirk Strauss
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of maqskywalker
maqskywalker

ASKER

thanks