Hey I have a react app where I made a slider for time. the time is for example: 3 PM or 7:30 AM like this format.
But I can't seem to format it correctly using moment, it is always returning AM Here is my implementation:
moment(dropoffTime.formattedTime, ['h:mm A'])
.locale('en')
.format('hh:mm A')
.format working fine and giving PM for me right now. Maybe you can console.log dropoffTime.formattedTime in order to see what is the value before formatting
console.log(moment(new Date())
.locale('en')
.format('hh:mm A'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment-with-locales.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment-with-locales.min.js"></script>
moment accepts dates either as a Date object or in a string format:
const dropoffTime = new Date();
const str = moment(dropoffTime)
.locale('en')
.format('hh:mm A');
console.log(str)
document.querySelector("input").addEventListener("input",function(){
console.log(moment(this.value, ['h:mm A']).locale('en').format('hh:mm A'))
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
<input type="time" value="12:34" >