I\'m currently attempting to display the user\'s time without displaying the seconds. Is there a way I can do this using Javascript\'s .toLocaleTimeString()?
Doing s
You can try this, which is working for my needs.
var d = new Date();
d.toLocaleTimeString().replace(/:\d{2}\s/,' ');
or
d.toLocaleString().replace(/:\d{2}\s/,' ');
I've also been looking for solution to this problem, here's what I eventually came up with:
function getTimeStr() {
var dt = new Date();
var d = dt.toLocaleDateString();
var t = dt.toLocaleTimeString();
t = t.replace(/\u200E/g, '');
t = t.replace(/^([^\d]*\d{1,2}:\d{1,2}):\d{1,2}([^\d]*)$/, '$1$2');
var result = d + ' ' + t;
return result;
}
You can try it here: http://jsfiddle.net/B5Zrx/
\u200E is some formatting character that I've seen on some IE version (it's unicode left-to-right mark).
I assume that if the formatted time contains something like "XX:XX:XX" then it must be time with seconds and I remove the last part, if I don't find this pattern, nothing is changed. Pretty safe, but there is a risk of leaving seconds in some weird circumstances.
I just hope that there is no locale that would change the order of formatted time parts (e.g. make it ss:mm:hh). This left-to-right mark is making me a bit nervous about that though, that is why I don't remove the right-to-left mark (\u202E) - I prefer to not find a match in this case and leave the time formatted with seconds in such case.
Even though this is an older question, I had the same one myself recently, and came up with a more simple solution using regular expressions and the string replace function as another alternative (no need for external js libraries or reliance on the ECMAScript Internalization API):
var d = new Date();
var localeTime = d.toLocaleTimeString();
var localeTimeSansSeconds = localeTime.replace(/:(\d{2}) (?=[AP]M)/, " ");
This approach uses a regex look-ahead to grab the :ss AM/PM end of the string and replaces the :ss part with a space, returning the rest of the string untouched. (Literally says "Find a colon with two digits and a space that is followed by either AM or PM and replace the colon, two digits, and space portion with just a space).
This expression/approach only works for en-US and en-US-like Locales. If you also wanted a similar outcome with, say, British English (en-GB), which doesn't use AM/PM, a different regular expression is needed.
Based on the original questioner's sample output, I assume that they were primarily dealing with American audiences and the en-US time schema.
With locales :
var date = new Date();
date.toLocaleTimeString('fr-FR', {hour: '2-digit', minute: '2-digit'})
I think the original question can easily be answered with something being overlooked so far: a simple string split. The time being returned is a text string, just split it on the ":" delimiter and reassemble the string the way you want. No plug ins, no complicated scripting. and here ya go:
var myVar=setInterval(function(){myTimer()},1000);
function myTimer() {
var d = new Date();
currentNow = d.toLocaleTimeString();
nowArray = currentNow.split(':');
filteredNow = nowArray[0]+':'+nowArray[1];
document.getElementById("demo").innerHTML = filteredNow;
}
This works for me:
var date = new Date();
var string = date.toLocaleTimeString([], {timeStyle: 'short'});