C#+格式的TimeSpan [英] C# + Format TimeSpan

查看:332
本文介绍了C#+格式的TimeSpan的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在[分钟]:[秒]的格式来格式化时间跨度元素。在此格式中,2分8秒将看起来像02:08。我已经试过各种具有的String.Format和方法的ToString的选择,但我得到出现FormatException。这是我目前想:

I am trying to format a TimeSpan element in the format of "[minutes]:[seconds]". In this format, 2 minutes and 8 seconds would look like "02:08". I have tried a variety of options with String.Format and the ToString methods, but I get a FormatException. This is what I'm currently trying:

DateTime startTime = DateTime.Now;
// Do Stuff
TimeSpan duration = DateTime.Now.Subtract(startTime);

Console.WriteLine("[paragraph of information] Total Duration: " + duration.ToString("mm:ss"));



我在做什么错了?我如何使用我想要的格式格式化一个时间跨度元素?

What am I doing wrong? How do I format a TimeSpan element using my desired format?

感谢您

推荐答案

试试这个:

Console.WriteLine("{0:D2}:{1:D2}", duration.Minutes, duration.Seconds);

这篇关于C#+格式的TimeSpan的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