leora
leora

Reputation: 196459

how to get the 12 hour date from DateTime

when i get the DateTime.Hour property, i always get the 24 hour time (so 6PM would give me 18).

how do i get the "12 hour" time so 6PM just gives me 6.

i obviously can do the check myself but i assume there is a built in function for this.

Upvotes: 7

Views: 17999

Answers (7)

geethu
geethu

Reputation: 98

    DateTime date = Convert.ToDateTime("12/12/2022 20:20:00 PM");
    var hour = date.Hour;
    var dateTime = Convert.ToDateTime((date.ToShortDateString() + " " + hour + ":00:00"));
    Console.WriteLine(dateTime); // 12/12/2022 8:00:00 PM
    Console.WriteLine(hour); // 20

Upvotes: 0

Code Novice
Code Novice

Reputation: 2398

I thought the most convenient answer was submitted by Jon Skeet. The below is the same but converted to Visual Basic. I like things to be super easy. It took me a few to figure out the C# to Visual Basic Conversion. I included some 'extra' stuff as well. Hope this saves someone else time.

Visual Basic

(((DateTime.Now().Hour + 11) Mod 12) + 1)

Extra

Dim stringDate = DateTime.Now().Year &
                 DateTime.Now().Month.ToString("00") &
                 DateTime.Now().Day.ToString("00") & "_" &
                 (((DateTime.Now().Hour + 11) Mod 12) + 1).ToString("00") &
                 DateTime.Now().Minute.ToString("00")

The ToString("00") forces each Month/Day/Hour/Minute to always be represented by two digits.

Year = 2019

Month: April 4 = 04

Day: 3 = 03

Hour: 10 = 10

5 Minutes = 05

stringDate = 201904031005

Upvotes: 0

Michel St-Louis
Michel St-Louis

Reputation: 19

What about simply:

public static int GetTwelveCycleHour(this DateTime dateTime)
{
    return Convert.ToInt32(dateTime.ToString("h"));
}

Upvotes: 1

Joel Coehoorn
Joel Coehoorn

Reputation: 415690

There's no built-in function, mainly because you shouldn't need it:

  • If you're doing this for output to the user, look at using a format string.
  • If you're doing this for a calculation, you can subtract datetimes or add timespans directly.

Outside of this, the math calculation is simple enough and already available in other answers here.

Upvotes: 0

Aaron Daniels
Aaron Daniels

Reputation: 9664

I don't know of any built in method, but you can always add an extension method to accomplish this.

Of course, you could always replace the code with the way you want to accomplish it.

public static class Extension
{
    public static int GetTwelveCycleHour(this DateTime dateTime)
    {
        if (dateTime.Hour > 12)
        {
            return dateTime.Hour - 12;
        }

        return dateTime.Hour;
    }
}

Upvotes: 3

Sergio
Sergio

Reputation: 8259

DateTime.Now.ToString("hh"); --> Using this you will get "06" for 18h.

Upvotes: 9

Jon Skeet
Jon Skeet

Reputation: 1500215

How about:

DateTime.Hour % 12 

That will give 0-11 of course... do you want 1-12? If so:

((DateTime.Hour + 11) % 12) + 1

I don't think there's anything simpler built in...

Upvotes: 24

Related Questions