How to format and display date without time zone?

Viewed 62

So this is how I get the date time:

for (var i = 0; i < response.length; i++) {
  console.log("Time: "+moment(response[i].feeding_1).format("hh:mm A"))
}

How can I display it as 06:00 AM?

I tried with moment js, but didn't work quite well like this:

console.log("Time: "+moment(response[i].feeding_1).format("hh:mm A"))

I get invalid date for this..

also in HTML i did this:

<td>
  <div>{{pen.feeding_1 | date: 'hh:mm a'}}</div>
</td>

Sorry for lack of code

1 Answers

Assuming you get a string of format '06:00:00Z' and you need another string ('06:00 AM') as the output, you may skip go messing with date types, libraries and timezones, simply doing String.prototype.replace():

const src = '06:00:00Z',

      result = src.replace(
        /^(\d{2}):(\d{2}).*/, 
        (_, hh, mm) => 
          `${hh > 12 ? +hh-12 : hh}:${mm} ${hh>=12 ? 'PM' : 'AM'}`)
          
console.log(result)

Related