c# - How to convert Hour to 12 HRS / 24 HRS Format with javascript -


i convert hour 12 hrs / 24 hrs format way in c# not being able same in javascript.

my c# code formatting 12 hrs / 24 hrs format below

for (int = 0; <= 15; i++) {     strhrs.add((i == 0 ? "00" : convert.todatetime(timespan.fromhours(i).tostring()).tostring("hh")),         (i == 0 ? "00" : convert.todatetime(timespan.fromhours(i).tostring()).tostring("hh"))); } 

i got js code looks fine

var timestring = "18:00:00"; var h = +timestring.substr(0, 2); var h = h % 12 || 12; var ampm = h < 12 ? "am" : "pm"; timestring = h + timestring.substr(2, 3) + ampm; 

i have show 12 hrs format in dropdown text have show 24 hrs format in dropdown value.
how apply in case not sure. appreciated. thanks

edit

        function convertdateto12hrs(timestr, is24) {             if (is24) {                 var bighrs = timestr.tostring().length == 1 ? "0" + timestr : timestr;                 return bighrs; // return if is24 true             }             var h = "" + (+timestr > 12 ? timestr - 12 : timestr);             return h.length == 1 ? "0" + h : h;         } 

calling

convert("2", true); // "02" convert("18", false); // "06" 

base on comment on post, create function return both.

function convert(timestr, is24) {     if (is24) return timestr; // return if is24 true     var h = "" + (+timestr  > 12 ? timestr - 12 : timestr);     return h.length == 1 ? "0" + h : h; } 

now can call

convert("18", true); // "18" convert("18", false); // "06" 

Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -