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

  • Home
  • SEARCH
  • 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 9220481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:21:32+00:00 2026-06-18T03:21:32+00:00

Possible Duplicate: Formatting a date in JavaScript How to get datetime in javascript? I’ve

  • 0

Possible Duplicate:
Formatting a date in JavaScript
How to get datetime in javascript?

I’ve found a script that displays the current date and time in various time zones. I can’t figure out how to change the format. Currently, it displays as MM/DD/YYYY HH:MM:SS AM/PM and I want it to just display as HH:MM AM/PM

I’m more skilled with jQuery than plain JavaScript, and this is confounding me:

$(document).ready(function() {
    function calcTime(offset) {
        currentDate = new Date();
        utc = currentDate.getTime() + (currentDate.getTimezoneOffset() * 60000);
        newDate = new Date(utc + (3600000*offset));
        return newDate.toLocaleString();
    }
    function displayTimes() {
        $("#chicago").html(calcTime("-6"));
        $("#london").html(calcTime("+1"));
        $("#shanghai").html(calcTime("+8"));
    };
    window.setInterval(displayTimes, 1000);
});
  • 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-06-18T03:21:33+00:00Added an answer on June 18, 2026 at 3:21 am

    The culprit is the following line.

        return newDate.toLocaleString();
    

    Specifically, toLocaleString()

    You can find out more about what that line’s doing here: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/toLocaleDateString

    A quick way would be to use .toTimeString()

    Instead of returning newDate.toLocaleString() or .toTimeString() you want to make it print as you wish it to.

    e.g.

        return newDate.getHours() + ':' newDate.getMinutes();
    

    That’ll give you the military time.

    If you want am/pm display, this can get that for you

    (newDate.getHours() >  11) ? 'pm' : 'am'
    

    If you want the time prettified, 0 is 12 midnight, and 12 is noon. You can subtract 12 if the hours are greater than 12. If 0, you can also set it to display 12. All that can be done like this:

     (newDate.getHours() === 0) ? 12 : ((newDate.getHours() > 12) ? newDate.getHours() - 12 : newDate.getHours());
    

    Should definitely use a var for newDate.getHours(). Speaking of vars…

    Please reformat your vars as such:
    var currentDate = new Date(),
    utc = currentDate.getTime() + (currentDate.getTimezoneOffset() * 60000),
    newDate = new Date(utc + (3600000*offset));

    Hope that helps.

    EDIT: All together now:
    replace the following

    return newDate.toLocaleString();
    

    with the following

    return (newDate.getHours() === 0) ? 12 : ((newDate.getHours() > 12) ? newDate.getHours() - 12 : newDate.getHours()) + ' : ' + newDate.getMinutes() + ' ' + (newDate.getHours() >  11) ? 'pm' : 'am';
    

    That’s rather sloppy, and hurriedly written, but if you use a var for newDate.getHours() it’ll be an easier read.

    Thanks.

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

Sidebar

Related Questions

Possible Duplicate: Formatting a date in JavaScript I have the following piece of script.
Possible Duplicate: Formatting a date in JavaScript I need to show a current date
Possible Duplicate: JavaScript: formatting number with exactly two decimals Now that I have got
Possible Duplicate: Formatting a date in JavaScript I have a form having lots of
Possible Duplicate: Formatting a date in javascript I know other possible formats in JavaScript
Possible Duplicate: Formatting a date in javascript I have this: HTML Start Date: <input
Possible Duplicate: JavaScript: formatting number with exactly two decimals Can some one please help
Possible Duplicate: JavaScript: formatting number with exactly two decimals How do I limit the
Possible Duplicate: Why does PHP echo'd text lose it's formatting? I cant get the
Possible Duplicate: Javascript: formatting a rounded number to N decimals I'm modifying a label

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.