Solved

DateTime object extract AM/PM

Posted on 2008-10-24
4
1,327 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

724 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