0

I am trying to convert a String that consist of "AM" or "PM" into a 24 hours format. I have tried using formatDate but couldn't able to get the outcome that I would like. The following are examples of the values that I am trying to convert from String into a 24 hours format String.

String (Before) String (After)
10:00AM 10:00
12:00PM 12:00
02:00PM 14:00
04:00PM 16:00

Is using formatDate a recommended approach in this case or if not what other possible methods or way am I able to convert this String into the format above without complicating the codes?

Nic
  • 127
  • 1
  • 11
  • Does this answer your question? [convert 12-hour hh:mm AM/PM to 24-hour hh:mm](https://stackoverflow.com/questions/15083548/convert-12-hour-hhmm-am-pm-to-24-hour-hhmm) – Alan Yu Dec 06 '21 at 14:02
  • This is making use of JQuery but if I am just using Angular libraries is there a way for this to be done? – Nic Dec 06 '21 at 14:03
  • you can use moment if you don't have restriction – Gurkan İlleez Dec 06 '21 at 14:06
  • @Gurkanİlleez How can I make use of moment in here since I am using datatype String in both the before and after value in here? – Nic Dec 06 '21 at 14:10
  • @Gurkanİlleez, moment is discontinued (probably day.js would be a better option).From the moment developers themselves:"we would like to discourage Moment from being used in new projects going forward" Considering using Moment in your project? There may be better modern alternatives. For more details and recommendations, please see Project Status in the docs. https://momentjs.com/docs/#/-project-status/ – Juan Vicente Berzosa Tejero Dec 06 '21 at 14:10
  • Thank you for your information @Juan Vicente Berzosa Tejero – Gurkan İlleez Dec 06 '21 at 14:16

2 Answers2

0

By using javascrpit, you can also convert "AM" or "PM" into a 24 hours format :

var time = $("#starttime").val();
var hours = Number(time.match(/^(\d+)/)[1]);
var minutes = Number(time.match(/:(\d+)/)[1]);
var AMPM = time.match(/\s(.*)$/)[1];
if(AMPM == "PM" && hours<12) hours = hours+12;
if(AMPM == "AM" && hours==12) hours = hours-12;
var sHours = hours.toString();
var sMinutes = minutes.toString();
if(hours<10) sHours = "0" + sHours;
if(minutes<10) sMinutes = "0" + sMinutes;
alert(sHours + ":" + sMinutes);
  • This is basically what Alex is suggesting above in the other link. Please do not copy and paste other solutions directly. – Nic Dec 06 '21 at 14:15
  • 1
    Please adhere @Nic comment – Raushan Kumar Dec 06 '21 at 16:59
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 06 '21 at 17:00
  • Correct me if im wrong but isnt this jquery syntax? – Tommi Sep 20 '22 at 08:00
0

You can use Momentjs to conver "AM" or "PM" into a 24 hours format.

console.log(moment("10:00 AM", "h:mm A").format("HH:mm"));
console.log(moment("12:00 PM", "h:mm A").format("HH:mm"));
console.log(moment("2:00 PM", "h:mm A").format("HH:mm"));
console.log(moment("4:00 PM", "h:mm A").format("HH:mm"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
Mohamed Fathy
  • 126
  • 1
  • 8