Hour from DateTime? in 24 hours format












128














So i have this DateTime? and what i want to do is to obtain the hour but show it in 24 hours format.

For example:

If the hour is 2:20:23 p.m. i want to convert it to 14:20 and that's it.



I'm working with Visual C#.
Any ideas please, thank you.



I have something like this



public static string FormatearHoraA24(DateTime? fechaHora)
{
if (!fechaHora.HasValue)
return "";

string retornar = "";
//here goes what i need
}









share|improve this question






















  • @OrelEraki are you serious? This question was posted before that one by two years. At least check your facts before calling duplicate.
    – Archibald
    Sep 26 '16 at 5:50












  • There is no need to get exited, it was on 2014. Removed it.
    – Orel Eraki
    Sep 26 '16 at 7:17






  • 2




    "hh" in lower case is for am/pm notation, "HH" is for 24h notation
    – Antoine Meltzheim
    Sep 13 '17 at 13:48
















128














So i have this DateTime? and what i want to do is to obtain the hour but show it in 24 hours format.

For example:

If the hour is 2:20:23 p.m. i want to convert it to 14:20 and that's it.



I'm working with Visual C#.
Any ideas please, thank you.



I have something like this



public static string FormatearHoraA24(DateTime? fechaHora)
{
if (!fechaHora.HasValue)
return "";

string retornar = "";
//here goes what i need
}









share|improve this question






















  • @OrelEraki are you serious? This question was posted before that one by two years. At least check your facts before calling duplicate.
    – Archibald
    Sep 26 '16 at 5:50












  • There is no need to get exited, it was on 2014. Removed it.
    – Orel Eraki
    Sep 26 '16 at 7:17






  • 2




    "hh" in lower case is for am/pm notation, "HH" is for 24h notation
    – Antoine Meltzheim
    Sep 13 '17 at 13:48














128












128








128


9





So i have this DateTime? and what i want to do is to obtain the hour but show it in 24 hours format.

For example:

If the hour is 2:20:23 p.m. i want to convert it to 14:20 and that's it.



I'm working with Visual C#.
Any ideas please, thank you.



I have something like this



public static string FormatearHoraA24(DateTime? fechaHora)
{
if (!fechaHora.HasValue)
return "";

string retornar = "";
//here goes what i need
}









share|improve this question













So i have this DateTime? and what i want to do is to obtain the hour but show it in 24 hours format.

For example:

If the hour is 2:20:23 p.m. i want to convert it to 14:20 and that's it.



I'm working with Visual C#.
Any ideas please, thank you.



I have something like this



public static string FormatearHoraA24(DateTime? fechaHora)
{
if (!fechaHora.HasValue)
return "";

string retornar = "";
//here goes what i need
}






c# datetime






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jun 11 '10 at 14:34









euther

92651434




92651434












  • @OrelEraki are you serious? This question was posted before that one by two years. At least check your facts before calling duplicate.
    – Archibald
    Sep 26 '16 at 5:50












  • There is no need to get exited, it was on 2014. Removed it.
    – Orel Eraki
    Sep 26 '16 at 7:17






  • 2




    "hh" in lower case is for am/pm notation, "HH" is for 24h notation
    – Antoine Meltzheim
    Sep 13 '17 at 13:48


















  • @OrelEraki are you serious? This question was posted before that one by two years. At least check your facts before calling duplicate.
    – Archibald
    Sep 26 '16 at 5:50












  • There is no need to get exited, it was on 2014. Removed it.
    – Orel Eraki
    Sep 26 '16 at 7:17






  • 2




    "hh" in lower case is for am/pm notation, "HH" is for 24h notation
    – Antoine Meltzheim
    Sep 13 '17 at 13:48
















@OrelEraki are you serious? This question was posted before that one by two years. At least check your facts before calling duplicate.
– Archibald
Sep 26 '16 at 5:50






@OrelEraki are you serious? This question was posted before that one by two years. At least check your facts before calling duplicate.
– Archibald
Sep 26 '16 at 5:50














There is no need to get exited, it was on 2014. Removed it.
– Orel Eraki
Sep 26 '16 at 7:17




There is no need to get exited, it was on 2014. Removed it.
– Orel Eraki
Sep 26 '16 at 7:17




2




2




"hh" in lower case is for am/pm notation, "HH" is for 24h notation
– Antoine Meltzheim
Sep 13 '17 at 13:48




"hh" in lower case is for am/pm notation, "HH" is for 24h notation
– Antoine Meltzheim
Sep 13 '17 at 13:48












4 Answers
4






active

oldest

votes


















205














You can get the desired result with the code below. Two'H' in HH is for 24-hour format.



return fechaHora.Value.ToString("HH:mm");





