如何从日期时间获取 AM/PM 值?

发布于 2024-12-11 11:07:46 字数 648 浏览 0 评论 0原文

有问题的代码如下:

public static string ChangePersianDate(DateTime dateTime)
{
    System.Globalization.GregorianCalendar PC = new System.Globalization.GregorianCalendar();
    PC.CalendarType = System.Globalization.GregorianCalendarTypes.USEnglish;
    return
    PC.GetYear(dateTime).ToString()
    + "/"
    + PC.GetMonth(dateTime).ToString()
    + "/"
    + PC.GetDayOfMonth(dateTime).ToString()
    + ""
    + PC.GetHour(dateTime).ToString()
    + ":"
    + PC.GetMinute(dateTime).ToString()
    + ":"
    + PC.GetSecond(dateTime).ToString()
    + " "
    ????????????????
}

如何从 dateTime 值获取 AM/PM?

The code in question is below:

public static string ChangePersianDate(DateTime dateTime)
{
    System.Globalization.GregorianCalendar PC = new System.Globalization.GregorianCalendar();
    PC.CalendarType = System.Globalization.GregorianCalendarTypes.USEnglish;
    return
    PC.GetYear(dateTime).ToString()
    + "/"
    + PC.GetMonth(dateTime).ToString()
    + "/"
    + PC.GetDayOfMonth(dateTime).ToString()
    + ""
    + PC.GetHour(dateTime).ToString()
    + ":"
    + PC.GetMinute(dateTime).ToString()
    + ":"
    + PC.GetSecond(dateTime).ToString()
    + " "
    ????????????????
}

how can I get the AM/PM from the dateTime value?

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

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

发布评论

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

