简体   繁体   English

使用 C# 在 DateTime.ToString 中添加文本?

[英]Add text inside DateTime.ToString with C#?

I want to add a 'at' inside my DateTime format string but every time I try it messes it up?我想在我的 DateTime 格式字符串中添加一个“at”,但每次我尝试都会把它搞砸?

origin.ToString("dd/MM/yyyy at HH:mm:ss");

When its this it works fine... but looks a bit ugly.当它这个它工作正常......但看起来有点难看。

origin.ToString("dd/MM/yyyy HH:mm:ss");

添加转义序列:

origin.ToString("dd/MM/yyyy 'at' HH:mm:ss")

t is a custom format specifier and will be replaced with A or P based on the time of day. t是自定义格式说明符,将根据一天中的时间替换为AP

t The first character of the AM/PM designator. t AM/PM 指示符的第一个字符。

You can prevent that from happening by escaping it with \\ .您可以通过使用\\对其进行转义来防止这种情况发生。

origin.ToString("dd/MM/yyyy a\t HH:mm:ss");

You may add the following code:您可以添加以下代码:

System.DateTime.Now.Date.ToString() + "at" + System.DateTime.Now.TimeOfDay.ToString()

and then use this in your code.然后在您的代码中使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM