Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7579057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:34:52+00:00 2026-05-30T17:34:52+00:00

The function returns the time in 24 hour format. function fomartTimeShow(h) { return h

  • 0

The function returns the time in 24 hour format.

function fomartTimeShow(h) {
    return h < 10 ? "0" + h + ":00" : h + ":00";
}

returns the time in 24 hour format. I want the time to be converted in 12 hour format.
Anyhelp would be greatly appreciated.
Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T17:34:53+00:00Added an answer on May 30, 2026 at 5:34 pm

    Just use modulus 12:

    function formatTimeShow(h_24) {
        var h = h_24 % 12;
        return (h < 10 ? '0' : '') + h + ':00';
    }
    

    Modulus (%) means divide and take remainder. For example 17 / 12 = 1 with remainder 5. So the result of 17 % 12 is 5. And hour 17 is hour 5 in 12-hour time.

    But note that this function is not complete since it doesn’t work for hour 0 (or hour 12). To fix it you have to add in another check for that:

    function formatTimeShow(h_24) {
        var h = h_24 % 12;
        if (h === 0) h = 12;
        return (h < 10 ? '0' : '') + h + ':00';
    }
    

    Also note that you can add a meridian easily, by seeing whether the hour is less than 12 (am) or equal to/greater (pm):

    function formatTimeShow(h_24) {
        var h = h_24 % 12;
        if (h === 0) h = 12;
        return (h < 10 ? '0' : '') + h + ':00' + (h_24 < 12 ? 'am' : 'pm');
    }
    

    Note: All of the above is assuming the parameter to this function is an integer between 0 and 23.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want this function to take a datetime and return the time expressed as
The Win32 GetTimeZoneInformation function returns your systems local time zone as set up in
The CreateFileMapping function returns a pointer to a memory mapped file, and I want
What is the easiest way to convert military (24 hour time) to HH:MM format
I have a function of which I need to return the time for another
uniqid() function returns a 13 digits long hexadecimal number. According to the spec in
PHP's explode function returns an array of strings split on some provided substring. It
WinAPI OpenFile function returns HFILE, and GetFileTime for instance needs HANDLE. When I feed
The list.index(x) function returns the index in the list of the first item whose
I'm calling a function and adding a listener for when the function returns some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.