3

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);
});
Community
  • 1
  • 1
JacobTheDev
  • 17,318
  • 25
  • 95
  • 158

2 Answers2

2

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.

shubniggurath
  • 956
  • 1
  • 15
  • 31
  • This works fine: `return newDate.getHours() + ':' + newDate.getMinutes();` but after trying to convert it to am/pm time it just shows up as "12" for everything. – JacobTheDev Jan 31 '13 at 20:32
  • Here you go, sorry about the '12's... (newDate.getHours() === 0) ? 12 : ((newDate.getHours() > 12) ? newDate.getHours() - 12 : newDate.getHours()) I'll update above as well – shubniggurath Jan 31 '13 at 20:39
  • 1
    Required a little tweaking, but this now works. Thanks. `return (newDate.getHours() === 0) ? 12 : ((newDate.getHours() > 12) ? newDate.getHours() - 12 : newDate.getHours()) + ' : ' + newDate.getMinutes() + ' ' + ((newDate.getHours() > 11) ? 'PM' : 'AM');` – JacobTheDev Jan 31 '13 at 20:44
  • Glad you got it working. – shubniggurath Jan 31 '13 at 20:49
-2
var myDate= new Date();
myDate.format("h:mm tt"); 

http://www.jslab.dk/library/date.format

BentOnCoding
  • 27,307
  • 14
  • 64
  • 92
  • This gives me the error: `Uncaught TypeError: Object Thu Jan 31 2013 14:18:54 GMT-0600 (Central Standard Time) has no method 'format'` – JacobTheDev Jan 31 '13 at 20:18
  • Me too. You can check your code here in future: http://repl.it/languages/JavaScript – Liam Mar 28 '14 at 05:56