开发者

c# custom date format

开发者 https://www.devze.com 2023-03-09 18:53 出处:网络
I would like to have the word \"day(s)\" displayed in开发者_运维知识库 the output.I\'ve tried putting escape characters in front of just the \"-\", in front of all the characters and spaces in \"days\

I would like to have the word "day(s)" displayed in开发者_运维知识库 the output. I've tried putting escape characters in front of just the "-", in front of all the characters and spaces in "days". Any ideas or pointers.

This is the line I'm working with, totalTimeInRoom is a TimeSpan:

totalTimeInRoom.ToString(@"dd\.hh\:mm\:ss"); 

EDIT: Just tried this, no luck:

totalTimeInRoom.ToString(@"dd \"Days(s)\" \- hh\:mm\:ss");


As explained here, you can use single quote characters to embed a literal string in your custom date format:

totalTimeInRoom.ToString("dd' Day(s) 'hh':'mm':'ss");


I've struggled with this before and always ended up "cheating" using String.Format:

// extend with minutes, seconds etc. if needed
var text = String.Format ("{0} day(s), {1} hour(s)", totalTimeInRoom.Days, totalTimeInRoom.Hours);

Of course, you could make it much more advanced and only show 'days' if there's actually 1 or more days in the timespan and also instead of unconditionally using "day(s)" you can dynamically append an 's' when the number of days is more than 1.

0

精彩评论

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