连接格式化字符串时出现问题
currTime = DateTime.Now.ToString("u");
上面的代码首先将现在的时间格式化为yyyy-mm-dd hh-mm-ss
(字母数字)。
我想删除字母数字字符,所以我使用了
currTime = currTime.Substring(0, currTime.Length - 1);
然后在末尾添加了“.000”,
currTime = currTime + ".000";
但是,这会删除格式,并且在显示时显示标准的 dd-mm-yyyy 格式。所以我的问题分为两部分:
那么...
如何将 DateTime.Now
格式化为 yyyy-mm-dd hh:mm:ss
而不带字母数字字符?
如何添加“.000”而不丢失我的格式?
currTime = DateTime.Now.ToString("u");
The code above first formats the time now to yyyy-mm-dd hh-mm-ss
(Alpha numerical).
I wanted to remove the alpha numerical character so I used
currTime = currTime.Substring(0, currTime.Length - 1);
I then added ".000" to the end using
currTime = currTime + ".000";
However, this then removes the formatting and when displayed shows the standard dd-mm-yyyy
format. So my question is in two parts:
So...
How do I format DateTime.Now
to yyyy-mm-dd hh:mm:ss
without the alpha numerical character?
And how to add ".000" without losing my format?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
DateTime.Now.ToString(@"yyyy\-MM\-dd hh\-mm\-ss.000")
不过你确定不想要DateTime.Now.ToString (@"yyyy\-MM\-dd hh\:mm\:ss.000")
?DateTime.Now.ToString(@"yyyy\-MM\-dd hh\-mm\-ss.000")
though are you sure you don't wantDateTime.Now.ToString(@"yyyy\-MM\-dd hh\:mm\:ss.000")
?恐怕我根本不相信你。简单的字符串连接不会开始扰乱字符串的其余部分。
这是一个简短但完整的示例,显示它没有发生变化:
输出:
我不认为这是提出该格式的最佳方式,但它的行为肯定不是您声称的那样。您能否提出一个类似的简短但完整的程序来支持您的断言“然后删除格式并将其显示为标准 dd-mm-yyyy 格式”?我的猜测是,您所看到的并不是您真正认为所看到的,但是如果没有完整的程序,就很难判断到底发生了什么。
I'm afraid I simply don't believe you. Simple string concatenation isn't going to start messing around with the rest of the string.
Here's a short but complete example showing it not changing:
Output:
I don't think that's the best way of coming up with that format, but it certainly isn't behaving the way you claim. Can you come up with a similar short but complete program which backs up your assertion that "this then removes the formatting and display it as the standard dd-mm-yyyy format"? My guess is that you're not seeing what you actually think you're seeing, but without a complete program it's hard to tell what's really going on.
像这样的东西应该可以解决问题,只需稍加修改即可附加实际秒数,而不是硬编码的
.000
:Something like this ought to do the trick, with the slight alteration that appends the actual fraction of seconds rather than a hard-coded
.000
: