javascript string to time and am/pm format

Problem

I've a time in string format

var start = "14:00:00"

and I need the output to be

02:00pm

<span>{{session.start | some-format}} to {{session.end}}<span>

or javascript manipulation (any will do)

I looked up this page but it needs some kind of time stamp I guess.

https://docs.angularjs.org/api/ng/filter/date

Problem courtesy of: user2727195

Solution

Do something like:

function toTime(timeString){
    var timeTokens = timeString.split(':');
    return new Date(1970,0,1, timeTokens[0], timeTokens[1], timeTokens[2]);
}

var start = "14:00:00";
var date = toTime(start);

then apply the angular filter:

<span>{{date | date:'h:mm a'}} to {{session.end}}<span>
Solution courtesy of: Joe

Discussion

Try this

function timeConvert (time) {
  time = time.toString ().match (/^([01]\d|2[0-3])(:)([0-5]\d)(:[0-5]\d)?$/) || [time];

  if (time.length > 1) { 
    time = time.slice (1);  
    time[5] = +time[0] < 12 ? 'AM' : 'PM';
    time[0] = +time[0] % 12 || 12;
  }
  return time.join ('');
}

timeConvert ('19:00:00');

out: "7:00:00PM"

Discussion courtesy of: vcrzy

Using moment you can do something like this:

var timeArr = "14:00:00".split(':');

var date = new Date().setHours(timeArr[0], timeArr[1], timeArr[2])

var startTime = moment(date).format("h:mm:ss A");

var endTime = moment().format("h:mm:ss A");
Discussion courtesy of: Seth Pollack

I saw you mentioned angular documentation in the question so if you are after an Angular approach you can give it a try with this:

$scope.start = new Date();
{{ start | date: 'shortTime'}}

Example here: Plnkr

Discussion courtesy of: Roco CTZ

This recipe can be found in it's original form on Stack Over Flow.