Add “am” or “pm” to Specific Time Format Dependent on Time of Day - Javascript












0















I have the following javaScript which is showing the time in hours and minutes.



Is there a way of having an "am" or "pm" next to the time, dependent on whether it's before or after midday?






var t = new Date();
var time = document.getElementById("time");

time.textContent = t.getHours() + ":" + t.getMinutes();

<h3 class="time-holder">Current UK time: <span id="time">12:00</span></h3>












share|improve this question























  • Possible duplicate: stackoverflow.com/questions/14638018/…

    – Chris Happy
    Nov 26 '18 at 2:06
















0















I have the following javaScript which is showing the time in hours and minutes.



Is there a way of having an "am" or "pm" next to the time, dependent on whether it's before or after midday?






var t = new Date();
var time = document.getElementById("time");

time.textContent = t.getHours() + ":" + t.getMinutes();

<h3 class="time-holder">Current UK time: <span id="time">12:00</span></h3>












share|improve this question























  • Possible duplicate: stackoverflow.com/questions/14638018/…

    – Chris Happy
    Nov 26 '18 at 2:06














0












0








0








I have the following javaScript which is showing the time in hours and minutes.



Is there a way of having an "am" or "pm" next to the time, dependent on whether it's before or after midday?






var t = new Date();
var time = document.getElementById("time");

time.textContent = t.getHours() + ":" + t.getMinutes();

<h3 class="time-holder">Current UK time: <span id="time">12:00</span></h3>












share|improve this question














I have the following javaScript which is showing the time in hours and minutes.



Is there a way of having an "am" or "pm" next to the time, dependent on whether it's before or after midday?






var t = new Date();
var time = document.getElementById("time");

time.textContent = t.getHours() + ":" + t.getMinutes();

<h3 class="time-holder">Current UK time: <span id="time">12:00</span></h3>








var t = new Date();
var time = document.getElementById("time");

time.textContent = t.getHours() + ":" + t.getMinutes();

<h3 class="time-holder">Current UK time: <span id="time">12:00</span></h3>





var t = new Date();
var time = document.getElementById("time");

time.textContent = t.getHours() + ":" + t.getMinutes();

<h3 class="time-holder">Current UK time: <span id="time">12:00</span></h3>






javascript time






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 26 '18 at 1:59









Paul12_Paul12_

3811211




3811211













  • Possible duplicate: stackoverflow.com/questions/14638018/…

    – Chris Happy
    Nov 26 '18 at 2:06



















  • Possible duplicate: stackoverflow.com/questions/14638018/…

    – Chris Happy
    Nov 26 '18 at 2:06

















Possible duplicate: stackoverflow.com/questions/14638018/…

– Chris Happy
Nov 26 '18 at 2:06





Possible duplicate: stackoverflow.com/questions/14638018/…

– Chris Happy
Nov 26 '18 at 2:06












2 Answers
2






active

oldest

votes


















1














Answer adapted from here.






var d = new Date();
var hr = d.getHours();
var min = d.getMinutes();
if (min < 10) {
min = "0" + min;
}
var ampm = "am";
if( hr > 12 ) {
hr -= 12;
ampm = "pm";
}


var time = document.getElementById("time");

time.textContent = hr + ":" + min + ampm;

<h3 class="time-holder">Current UK time: <span id="time">12:00</span></h3>








