Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert 24 hr format to 12 hr format

This might be really easy but I can't figure it out. I am trying to convert the 24 hr time to a 12 hr time to display on the UI.

var hrs = '<%=Model.Scheduled.Value.Hour%>';
var hrs12 = hrs > 12 ? hrs - 12 : hrs;
$("#ScheduledHour").val(hrs12);

But the above is not working coz hrs is a string. Any suggestions on how to get this working?

like image 450
dotNetNewbie Avatar asked Jan 30 '26 10:01

dotNetNewbie


1 Answers

you can use parseInt():

var hrs = '<%=Model.Scheduled.Value.Hour%>';
hrs = parseInt(hrs, 10) // converts the value to an integer
var hrs12 = hrs > 12 ? hrs - 12 : hrs;
$("#ScheduledHour").val(hrs12);
like image 196
undefined Avatar answered Feb 02 '26 02:02

undefined



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!