评论(15

芯好空 2024-12-18 11:07:46

怎么样:

dateTime.ToString("tt", CultureInfo.InvariantCulture);

How about:

dateTime.ToString("tt", CultureInfo.InvariantCulture);
小…红帽 2024-12-18 11:07:46
string.Format("{0:hh:mm:ss tt}", DateTime.Now)

这应该会给你时间的字符串值。 tt 应附加 am/pm。

您还可以查看相关主题:

您怎么样获取当前时间?

string.Format("{0:hh:mm:ss tt}", DateTime.Now)

This should give you the string value of the time. tt should append the am/pm.

You can also look at the related topic:

How do you get the current time of day?

满栀 2024-12-18 11:07:46

使用字符串格式非常简单

通过在 .ToString("") 上

  • 如果您使用“hh” ->>小时,使用 12 小时制,从 01 点到 12 点。

  • 如果使用“HH”->>小时,使用 24 小时制,从 00 到 23。

  • 如果添加“tt”->> Am/Pm 指示符。

例如,从 23:12 转换为 11:12 Pm :

DateTime d = new DateTime(1, 1, 1, 23, 12, 0);
var res = d.ToString("hh:mm tt"); // this shows 11:12 PM
var res2 = d.ToString("HH:mm");   // this shows 23:12

Console.WriteLine(res);
Console.WriteLine(res2);

Console.Read();

等一下,这并不是您需要关心的全部内容,其他的是系统文化,因为在使用其他语言的 Windows 上执行相同的代码
特别是使用不同的文化语言时,使用相同的代码将生成不同的结果,

设置为阿拉伯语言文化的窗口示例将显示如下:

// 23:12

重要意味着晚上(重要的第一个字母)。

在另一个系统文化中,取决于 Windows 区域和语言选项的设置,它将显示 // 23:12 du。

您可以在 Windows 控制面板上的 Windows 区域和语言 -> 下更改不同的格式。当前格式(组合框)并更改...应用它来重建(执行)您的应用程序并观看我正在谈论的内容。

如果>当前系统的文化未设置为英语,我可以强制谁在英语事件中显示 Am 和 Pm 单词?

只需添加两行即可轻松:->

第一步在代码顶部添加 using System.Globalization;

并将之前的代码修改为如下所示:

DateTime d = new DateTime(1, 1, 1, 23, 12, 0);
var res = d.ToString("HH:mm tt", CultureInfo.InvariantCulture); // this show  11:12 Pm

InvariantCulture =>使用默认的英文格式。

另一个问题我想让下午使用阿拉伯语或特定语言,即使我使用设置为英语(或其他语言)区域格式的窗口?

阿拉伯语示例的 Soution :

DateTime d = new DateTime(1, 1, 1, 23, 12, 0);
var res = d.ToString("HH:mm tt", CultureInfo.CreateSpecificCulture("ar-AE")); 

这将显示 // 23:12 -

如果我的系统设置为英语区域格式, 事件。
如果您想要其他语言格式,您可以更改“ar-AE”。有每种语言及其格式的列表。

示例:

ar          ar-SA       Arabic
ar-BH       ar-BH       Arabic (Bahrain)
ar-DZ       ar-DZ       Arabic (Algeria)
ar-EG       ar-EG       Arabic (Egypt)

big list...

如果您还有其他问题,请告诉我。

Very simple by using the string format

on .ToString("") :

  • if you use "hh" ->> The hour, using a 12-hour clock from 01 to 12.

  • if you use "HH" ->> The hour, using a 24-hour clock from 00 to 23.

  • if you add "tt" ->> The Am/Pm designator.

exemple converting from 23:12 to 11:12 Pm :

DateTime d = new DateTime(1, 1, 1, 23, 12, 0);
var res = d.ToString("hh:mm tt"); // this shows 11:12 PM
var res2 = d.ToString("HH:mm");   // this shows 23:12

Console.WriteLine(res);
Console.WriteLine(res2);

Console.Read();

wait a second that is not all you need to care about something else is the system Culture because the same code executed on windows with other langage
especialy with difrent culture langage will generate difrent result with the same code

exemple of windows set to Arabic langage culture will show like that :

// 23:12 م

م means Evening (first leter of مساء) .

in another system culture depend on what is set on the windows regional and language option, it will show // 23:12 du.

you can change between different format on windows control panel under windows regional and language -> current format (combobox) and change... apply it do a rebuild (execute) of your app and watch what iam talking about.

so who can I force showing Am and Pm Words in English event if the culture of the >current system isn't set to English ?

easy just by adding two lines : ->

the first step add using System.Globalization; on top of your code

and modifing the Previous code to be like this :

DateTime d = new DateTime(1, 1, 1, 23, 12, 0);
var res = d.ToString("HH:mm tt", CultureInfo.InvariantCulture); // this show  11:12 Pm

InvariantCulture => using default English Format.

another question I want to have the pm to be in Arabic or specific language, even if I use windows set to English (or other language) regional format?

Soution for Arabic Exemple :

DateTime d = new DateTime(1, 1, 1, 23, 12, 0);
var res = d.ToString("HH:mm tt", CultureInfo.CreateSpecificCulture("ar-AE")); 

this will show // 23:12 م

event if my system is set to an English region format.
you can change "ar-AE" if you want to another language format. there is a list of each language and its format.

exemples :

ar          ar-SA       Arabic
ar-BH       ar-BH       Arabic (Bahrain)
ar-DZ       ar-DZ       Arabic (Algeria)
ar-EG       ar-EG       Arabic (Egypt)

big list...

make me know if you have another question .

﹏雨一样淡蓝的深情 2024-12-18 11:07:46

DateTime 应始终位于“美国”(公历)日历内部。因此,如果您这样做,

var str = dateTime.ToString(@"yyyy/MM/dd hh:mm:ss tt", new CultureInfo("en-US"));

您应该可以用更少的行数获得您想要的东西。

The DateTime should always be internally in the "american" (Gregorian) calendar. So if you do

var str = dateTime.ToString(@"yyyy/MM/dd hh:mm:ss tt", new CultureInfo("en-US"));

you should get what you want in many less lines.

↘紸啶 2024-12-18 11:07:46

我知道这可能看起来已经很晚了..但是它可能会帮助那里的人

我想获得日期的 AM PM 部分,所以我使用了 Andy 的建议:

dateTime.ToString("tt");

我使用该部分构建了一个路径来保存我的文件。 。
我假设我会得到上午或下午,除此之外什么都没有!

但是,当我使用的 PC 的文化不是英语时..(在我的情况下是阿拉伯语)..我的应用程序失败了,因为格式“tt”返回了新的内容,而不是 AM 或 PM(Μ 或 õ)..

所以修复此问题是通过添加第二个参数来忽略文化,如下所示:

dateTime.ToString("tt", CultureInfo.InvariantCulture);

..
当然你必须添加:using System.Globalization;在你的文件顶部
我希望这会对某人有所帮助:)

I know this might seem to be extremely late.. however it may help someone out there

I wanted to get the AM PM part of the date, so I used what Andy advised:

dateTime.ToString("tt");

I used that part to construct a Path to save my files..
I built my assumptions that I will get either AM or PM and nothing else !!

however when I used a PC that its culture is not English ..( in my case ARABIC) .. my application failed becase the format "tt" returned something new not AM nor PM (م or ص)..

So the fix to this was to ignore the culture by adding the second argument as follow:

dateTime.ToString("tt", CultureInfo.InvariantCulture);

..
of course u have to add : using System.Globalization; on top of ur file
I hope that will help someone :)

拥醉 2024-12-18 11:07:46

这很简单

  Date someDate = new DateTime();
  string timeOfDay = someDate.ToString("hh:mm tt"); 
  // hh - shows hour and mm - shows minute - tt - shows AM or PM

its pretty simple

  Date someDate = new DateTime();
  string timeOfDay = someDate.ToString("hh:mm tt"); 
  // hh - shows hour and mm - shows minute - tt - shows AM or PM
时光无声 2024-12-18 11:07:46
+ PC.GetHour(datetime) > 11 ? "pm" : "am"

对于您的示例,但有更好的方法来格式化日期时间。

+ PC.GetHour(datetime) > 11 ? "pm" : "am"

