-1

I've the following code that returns the time in 24 hour format. I want it in 12 hr am pm format

public void LogProcess()
    {
        string Reason = null;
        Console.WriteLine("Enter your name:");
        string UserName = Console.ReadLine();
        DateTime t = DateTime.Now;
        int hr = t.Hour;
        int m = t.Minute;

can u please guide me in this.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • 2
    MSDN documentation: [Custom Date and Time Format Strings](http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx) – germi Apr 25 '14 at 12:26
  • If you want the value (i. e. 3 instead of 15), just do `(dateHour % 12)` – germi Apr 25 '14 at 12:29
  • @germi: Nope, that doesn't give you the normal 12-hour format, which is 1-12, not 0-11. – Jon Skeet Apr 25 '14 at 12:33
  • @germi Why? There is already a facility to fetch correct value. – Bharadwaj Apr 25 '14 at 12:34
  • @JonSkeet Ah right. I never use it and only thought about midnight, in which case I normally say 0:... But you're right, of course. – germi Apr 25 '14 at 12:35
  • ... and this is why Noda Time has [`LocalTime.ClockHourOfHalfDay`](http://nodatime.org/unstable/api/html/P_NodaTime_LocalTime_ClockHourOfHalfDay.htm). – Jon Skeet Apr 25 '14 at 12:35
  • Caroline, this code doesn't *return* anything. Given the name of the method, presumably you're really getting the values in order to format them into a string... in which case you should perform a *single* string formatting operation, rather than splitting things up. More context would help us to help you more. – Jon Skeet Apr 25 '14 at 12:36
  • @Bharadwaj Can you get the [1-12] value without formatting it as `string` and then casting it back to `int`? I only know that way... – germi Apr 25 '14 at 12:38
  • (I'd also avoid using a 12 hour format anyway, in almost all cases...) – Jon Skeet Apr 25 '14 at 12:40
  • @germi `I want it in 12 hr am pm format`, this doesn't explain to get the value in `int`. – Bharadwaj Apr 25 '14 at 12:40
  • @Bharadwaj That's why I first directed OP to the documentation where the string formatting options are explained. Getting the `int` value was an additional (though wrong) comment. – germi Apr 25 '14 at 12:41
  • @germi Your first link is good, but your next comment is not wrong, but I guess it is not what OP want. – Bharadwaj Apr 25 '14 at 12:43

2 Answers2

3

Solution 1 : You can use ToString() function with custom date format strings to get the required part of the DateTime string.

1. You need to use small hh for getting the hours in 12 Hour format. 2. You need to use tt for getting the AM or PM string.

Try

string Hours = DateTime.Now.ToString("hh");
string AMorPM = DateTime.Now.ToString("tt");

For complete Time Info:

DateTime dt = DateTime.Now;
int hours = Convert.ToInt32(dt.ToString("hh"));
int minutes = dt.Minute;
int seconds = dt.Second;
string AMorPM  = dt.ToString("tt");

Solution 2: You can perfomr the mod operation with 24 hour value with 12 to get the 12 hour value.

Note: You need to map result (after mod operation) to 1 if it is 0.

int hours = (dt.Hour % 12 != 0) ? dt.Hour : 1;
string AMorPM  = dt.ToString("tt");
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
2

DateTime has no implicit format, it is just a DateTime value.. But strings have. You can use hh specifier with DateTime.ToString(string) method to get 12 hour-clock representation of your hour.

var dt = DateTime.Now;
string hr = dt.ToString("hh");
string m =  dt.ToString("mm");

For more information, take a look at;

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • Except you shouldn't call `DateTime.Now` twice, as the time could change between the two. For example, at about 3pm you could end up with "2" and "0" instead of either "2" and "59" or "3" and "0". – Jon Skeet Apr 25 '14 at 12:34
  • @JonSkeet Oh, good point Jon. Updated. Thanks. – Soner Gönül Apr 25 '14 at 13:33