开发者

Possible to pass format specifier for an argument as another argument to String.Format?

开发者 https://www.devze.com 2022-12-13 02:39 出处:网络
For instance, let\'s say I have the DateTime format-string in a string variable, is there any syntax or method in .NET that would let me do the equivalent of this invalid code:

For instance, let's say I have the DateTime format-string in a string variable, is there any syntax or method in .NET that would let me do the equivalent of this invalid code:

String line = String.Format("{0:{1}}", DateTime.Now, dateTimeFormat);
                   开发者_StackOverflow中文版             ^^^                        ^
                                 |                         |
                                 +-- this would use this --+


I think this syntax overview pretty much excludes dynamic parameters.

You will have to use a 2 stage system somehow, either pre-formatting your Date or by generating a format string. But I think both will be worse than the problem.


What's wrong with just

string line = DateTime.Now.ToString(dateTimeFormat);

?

0

精彩评论

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