For your example but there are better ways to format datetime.

残花月 2024-12-18 11:07:46

来自: http://www.csharp-examples.net/string-format-datetime/

string.Format("{0:t tt}", datetime);  // -> "P PM"  or "A AM"

From: http://www.csharp-examples.net/string-format-datetime/

string.Format("{0:t tt}", datetime);  // -> "P PM"  or "A AM"
娜些时光,永不杰束 2024-12-18 11:07:46

类似 bool isPM = GetHour() > 11..但如果您想将日期格式化为字符串,则不需要自己执行此操作。为此,请使用日期格式函数。

Something like bool isPM = GetHour() > 11. But if you want to format a date to a string, you shouldn't need to do this yourself. Use the date formatting functions for that.

夜夜流光相皎洁 2024-12-18 11:07:46

@Andy的回答是好的。但是如果你想显示没有 24 小时格式的时间,那么你可以像这样遵循

string dateTime = DateTime.Now.ToString("hh:mm:ss tt", CultureInfo.InvariantCulture);

之后,你应该得到像“10:35:20 PM”或“10:35:20 AM”这样的时间

@Andy's answer is Okay. But if you want to show time without 24 hours format then you may follow like this way

string dateTime = DateTime.Now.ToString("hh:mm:ss tt", CultureInfo.InvariantCulture);

After that, you should get time like as "10:35:20 PM" or "10:35:20 AM"

画▽骨i 2024-12-18 11:07:46

你可以用这种方式测试它的

Console.WriteLine(DateTime.Now.ToString("tt "));

输出将是这样的:

AM

或者

PM

You can test it in this way

Console.WriteLine(DateTime.Now.ToString("tt "));

The output will be like this:

AM

or

PM
那伤。 2024-12-18 11:07:46

如果你想在 LongDateString Date 中添加时间,你可以这样格式化:

    DateTime date = DateTime.Now;
    string formattedDate = date.ToLongDateString(); 
    string formattedTime = date.ToShortTimeString();
    Label1.Text = "New Formatted Date: " + formattedDate + " " + formattedTime;

输出:

New Formatted Date: Monday, January 1, 1900 8:53 PM 

If you want to add time to LongDateString Date, you can format it this way:

    DateTime date = DateTime.Now;
    string formattedDate = date.ToLongDateString(); 
    string formattedTime = date.ToShortTimeString();
    Label1.Text = "New Formatted Date: " + formattedDate + " " + formattedTime;

Output:

New Formatted Date: Monday, January 1, 1900 8:53 PM 
森林很绿却致人迷途 2024-12-18 11:07:46

另一种选择是更改用于获得正确格式的默认区域性。优点是新格式将在整个代码中使用。

CultureInfo.CurrentCulture = new CultureInfo("Fr-fr");
Console.WriteLine(DateTime.Now.ToString("t")); // 14:08

var customCulture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
customCulture.DateTimeFormat.AMDesignator = "am";
customCulture.DateTimeFormat.PMDesignator = "pm";
customCulture.DateTimeFormat.ShortTimePattern = "HH:mm tt";
CultureInfo.CurrentCulture = customCulture;

Console.WriteLine(DateTime.Now.ToString("t")); // 14:08 pm

Another option is to change the default culture used to get the right formatting. The advantage is that the new format will be used throughout the code.

CultureInfo.CurrentCulture = new CultureInfo("Fr-fr");
Console.WriteLine(DateTime.Now.ToString("t")); // 14:08

var customCulture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
customCulture.DateTimeFormat.AMDesignator = "am";
customCulture.DateTimeFormat.PMDesignator = "pm";
customCulture.DateTimeFormat.ShortTimePattern = "HH:mm tt";
CultureInfo.CurrentCulture = customCulture;

Console.WriteLine(DateTime.Now.ToString("t")); // 14:08 pm
嘿嘿嘿 2024-12-18 11:07:46

这是一种更简单的方法,您可以编写时间格式 (hh:mm:ss tt) 并根据需要单独显示它们。

string time = DateTime.Now.Hour.ToString("00") + ":" + DateTime.Now.Minute.ToString("00") + ":" + DateTime.Now.Second.ToString("00") + DateTime.Now.ToString(" tt");

或者只是简单地:

 DateTime.Now.ToString("hh:mm:ss tt")

Here is an easier way you can write the time format (hh:mm:ss tt) and display them separately if you wish.

string time = DateTime.Now.Hour.ToString("00") + ":" + DateTime.Now.Minute.ToString("00") + ":" + DateTime.Now.Second.ToString("00") + DateTime.Now.ToString(" tt");

or just simply:

 DateTime.Now.ToString("hh:mm:ss tt")
花开浅夏 2024-12-18 11:07:46
string AM_PM = string.Format("{0:hh:mm:ss tt}", DateTime.Now).Split(new char[]{' '})[1];
string AM_PM = string.Format("{0:hh:mm:ss tt}", DateTime.Now).Split(new char[]{' '})[1];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文