Solved

DateTime object extract AM/PM

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

Expert Comment

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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
System.Security 2 29
Diagnostics with Net and Net.Sockets 2 33
Media.Imaging 1 24
Set value of dynamically created checkboxlist 2 42
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

820 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