Problem
Here’s the code in question:
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()
+ " "
????????????????
}
What is the best way to extract the AM/PM from a dateTime value?
Asked by SilverLight
Solution #1
How about:
dateTime.ToString("tt", CultureInfo.InvariantCulture);
Answered by Andy
Solution #2
string.Format("{0:hh:mm:ss tt}", DateTime.Now)
This should give you the time’s string value. The am/pm should be appended by tt.
You might also be interested in the following topic:
How do you find out what time it is right now?
Answered by XikiryoX
Solution #3
Internally, the DateTime should always use the “American” (Gregorian) calendar. As a result, if you do
var str = dateTime.ToString(@"yyyy/MM/dd hh:mm:ss tt", new CultureInfo("en-US"));
You should be able to get what you want in a lot less lines.
Answered by xanatos
Solution #4
Using the string format makes it very straightforward.
on .ToString(“”) :
Converting from 23:12 to 11:12 p.m., for example:
DateTime d = new DateTime(1, 1, 1, 23, 12, 0);
var res = d.ToString("hh:mm tt"); // this show 11:12 Pm
var res2 = d.ToString("HH:mm"); // this show 23:12
Console.WriteLine(res);
Console.WriteLine(res2);
Console.Read();
pause for a moment That is not all; you must also consider the system culture, because the same code executed on Windows with different languages, particularly with different cultures, may produce different results.
The following is an example of a window set to the Arabic language culture:
// 23:12 م
‘Evening’ is the initial letter of the word ‘evening’.
It will show / 23:12 du on another system, depending on what is specified on the windows regional and language options.
On the Windows Control Panel, go to Windows Regional and Language -> Current Format (combobox) and adjust… Apply that, rebuild (run) your program, and pay attention to what I’m saying.
simple as adding two lines: ->
The first step is to include System. On top of your code, there’s globalisation.
as well as changing the previous code to look 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
Default English Format is used by InvariantCulture.
a different query Even if I use windows configured to English (or other language) regional format, I want the pm to be in Arabic or a certain language.
Exercising Arabic Exercising Arabic Exercising Arabic Exercising Arabic Exercis
DateTime d = new DateTime(1, 1, 1, 23, 12, 0);
var res = d.ToString("HH:mm tt", CultureInfo.CreateSpecificCulture("ar-AE"));
This will be shown / 23:12
event If my system’s region format is set to English. If you want to use a different language format, change “ar-AE.” Each language is listed along with 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...
Please let me know if you have any other questions.
Answered by Bilal
Solution #5
Although this may appear to be terribly late, it may be of use to someone out there.
I needed the AM/PM portion of the date, so I followed Andy’s advice:
dateTime.ToString("tt");
I utilized that section to create a Path where I could save my stuff. I constructed my assumptions around the fact that I’ll only get AM or PM and nothing else!!
When I used a PC with a culture other than English (in my case, ARABIC), however, my application failed since the format “tt” returned something different than AM or PM (or).
So the solution was to ignore the culture by adding the following second argument:
dateTime.ToString("tt", CultureInfo.InvariantCulture);
Of course, you must include: using System.Globalization; at the top of your code. I hope this information is useful:)
Answered by searchingSO
Post is based on https://stackoverflow.com/questions/7875259/how-do-i-get-the-am-pm-value-from-a-datetime