开发者

Data Time Format(Duration)

I have a query like i want to show th开发者_StackOverflowe time information in custom format like(1min 06sec) from date .I have a filed Duration in database and when i am binding my data control then in item i want to display in above format(1min 06 sec),so is it possible?


Checkout this documentation - http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

But, assuming you have a DateTime object, something like this should do the trick:

var test1 = DateTime.UtcNow.ToString("m'min 's'sec'");

Or for a TimeSpan:

var test2 = TimeSpan.FromSeconds(123).ToString("m'min 's'sec'");

You can easily add in hours/days/etc. depending on the exact format you want. If your object isn't a DateTime or TimeSpan object, you will have to do something custom.


You can use System.TimeSpan structure. It represents time interval. MSDN


Given you DateTime variable, you could do the following:

  var rr = dt1.ToString("mm'min 'ss'sec'");

Or if you have a TimeSpan:

You'll need to use a TimeSpan for this. A simplistic approach is the following:

  var ts = new TimeSpan(0, 2, 30);
  var result = ts.Minutes.ToString() + "min " + ts.Seconds.ToString() + "sec";

In this example, I've set the TimeSpan variable to 2 minutes and 30 seconds.

Or if you have two dates: If you have two dates you can do a diff and get the timespan and from there use the code I've shown above:

  var dt1 = new DateTime(2011, 01, 01, 12, 01, 00);
  var dt2 = new DateTime(2011, 01, 01, 12, 03, 30);
  var diffTimeSpan = dt2.Subtract(dt1);
  var r = diffTimeSpan.Minutes.ToString() + "min " + diffTimeSpan.Seconds.ToString() + "sec";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