Format personnalisé Timespan avec String.Format
Question
Je veux formater le Timespan avoir le format comme celui-ci 49 h 34 mn 20 s
J'ai utilisé le format de chaîne ci-dessous:
String.Format("{0:00}:{1:00}:{2:00}", theTimeSpan.TotalHours, theTimeSpan.Minutes, theTimeSpan.Seconds)
Il formate le Timespan à ce format 49:34:20. Comment puis-je ajouter h mn sec au-dessus String.Format? ou il y a un autre moyen facile? Merci.
La solution
C'est simple:
String.Format("{0:00} hr {1:00} mn {2:00} sec ", _
Math.Truncate(theTimeSpan.TotalHours), _
theTimeSpan.Minutes, theTimeSpan.Seconds)
Il vaut la peine de se familiariser avec comment fonctionne le formatage de chaîne dans .NET -. il est un sujet important
Il est regrettable que TimeSpan
ne supporte pas les chaînes de format personnalisé au moment - mais il ne sera que de 4,0 .NET !
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow