Solved

Display Format Error

Posted on 2014-01-20
2
337 Views
Last Modified: 2014-01-21
Hi,

I have the below class with display format attribute for TimeSpan

where it has to display only hours and minutes

public partial class abc
    {
        [DataType(DataType.Time)]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = @"{0:hh\:mm}")]
        public System.TimeSpan getTimes
        {
            get;
            set;
        }      
     
    }


and now i am calling it from button click event as below

 private void button1_Click(object sender, EventArgs e)
        {
            System.TimeSpan d = new System.TimeSpan();
            abc  obj = new abc();
            obj.getTimes = d;
            MessageBox.Show(obj.getTimes.ToString());
        }

 here after executing it is showing the time with hours , minutes and seconds

 but i want to display it with only hours and minutes , i don't want seconds

 could you please suggest me what is the wrong in above code, I am using mvc4 Razor

                   Many thanks
0
Comment
Question by:praveen1981
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 23

Accepted Solution

by:
Ioannis Paraskevopoulos earned 500 total points
ID: 39796303
Hi,

DataFormatingString is for when you use the html helpers, like

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

Open in new window


In your case, since you are using this in a messagebox, you should format it using the ToString:

MessageBox.Show(obj.getTimes.ToString(@"hh\:mm"));

Open in new window


Giannis
0
 

Author Closing Comment

by:praveen1981
ID: 39796363
Thanks.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 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