Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
can any one help , how to format time, i have UInt32 Minutes(800), this number i want to convert as Days, Hours, Mins, Secs .and binding to XAML DataGrid.
i have three columns, in that this Time is also one column.

i have ViewModel
C#
private UInt32 _time;
public UInt Time
{
get
{return _time;}
set{_time value;}
}

output should be like 08D:13H:20M:04S

thanks in advance
Posted
Updated 23-Aug-12 7:34am
v2
Comments
[no name] 23-Aug-12 9:23am    
I am not sure what you are asking here. Converting a UInt to time? Or how to format a string using String.Format()?
getanswer 23-Aug-12 10:16am    
i need somethin like output

private UInt32 _time=800;
public UInt32 Time
{

get{
TimeSpan t = TimeSpan.FromMinutes(_time);
_time= string.Format("{0:D2}H:{1:D2}M:{2:D2}S",
t.Hours,
t.Minutes,
t.Seconds);
return _time;
}

}

Bind to DataGridTextColumn in XAML, output should be like 08D:13H:20M:04S

thanks
Clifford Nelson 23-Aug-12 14:25pm    
Updated my solution with the letters

Solution 1 "ddD:hhH:mmM:ssS" is invalid and will not work. There is some default formatting that might do it for you, but if that will not work, then you can use some of the properties to create whatever format you want. A sample is as follows:

C#
public string Duration
{
    get
    {
        TimeSpan timeSpan = TimeSpan.FromSeconds(_seconds);
        return String.Format("{0:00}D:{1:00}H:{2:00}M:{3:00}S", timeSpan.Days, timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds);
    }
}
private int _seconds;


See http://msdn.microsoft.com/en-us/library/ee372286.aspx[^] for details. Unfortunately, there is no way to simplify custom formating with TimeSpans.

I have updated the answer with the letters.
 
Share this answer
 
v2
Comments
Matt T Heffron 23-Aug-12 14:42pm    
Actually, custom formatting with TimeSpan is not difficult, it just needs appropriate attention to detail (like quoting literals).
Clifford Nelson 23-Aug-12 15:01pm    
I found the reference: http://msdn.microsoft.com/en-us/library/ee372287.aspx. It is no where near as easy as with time formatting. So using timeSpan.ToString(@"dd\D\:hh\H\:mm\M\:ss\S") works
Kenneth Haugland 23-Aug-12 15:09pm    
http://msdn.microsoft.com/en-us/library/ee372287.aspx
Matt T Heffron 23-Aug-12 15:11pm    
Kenneth was just a bit quicker.
It requires .Net 4 or later, or Silverlight.
TimeSpan.FromSeconds is what you need to turn this in to a time span on which you can call a formatted ToString in order to get the format you need. You need to add another gettable property that returns it.

C#
public string TheTime
{
   get
   {
       TimeSpan ts = TimeSpan.FromSeconds(_time);
       return ts.ToString("ddD:hhH:mmM:ssS");
   }
}
 
Share this answer
 
Comments
Matt T Heffron 23-Aug-12 14:40pm    
The literal characters need to be escaped:
"dd'D:'hh'H:'mm'M:'ss'S'"
OR
@"dd\D\:hh\H\:mm\M\:ss\S"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900