share|improve this answer



















  • 3




    Description as to why this works would be useful. Such as HH for 24 hour format as hh for 12 hour.
    – Luke Alderton
    Aug 6 '18 at 13:56





















114














date.ToString("HH:mm:ss"); // for 24hr format
date.ToString("hh:mm:ss"); // for 12hr format, it shows AM/PM


Refer this link for other Formatters in DateTime.






share|improve this answer





























    12














    Using ToString("HH:mm") certainly gives you what you want as a string.



    If you want the current hour/minute as numbers, string manipulation isn't necessary; you can use the TimeOfDay property:



    TimeSpan timeOfDay = fechaHora.TimeOfDay;
    int hour = timeOfDay.Hours;
    int minute = timeOfDay.Minutes;





    share|improve this answer





















    • I like this method because it gives me integer values for calculation.
      – Hao Nguyen
      Jan 4 '16 at 20:03










    • This was the preferred solution for me because I just needed the int from the hour of the day. Much easier then converting to a string just to parse back into a DateTime again.
      – Sev
      Nov 17 '16 at 22:22



















    4














    Try this:



    //String.Format("{0:HH:mm}", dt);  // where dt is a DateTime variable

    public static string FormatearHoraA24(DateTime? fechaHora)
    {
    if (!fechaHora.HasValue)
    return "";

    return retornar = String.Format("{0:HH:mm}", (DateTime)fechaHora);
    }





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3023649%2fhour-from-datetime-in-24-hours-format%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      205














      You can get the desired result with the code below. Two'H' in HH is for 24-hour format.



      return fechaHora.Value.ToString("HH:mm");





      share|improve this answer



















      • 3




        Description as to why this works would be useful. Such as HH for 24 hour format as hh for 12 hour.
        – Luke Alderton
        Aug 6 '18 at 13:56


















      205














      You can get the desired result with the code below. Two'H' in HH is for 24-hour format.



      return fechaHora.Value.ToString("HH:mm");





      share|improve this answer



















      • 3




        Description as to why this works would be useful. Such as HH for 24 hour format as hh for 12 hour.
        – Luke Alderton
        Aug 6 '18 at 13:56
















      205












      205








      205






      You can get the desired result with the code below. Two'H' in HH is for 24-hour format.



      return fechaHora.Value.ToString("HH:mm");





      share|improve this answer














      You can get the desired result with the code below. Two'H' in HH is for 24-hour format.



      return fechaHora.Value.ToString("HH:mm");






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 21 '18 at 8:31









      Heemanshu Bhalla

      2,08211233




      2,08211233










      answered Jun 11 '10 at 14:40









      vtortola

      20.3k21120206




      20.3k21120206








      • 3




        Description as to why this works would be useful. Such as HH for 24 hour format as hh for 12 hour.
        – Luke Alderton
        Aug 6 '18 at 13:56
















      • 3




        Description as to why this works would be useful. Such as HH for 24 hour format as hh for 12 hour.
        – Luke Alderton
        Aug 6 '18 at 13:56










      3




      3




      Description as to why this works would be useful. Such as HH for 24 hour format as hh for 12 hour.
      – Luke Alderton
      Aug 6 '18 at 13:56






      Description as to why this works would be useful. Such as HH for 24 hour format as hh for 12 hour.
      – Luke Alderton
      Aug 6 '18 at 13:56















      114














      date.ToString("HH:mm:ss"); // for 24hr format
      date.ToString("hh:mm:ss"); // for 12hr format, it shows AM/PM


      Refer this link for other Formatters in DateTime.






      share|improve this answer


























        114














        date.ToString("HH:mm:ss"); // for 24hr format
        date.ToString("hh:mm:ss"); // for 12hr format, it shows AM/PM


        Refer this link for other Formatters in DateTime.






        share|improve this answer
























          114












          114








          114






          date.ToString("HH:mm:ss"); // for 24hr format
          date.ToString("hh:mm:ss"); // for 12hr format, it shows AM/PM


          Refer this link for other Formatters in DateTime.






          share|improve this answer












          date.ToString("HH:mm:ss"); // for 24hr format
          date.ToString("hh:mm:ss"); // for 12hr format, it shows AM/PM


          Refer this link for other Formatters in DateTime.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jun 11 '10 at 14:42









          Manish Basantani

          8,378185594




          8,378185594























              12














              Using ToString("HH:mm") certainly gives you what you want as a string.



              If you want the current hour/minute as numbers, string manipulation isn't necessary; you can use the TimeOfDay property:



              TimeSpan timeOfDay = fechaHora.TimeOfDay;
              int hour = timeOfDay.Hours;
              int minute = timeOfDay.Minutes;





              share|improve this answer





















              • I like this method because it gives me integer values for calculation.
                – Hao Nguyen
                Jan 4 '16 at 20:03










              • This was the preferred solution for me because I just needed the int from the hour of the day. Much easier then converting to a string just to parse back into a DateTime again.
                – Sev
                Nov 17 '16 at 22:22
















              12














              Using ToString("HH:mm") certainly gives you what you want as a string.



              If you want the current hour/minute as numbers, string manipulation isn't necessary; you can use the TimeOfDay property:



              TimeSpan timeOfDay = fechaHora.TimeOfDay;
              int hour = timeOfDay.Hours;
              int minute = timeOfDay.Minutes;





              share|improve this answer





















              • I like this method because it gives me integer values for calculation.
                – Hao Nguyen
                Jan 4 '16 at 20:03










              • This was the preferred solution for me because I just needed the int from the hour of the day. Much easier then converting to a string just to parse back into a DateTime again.
                – Sev
                Nov 17 '16 at 22:22














              12












              12








              12






              Using ToString("HH:mm") certainly gives you what you want as a string.



              If you want the current hour/minute as numbers, string manipulation isn't necessary; you can use the TimeOfDay property:



              TimeSpan timeOfDay = fechaHora.TimeOfDay;
              int hour = timeOfDay.Hours;
              int minute = timeOfDay.Minutes;





              share|improve this answer












              Using ToString("HH:mm") certainly gives you what you want as a string.



              If you want the current hour/minute as numbers, string manipulation isn't necessary; you can use the TimeOfDay property:



              TimeSpan timeOfDay = fechaHora.TimeOfDay;
              int hour = timeOfDay.Hours;
              int minute = timeOfDay.Minutes;






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jun 11 '10 at 14:44









              Dan Tao

              94k40249401




              94k40249401












              • I like this method because it gives me integer values for calculation.
                – Hao Nguyen
                Jan 4 '16 at 20:03










              • This was the preferred solution for me because I just needed the int from the hour of the day. Much easier then converting to a string just to parse back into a DateTime again.
                – Sev
                Nov 17 '16 at 22:22


















              • I like this method because it gives me integer values for calculation.
                – Hao Nguyen
                Jan 4 '16 at 20:03










              • This was the preferred solution for me because I just needed the int from the hour of the day. Much easier then converting to a string just to parse back into a DateTime again.
                – Sev
                Nov 17 '16 at 22:22
















              I like this method because it gives me integer values for calculation.
              – Hao Nguyen
              Jan 4 '16 at 20:03




              I like this method because it gives me integer values for calculation.
              – Hao Nguyen
              Jan 4 '16 at 20:03












              This was the preferred solution for me because I just needed the int from the hour of the day. Much easier then converting to a string just to parse back into a DateTime again.
              – Sev
              Nov 17 '16 at 22:22




              This was the preferred solution for me because I just needed the int from the hour of the day. Much easier then converting to a string just to parse back into a DateTime again.
              – Sev
              Nov 17 '16 at 22:22











              4














              Try this:



              //String.Format("{0:HH:mm}", dt);  // where dt is a DateTime variable

              public static string FormatearHoraA24(DateTime? fechaHora)
              {
              if (!fechaHora.HasValue)
              return "";

              return retornar = String.Format("{0:HH:mm}", (DateTime)fechaHora);
              }





              share|improve this answer




























                4














                Try this:



                //String.Format("{0:HH:mm}", dt);  // where dt is a DateTime variable

                public static string FormatearHoraA24(DateTime? fechaHora)
                {
                if (!fechaHora.HasValue)
                return "";

                return retornar = String.Format("{0:HH:mm}", (DateTime)fechaHora);
                }





                share|improve this answer


























                  4












                  4








                  4






                  Try this:



                  //String.Format("{0:HH:mm}", dt);  // where dt is a DateTime variable

                  public static string FormatearHoraA24(DateTime? fechaHora)
                  {
                  if (!fechaHora.HasValue)
                  return "";

                  return retornar = String.Format("{0:HH:mm}", (DateTime)fechaHora);
                  }





                  share|improve this answer














                  Try this:



                  //String.Format("{0:HH:mm}", dt);  // where dt is a DateTime variable

                  public static string FormatearHoraA24(DateTime? fechaHora)
                  {
                  if (!fechaHora.HasValue)
                  return "";

                  return retornar = String.Format("{0:HH:mm}", (DateTime)fechaHora);
                  }






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jun 11 '10 at 14:51

























                  answered Jun 11 '10 at 14:39









                  Mike Dinescu

                  38.4k881121




                  38.4k881121






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3023649%2fhour-from-datetime-in-24-hours-format%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Wiesbaden

                      Marschland

                      Dieringhausen