Solved

DateTime object extract AM/PM

Posted on 2008-10-24
4
1,313 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
  • 2
4 Comments
 
LVL 62

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 85

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 62

Expert Comment

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

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video discusses moving either the default database or any database to a new volume.

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now