Solved

DateTime object extract AM/PM

Posted on 2008-10-24
4
1,325 Views
Last Modified: 2012-06-27
Hi - seems microsoft hasn't added a method to retrieve whether a DateTime time is AM or PM. Is there an easy way to determine whether a DateTime is AM or PM? You seem to be able to retrieve everything else...but not what part of the day it is in. I hope I won't have to resort to extracting the entire time to a string and looking at the last two characters to determine whether its am or pm...
0
Comment
Question by:jeremyduj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 22797052
Hi jeremyduj;

The following code snippet show how to get the AM or PM of a DateTime object.

            // Get the current date and time
            DateTime dt = DateTime.Now;
            // Get what part of the day it is as AM or PM
            String amPm = dt.ToString("tt");

Fernando
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22797426
if you want the result as boolean, and avoid any cultural problems, you can use:

DateTimeFormatInfo.InvariantInfo

bool isAm = (dt.ToString("tt", DateTimeFormatInfo.InvariantInfo) == "AM");
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 22798397
I would utilize the HOUR() Property...which uses a 24 hour system.  Anything less than 12 would be AM...anything greater than or equal to 12 is PM:


        private void button1_Click(object sender, EventArgs e)
        {
            DateTime dt = DateTime.Now;
            if (dt.Hour < 12)
            {
                // AM
                System.Diagnostics.Debug.Print("AM");
            }
            else
            {
                // PM
                System.Diagnostics.Debug.Print("PM");
            }
        }

Open in new window

0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 22868129
Did any of the post help you out?
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
"Emulate" TAB key when press Enter Key 3 92
Upgrade code from VS 2010 to VS 2015 7 45
C# LINQ 5 41
How do you think this website does searches? 5 34
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Suggested Courses

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question