C# 中的日期时间字符串问题

发布于 2024-08-06 01:02:39 字数 240 浏览 6 评论 0原文

假设我有以下代码将日期时间转换为字符串:

DateTime dt;
//...
string ds = dt.ToString("dd/MM/yyyy hh:mm")

如果 dt 是 15/02/2009 08:22,我想要字符串是 15/02/2009 08:22AM 如果dt是15/02/2009 20:22,我想要的字符串是15/02/2009 08:22PM

如何实现呢?

Suppose I have following code to convert datetime to string:

DateTime dt;
//...
string ds = dt.ToString("dd/MM/yyyy hh:mm")

If the dt is 15/02/2009 08:22, I want to the string is 15/02/2009 08:22AM
If the dt is 15/02/2009 20:22, I want to the string is 15/02/2009 08:22PM

How to implement it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

記憶穿過時間隧道 2024-08-13 01:02:39

使用此:

string ds = dt.ToString("dd/MM/yyyy hh:mmtt")

以下是将 DateTime 转换为字符串的所有可用选项

use this:

string ds = dt.ToString("dd/MM/yyyy hh:mmtt")

Here are all of the available options for converting DateTime to string

最单纯的乌龟 2024-08-13 01:02:39

根据 DateTime.ToString 的文档,您需要的字符添加 are t,所以这应该可行:

string ds = dt.ToString("dd/MM/yyyy hh:mmtt")

一个“t”将为您提供“P”或“A”,两个将为您提供“PM”或“AM”。

请注意,根据您当前的 CultureInfo,您可能,或者可能不会,获取 AM/PM。

As per the documentation of DateTime.ToString, the characters you need to add are t's, so this should work:

string ds = dt.ToString("dd/MM/yyyy hh:mmtt")

One 't' would give you 'P' or 'A', and two will give you 'PM' or 'AM'.

Note that depending on your current CultureInfo, you might, or might not, get the AM/PM.

不气馁 2024-08-13 01:02:39

你应该使用小写“t”...

DateTime dt;
//...
string ds = dt.ToString("dd/MM/yyyy hh:mmtt")

you should use lowercase "t"...

DateTime dt;
//...
string ds = dt.ToString("dd/MM/yyyy hh:mmtt")
坠似风落 2024-08-13 01:02:39
DateTime dt;

string ds = dt.ToString("dd/MM/yyyy hh:mmtt");
DateTime dt;

string ds = dt.ToString("dd/MM/yyyy hh:mmtt");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文