Solved

Display Format Error

Posted on 2014-01-20
2
331 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 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

914 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

19 Experts available now in Live!

Get 1:1 Help Now