Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Display Format Error

Posted on 2014-01-20
2
Medium Priority
?
351 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
2 Comments
 
LVL 23

Accepted Solution

by:
Ioannis Paraskevopoulos earned 1500 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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 …
Loops Section Overview
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

876 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