Displaying 12 hour format

206 views
Skip to first unread message

Gabriele Cozzolino

unread,
Nov 13, 2015, 4:35:34 PM11/13/15
to DroidScript
Hi, what about format hours?
I found Javascript reference using SimpleDateFormat but it seems is not accepted by DroidScript... how can I do this? Thanks

Steve Garman

unread,
Nov 13, 2015, 5:04:56 PM11/13/15
to DroidScript
It would help if you would provide a link to the JavaScript reference where you found this.

As far as I am aware, SimpleDateFormat is not available in standard JavaScript.

You may have been looking at a Java date reference or a JavaScript library that provides a function SimpleDateFormat.

If all you want is to display the time in 12 hour format, the easiest answer may be to write a little function to do that, based on what you can find at
http://www.w3schools.com/jsref/jsref_obj_date.asp

Or you could find a pre-written function somewhere like
http://stackoverflow.com/questions/8888491/how-do-you-display-javascript-datetime-in-12-hour-am-pm-format

Charles Wilt

unread,
Nov 13, 2015, 9:38:11 PM11/13/15
to DroidScript
Try

 .toLocaleTimeString()

Steve Garman

unread,
Nov 14, 2015, 12:42:07 AM11/14/15
to DroidScript
Charles,
You can't rely on toLocaleTimeString() to return 12 hour time. It returns 24 hour time here.

From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleTimeString
// British English uses 24-hour time without AM/PM
console.log(date.toLocaleTimeString('en-GB'));
// → "03:00:00"

Chris

unread,
Nov 14, 2015, 12:51:00 AM11/14/15
to DroidScript
I've used this in a few projects, it hasn't been updated since 2007, but, the functions still work great, and you can manipulate the date in a bunch of different ways:

http://www.datejs.com/

Gabriele Cozzolino

unread,
Nov 14, 2015, 4:52:08 AM11/14/15
to DroidScript
Thank you for all the answers, I found that this is somehow working
new Date().toString("hh:mm")
But it always return the complete string "Sat Nov 14 10:50:20 GMT+0100 (CET)" and apparently it doesn't consider the format option.

Gabriele Cozzolino

unread,
Nov 14, 2015, 4:57:38 AM11/14/15
to DroidScript
Sorry for the double post, but this is working ok:

new Date().getHours() % 12 || 12

Charles Wilt

unread,
Nov 14, 2015, 7:11:44 AM11/14/15
to DroidScript
Thanks Steve, I did not know that.
Reply all
Reply to author
Forward
0 new messages