Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get LocaleTime without seconds in javascript?

i have a timestamp which is for example '2013-01-21T01:23:44' i am doing this to get the time

var time1= new Date('2013-01-21T01:23:44');
var time2 = time1.toLocaleTimeString();

This is returning the time but as 01:23:44 . How do i do this in a way it does not return the seconds .

like image 628
vijar Avatar asked Jan 23 '14 02:01

vijar


People also ask

How do you get toLocaleString time?

To get the current date and time in JavaScript, you can use the toLocaleString() method, which returns a string representing the given date according to language-specific conventions. To display only the time, you can use the toLocaleTimeString() method.

What is toLocaleTimeString () in JavaScript?

The toLocaleTimeString() method returns a string with a language-sensitive representation of the time portion of the date. In implementations with Intl.DateTimeFormat API support, this method simply calls Intl.DateTimeFormat .


3 Answers

What about trimming it off using regex?

time2 = time1.toLocaleTimeString().replace(/(.*)\D\d+/, '$1');
like image 52
p.s.w.g Avatar answered Sep 27 '22 21:09

p.s.w.g


This sometimes wont work, see RobG answer instead.

remove the last 3 characters:

str = str.slice(0, -3);

like image 21
ajax333221 Avatar answered Sep 27 '22 23:09

ajax333221


> var time1= new Date('2013-01-21T01:23:44');

Until ES5, parsing of strings by the Date constructor was entirely implementation dependent. Subsequent to ES5, there are 3 formats that are supported (the formats produced by Date.prototype.toString, toISOString and toUTCString) but everything else is still implementation dependent.

Timestamps in the format YYYY-MM-DDTHH:mm:ss without an offset should be parsed as local, but there may be non–conforming implementations in use.

One solution is to parse it to a Date then format the time as required.

The localeTimeString function is implementation dependent and results vary depending on system settings. It's intended to represent a time that is tailored for particular users, so you should not mess with it as you can't be certain of its format (and it's supposed to be something the user will understand, not somethign you think they will understand).

The following uses a couple of small functions to parse the timestamp to a Date then format the time as required.

// Parse timestamp in YYYY-MM-DDTHH:mm:ss format as local
function parseDateString(s) {
  var b = s.split(/\D/g);
  return new Date(b[0], --b[1], b[2], b[3], b[4], b[5], 0);
}

// Format time as hh:mm AM/PM
function formatHHMM(date) {
  function z(n) {
    return (n < 10 ? '0' : '') + n;
  }
  var h = date.getHours();
  return z(h % 12 || 12) + ':' + z(date.getMinutes()) + ' ' + (h < 12 ? 'AM' : 'PM');
}

console.log(
  formatHHMM(parseDateString('2013-01-21T00:23:44')) // 12:23 AM
);

your result may differ, it depends on your timezone as the string is treated as UTC.

like image 35
RobG Avatar answered Sep 27 '22 23:09

RobG