I am getting the time like 13.40, but i need to convert it to 1.40.. any one know, what is the best way to do this. i am using jquery to make time.
my code is :
var time = new Date(myDate);
var hours = time.getHours();
alert(hours);
2213 Hours Is 10:13 PM in Regular Time - Capitalize My Title.
if (hours > 12) {
hours -= 12;
}
Um, as simple as that.
Use the modulus
operator, %
for this
var input = "13.40";
var atoms = input.split(".");
var output = atoms[0] % 12 + "." + atoms[1];
output; // "1.40";
If you want to prefix with 0 then you can do this
var output = ("0" + atoms[0] % 12).slice(-2) + "." + atoms[1];
output; // "01.40";
If you want AM/PM as a suffix
var output = ("0" + atoms[0] % 12).slice(-2) + "." + atoms[1] +
(atoms[0] < 13 ? " AM" : " PM");
output; // "01.40 PM";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With