For this task, I have used & extended a jquery plugin its jquery.maskedinput-1.2.2.js. You can see its more versions here:
http://plugins.jquery.com/node/104/release
It has its support for number masking like "99:99". To make it more meaningful with time mask in 12 hours AM/PM format, I have added some definitions like follows:
$.mask.definitions['g']="[ ]";
$.mask.definitions['h']="[aApP]";
$.mask.definitions['i']="[mM]";
$.mask.definitions['2']="[0-1]";
$.mask.definitions['6']="[0-5]";
Then the mask expression will be:
myMask = "29:69ghi";
$('#textBoxId').mask(myMask);
There, user will be forced to input a 12 hours format time like "11:19 AM". You can also, make your own definitions & use it as you like.
If you are having trouble to convert a 24 hours format into 12 hours am-pm format, then the following code snippet will help you to do this, very simple:
var time24HoursFormat = "17:30";//24 hours time to convert
var dateTime = new Date();
var splitted24Time = time24HoursFormat.split(":");
dateTime.setHours(splitted24Time[0].toString(),splitted24Time[1].toString());
alert(dateTime.toLocaleTimeString());
//give alert with 12 hours format time.
Subscribe to:
Post Comments (Atom)










0 comments:
Post a Comment