How to get am pm from date time string using moment js

I have a string like Mon 03-Jul-2017, 11:00 AM/PM, and I need to convert it to a type string 11:00 AM/PMusing js moment.

The problem is that I cannot get AMeither PMfrom the date time string.

I'm doing it:

moment(Mon 03-Jul-2017, 11:00 AM, 'dd-mm-yyyy hh:mm').format('hh:mm A')

and it works fine as I get 11:00 AM, but if the string has PMin it, it still gives AMin the output.

like this moment(Mon 03-Jul-2017, 11:00 PM, 'dd-mm-yyyy hh:mm').format('hh:mm A')also gives 11:00 AMin conclusion instead11:00 PM

+38
source share
2 answers

. ddd , DD , MMM , YYYY , hh 1-12 , mm A AM/PM. moment(String, String) .

:

console.log( moment('Mon 03-Jul-2017, 11:00 AM', 'ddd DD-MMM-YYYY, hh:mm A').format('hh:mm A') );
console.log( moment('Mon 03-Jul-2017, 11:00 PM', 'ddd DD-MMM-YYYY, hh:mm A').format('hh:mm A') );
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
Hide result

+76

. , Date

var myDate = new Date('Mon 03-Jul-2017, 06:00 PM');

:

var myDate= new Date('Mon 03-Jul-2017, 06:00 PM');
console.log(moment(myDate).format('HH:mm')); // 24 hour format 
console.log(moment(myDate).format('hh:mm'));
console.log(moment(myDate).format('hh:mm A'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
Hide result

0

Source: https://habr.com/ru/post/1680969/


All Articles