Solved

DateTime object extract AM/PM

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

831 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