New Developer
New Developer

Reputation: 3305

how to display plain string as date mvc

In my mvc application I have stored my date in string format. eg: 10212013

But I would like to display that sting as a date in my UI eg: 10/21/2013

How to archive this?? Is it possible to user DisplayFormat for this purpose.

EDIT
This is what I need to do.

@Html.DisplayFor(model => DailyTransaction.MyDate)  

Datatype of MyDate is string so this will display as 10212013

But I need to display this as 10/21/2013

can I use string format inside the view of MVC project

I tried to use the following and that is also did not work for me.

[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public string MyDate{ get; set; }

Upvotes: 0

Views: 565

Answers (5)

Kami
Kami

Reputation: 19447

You can create custom templates for various data types that are specific to your requirements.

To create a custom template create a new file in /Views/Shared/DisplayTemplates - Call it StringToDate.cshtml. The name is important and is used in the function call below.

Next, in the file add the code to convert the string to a date format as you require. The simplest solution is to insert the / characters.

@model string
if (Model != null && Model.Length == 8)
{
  @(Model.Insert(2,"/").Insert(5,"/"))
} else {
  @("Err")
}

Add the above to the file and save it. The else part simply outputs an error if the basic format checks fail.

Next in your view, where ever you need to display this value. Simply use

@Html.DisplayFor(model => model.StringDate, "StringToDate")

Upvotes: 0

Mike Norgate
Mike Norgate

Reputation: 2431

Try creating a custom DisplayFormatAttribute that will format the string in the correct format. See this How to make configurable DisplayFormat attribute

EDIT: After looking at your question again you could look at using an editor template to display the data: See this: http://blogs.msdn.com/b/nunos/archive/2010/02/08/quick-tips-about-asp-net-mvc-editor-templates.aspx

Upvotes: 1

Kaf
Kaf

Reputation: 33839

If you are sure about the saved format of the date you could use Substring() and Format() methods as below:

string.Format("{0}/{1}/{2}", myStringDate.Substring(0,2),
                             myStringDate.Substring(2,2),
                             myStringDate.Substring(4))

You can apply this to your Class and here is a demo;

public class myModelClass
{
    private string _MyDate;
    public string MyDate
    {
        get {
            return string.Format("{0}/{1}/{2}", _MyDate.Substring(0,2),
                                     _MyDate.Substring(2,2),
                                     _MyDate.Substring(4)); }
        set { _MyDate = value; }
    }

}

Upvotes: 0

Fazil Mir
Fazil Mir

Reputation: 833

yeah you just have to use insert function. here is the example

string dateValue = "10212013";          
string dateFormat = x.Insert(2, "/");
dateFormat = dateFormat.Insert(5, "/");

thats all...

Upvotes: 0

Soner Gönül
Soner Gönül

Reputation: 98858

Use DateTime.ToString() like;

YourDateTime.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture);

For example;

DateTime d = DateTime.Today;
Console.WriteLine(d.ToString("MM/yy/yyyy", CultureInfo.InvariantCulture));

Output will be;

10/21/2013

Here a DEMO.

As an alternative, you can use DisplayFormatAttribute.DataFormatString like;

[DisplayFormat(DataFormatString="{0:MM/yy/yyyy}")]

Upvotes: 0

Related Questions