share|improve this answer































    -1














    use the "toLocaleTimeString()" method instead of the "getHours()" and "getMinutes()"



    for example:



    var t = new Date();
    var time = document.getElementById("time");

    time.textContent = t.toLocaleTimeString('en-US');


    source:
    https://www.w3schools.com/jsref/jsref_tolocaletimestring.asp






    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%2f53473877%2fadd-am-or-pm-to-specific-time-format-dependent-on-time-of-day-javascript%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Answer adapted from here.






      var d = new Date();
      var hr = d.getHours();
      var min = d.getMinutes();
      if (min < 10) {
      min = "0" + min;
      }
      var ampm = "am";
      if( hr > 12 ) {
      hr -= 12;
      ampm = "pm";
      }


      var time = document.getElementById("time");

      time.textContent = hr + ":" + min + ampm;

      <h3 class="time-holder">Current UK time: <span id="time">12:00</span></h3>








      share|improve this answer




























        1














        Answer adapted from here.






        var d = new Date();
        var hr = d.getHours();
        var min = d.getMinutes();
        if (min < 10) {
        min = "0" + min;
        }
        var ampm = "am";
        if( hr > 12 ) {
        hr -= 12;
        ampm = "pm";
        }


        var time = document.getElementById("time");

        time.textContent = hr + ":" + min + ampm;

        <h3 class="time-holder">Current UK time: <span id="time">12:00</span></h3>








        share|improve this answer


























          1












          1








          1







          Answer adapted from here.






          var d = new Date();
          var hr = d.getHours();
          var min = d.getMinutes();
          if (min < 10) {
          min = "0" + min;
          }
          var ampm = "am";
          if( hr > 12 ) {
          hr -= 12;
          ampm = "pm";
          }


          var time = document.getElementById("time");

          time.textContent = hr + ":" + min + ampm;

          <h3 class="time-holder">Current UK time: <span id="time">12:00</span></h3>








          share|improve this answer













          Answer adapted from here.






          var d = new Date();
          var hr = d.getHours();
          var min = d.getMinutes();
          if (min < 10) {
          min = "0" + min;
          }
          var ampm = "am";
          if( hr > 12 ) {
          hr -= 12;
          ampm = "pm";
          }


          var time = document.getElementById("time");

          time.textContent = hr + ":" + min + ampm;

          <h3 class="time-holder">Current UK time: <span id="time">12:00</span></h3>








          var d = new Date();
          var hr = d.getHours();
          var min = d.getMinutes();
          if (min < 10) {
          min = "0" + min;
          }
          var ampm = "am";
          if( hr > 12 ) {
          hr -= 12;
          ampm = "pm";
          }


          var time = document.getElementById("time");

          time.textContent = hr + ":" + min + ampm;

          <h3 class="time-holder">Current UK time: <span id="time">12:00</span></h3>





          var d = new Date();
          var hr = d.getHours();
          var min = d.getMinutes();
          if (min < 10) {
          min = "0" + min;
          }
          var ampm = "am";
          if( hr > 12 ) {
          hr -= 12;
          ampm = "pm";
          }


          var time = document.getElementById("time");

          time.textContent = hr + ":" + min + ampm;

          <h3 class="time-holder">Current UK time: <span id="time">12:00</span></h3>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 26 '18 at 2:08









          Chris HappyChris Happy

          4,62811234




          4,62811234

























              -1














              use the "toLocaleTimeString()" method instead of the "getHours()" and "getMinutes()"



              for example:



              var t = new Date();
              var time = document.getElementById("time");

              time.textContent = t.toLocaleTimeString('en-US');


              source:
              https://www.w3schools.com/jsref/jsref_tolocaletimestring.asp






              share|improve this answer




























                -1














                use the "toLocaleTimeString()" method instead of the "getHours()" and "getMinutes()"



                for example:



                var t = new Date();
                var time = document.getElementById("time");

                time.textContent = t.toLocaleTimeString('en-US');


                source:
                https://www.w3schools.com/jsref/jsref_tolocaletimestring.asp






                share|improve this answer


























                  -1












                  -1








                  -1







                  use the "toLocaleTimeString()" method instead of the "getHours()" and "getMinutes()"



                  for example:



                  var t = new Date();
                  var time = document.getElementById("time");

                  time.textContent = t.toLocaleTimeString('en-US');


                  source:
                  https://www.w3schools.com/jsref/jsref_tolocaletimestring.asp






                  share|improve this answer













                  use the "toLocaleTimeString()" method instead of the "getHours()" and "getMinutes()"



                  for example:



                  var t = new Date();
                  var time = document.getElementById("time");

                  time.textContent = t.toLocaleTimeString('en-US');


                  source:
                  https://www.w3schools.com/jsref/jsref_tolocaletimestring.asp







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 26 '18 at 2:07









                  hortighortig

                  854




                  854






























                      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.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53473877%2fadd-am-or-pm-to-specific-time-format-dependent-on-time-of-day-javascript%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

                      Costa Masnaga

                      Fotorealismo

                      Sidney Franklin