JavaScript - Convert An Integer To Time Format.
Does JavaScript have an easy of converting an ordinary integer like 135 into a time format like 02:15? I'm working on a timer that handles the backend with just a plain integer that counts down every second but it needs to output looking like a clock.
Similar TutorialsHi Guys, I'm looking for a way to convert a number in to currency format so 10000000 is 10,000,000 etc... i found this code and was hoping someone could advise how to tweak it so that it works automatically without having to hit a button i.e. I will be passing a value dynamically to the code and want it evaluated without the user clicking anything if the code was like var="10000000" <-the number I dynamically pass in and output 10,000,000 You probably guessed Javascript aint my forte. Thanks http://www.designerwiz.com/JavaScrip...ncy_format.htm Code: <!-- // == This Script Free To Use Providing This Notice Remains == // // == This Script Has Been Found In The http://www.DesignerWiz.com Javascript Public Archive Library == // // == NOTICE: Though This Material May Have Been In A Public Depository, Certain Author Copyright Restrictions May Apply == // --><script language="JavaScript" type="text/javascript"> <!-- Begin function checkNum(data) { // checks if all characters var valid = "0123456789."; // are valid numbers or a "." var ok = 1; var checktemp; for (var i=0; i<data.length; i++) { checktemp = "" + data.substring(i, i+1); if (valid.indexOf(checktemp) == "-1") return 0; } return 1; } function dollarAmount(form, field) { // idea by David Turley Num = "" + eval("document." + form + "." + field + ".value"); dec = Num.indexOf("."); end = ((dec > -1) ? "" + Num.substring(dec,Num.length) : ".00"); Num = "" + parseInt(Num); var temp1 = ""; var temp2 = ""; if (checkNum(Num) == 0) { alert("This does not appear to be a valid number. Please try again."); } else { if (end.length == 2) end += "0"; if (end.length == 1) end += "00"; if (end == "") end += ".00"; var count = 0; for (var k = Num.length-1; k >= 0; k--) { var oneChar = Num.charAt(k); if (count == 3) { temp1 += ","; temp1 += oneChar; count = 1; continue; } else { temp1 += oneChar; count ++; } } for (var k = temp1.length-1; k >= 0; k--) { var oneChar = temp1.charAt(k); temp2 += oneChar; } temp2 = "$" + temp2 + end; eval("document." + form + "." + field + ".value = '" + temp2 + "';"); } } // End --></script> <center> <form name=commaform>Enter a number then click the button: <input type=text name=input size=10 value=""> <input type=button value="Convert" onclick="dollarAmount(this.form.name, 'input')"> <br><br> or enter a number and click another field: <input type=text name=input2 size=10 value="" onBlur="dollarAmount(this.form.name, this.name)"> </form> Hi - I have a date in my javascript like this: Mon Dec 12 00:00:00 UTC+0430 2011 I need to convert it to a date without the offset (the +0430). I actually changed my computer's timezone to generate this offset - we have uses placing orders on our site who don't use UTC and it's throwing our orders out as they need to select a delivery date but it's passing the date with the offset. How do I convert a date to UTC time? Thanks Hello, I want to change time format from 12 to 24 hours and remove AM and PM in this script. Anybody can help me? Code: <style type="text/css"> /*<![CDATA[*/ #clock { font-family: "Trebuchet MS", Arial, Helvetica, sans-serif; font-size:16px; color:#030; padding-top: 24px; text-align:center; } /*//]]>*/ </style> <script type='text/javascript'> //<![CDATA[ var dayarray=new Array('Svē','Pir','Otr','Tre','Cet','Pie','Ses'); var montharray=new Array('01','02','03','04','05','06','07','08','09','10','11','12'); function getthedate(){ mydate=new Date(); year=mydate.getUTCFullYear(); day=mydate.getDay(); month=mydate.getMonth(); daym=mydate.getDate(); if(daym<10) { daym='0'+daym; } hours=mydate.getHours(); minutes=mydate.getMinutes(); seconds=mydate.getSeconds(); dn='AM'; if(hours>=12) { dn='PM'; } dn='AM'; if(hours>=12) { dn='PM'; } if(hours>12){ hours=hours-12; } if(hours==0){ hours=12; } if(minutes<=9) { minutes='0'+minutes; } if(seconds<=9) { seconds='0'+seconds; } cdate=dayarray[day]+', '+daym+'-'+montharray[month]+'-'+year+', '+hours+':'+minutes+':'+seconds+' '+dn; document.getElementById('clock').firstChild.nodeValue=cdate; setTimeout('getthedate()',1000); } window.onload=function() { getthedate(); } //]]> </script> Thanks. Hello, I would like to learn if there is way to change a different format every time the user want as example is Code: London 12:00:33 - Friday 18/03/2011 Thanks How would I get the local time of a pc using JavaScript in dd-mm-yyyy format ?
Frustrated by the fact that you don't have the integer division operator in JavaScript? Here's a surprisingly little known method: parseInt(19/4) = 4 Now when a VBguru or vbscripter says "ha ha, I can integer divide and you can't!" You now know better. Can someone point me in the right direction for using Javascript to increase a number over time without refreshing the page? I also need it to continually go up, and not start at 0 on every refresh. Is this possible? Blockis I have strings's that are causing problems when being sent to a PHP script via JavaScript. (not all of it is getting sent) i.e. This is someone's line of text & it's rather long. I wish to convert the whole string to some other format say like the spaces to %20 and and the & to & I think, and so on... Then once the PHP script $_GET's the string it converts it back to the original. PLEASE NOTE: I wish to use JavaScript, not JQuery. Hi all, this is my first post to this great useful forum. I have a javascript 2d array [x,y] that contains integer values like this: [0,0], [0,1], [0,2], [0,3], [1,0], [1,1], [1,2], [2,0], [2,1], [2,2], [0,-1], [0,-2], [0,-3], [1,-1], [1,-2], [2,-3] my question is how to get the max,min y value for each x. for the sample above it should give me another array with the following results [0,3],[0,-3],[1,2],[1,-2],[2,2],[2,-3] your help is highly appreciated Regards Omran In some cases toPrecision(X) will result in an integer value, which is the expected result. However, it is typical in some fields to always follow the integer value with a decimal and zero (i.e 522 --> 522.0). Technically, this would be incorrect since it would imply an extra digit of precision that does not exist. However, this is often disregarded, provided there are sufficient number of digits (i.e. precision of 5+). That said, is there any way to force an integer result to have a trailing ".0"? Hi, I was hoping you could help me, I was wanting to write text to the screen but without re-writing the whole screen (e.g. document.write();. I wish to take text and put it in a function, modify it, then display it. So far I have: Code: <script type = "text/javascript"> function calculate(text){ var x = "10.0"; var gpa = text.replace(/0.0/g,x); document.getElementById("text").innerText=gpa; } </script> <p id = text >Your Grade Point Average (GPA) is: 0.0</p> <button type="button" onclick="calculate(text)">Calculate</button> Your help would be really appreciated Hi all, I am having a problem converting a physical string to a integer. I have a dropdown list of a few items but are numbers though in chracters. I ned to output the value and the text of the selected item. As I cannot use a if-else statement, any help would be great. Code: <select id="std_num" onchange="displayValue()"> <option value="Two">Two</option> <option value="Zero">Zero</option> <option value="Five">Five</option> <option value="Three">Three</option> <option value="Two">Two</option> <option value="Zero">Zero</option> </select> Code: function displayValue() { alert(document.getElementById('std_num').value); var c = (document.getElementById('std_num').value); } OVERVIEW of main webpage: when a user logs in, they will be directed to the main page of the website; and will see images displayed according to a value that was stored in the database. the main page preloads an image array: Code: <script type="text/javascript"> var img_FILE = [ 'images/house.png', 'images/car.png', 'images/boat.png' ]; </script> easy enough the database holds a column of integers that are used to access the proper image in the array. i can successfully pull the integer value i need from the db. what i want to do is when the main page loads, i want the value that was pulled from the database to be used to display the correct image. example: lets say i pulled the value 2 from the db. then i want to display img_FILE[2] on the main page when the page "loads". Code: <body> <div id="quadrantOne"> <img id="firstImg" src="images/blankImg.png" width="190" height="235" /> <script type="text/javascript"> $.post('imageScript.php', 'data=0', function(data,status){ if (status == 'success') { imgValue = data } else { imgValue = 'error' } document.getElementById('firstImg').src=img_FILE[imgValue]; }); </script> </div> </body> i debugged imgValue and it is correctly assigned the value coming from the db. but..........the image won't appear on the main page. any ideas how to get this to work? thanks, Paul Williams Hi Code: var err_str=""; function validate_grid(value,id,ind) { $("#result").html(" ").show(); if(ind==1 || ind==2) { var patt=/^([0][0-9]|[1][0-9]|[2][0-3])[:]{1}[0-5][0-9]$/; if(!patt.test(value)) { mygrid.setCellTextStyle(id,ind,"background-color:yellow;"); if(err_str!="") err_str+="\n"+(ind+1)+".Enter numbers and : only.."; else err_str=(ind+1)+".Enter numbers and : only"; return false; } else { mygrid.setCellTextStyle(id,ind,"background-color:white;"); return true; } } } alert(err-str); here ind==1 is from_time and ind==2 is to_time,i need to validate time as to_time must be always greater than from-time,if condition fails an alert msg should populate.time format is[05:00]as it is in string format im unable to do the validation for it.(ex:from-time=08:00 to-time=07:59 condition fails) thank you. hi im using dhtml xgrid where i need to do validation for time ,(from-time and to-time i.e ind==1 and ind==2 ) to-time must be greater than from-time else i need to alert a message to-time must be greater than from-time. can any one send me the code for it time format is[05:00]. Code: var err_str=""; function validate_grid(value,id,ind) { $("#result").html(" ").show(); if(ind==1 || ind==2) { var patt=/^([0][0-9]|[1][0-9]|[2][0-3])[:]{1}[0-5][0-9]$/; if(!patt.test(value)) { mygrid.setCellTextStyle(id,ind,"background-color:yellow;"); if(err_str!="") err_str+="\n"+(ind+1)+".Enter numbers and : only.."; else err_str=(ind+1)+".Enter numbers and : only"; return false; } else { mygrid.setCellTextStyle(id,ind,"background-color:white;"); return true; } } } Hello im using dhtmlx grid in this cells i have from time and to time (i.e ind==1 and ind==2) i need to validate time where to-time must be greater than from-time else it should alert an message,i have writeen a normal Regular expression for the time validation.as the time is i string format i.e[05:00],please can any one send me code for that. Code: var err_str=""; function validate_grid(value,id,ind) { if(ind==1 || ind==2) { var patt=/^([0][0-9]|[1][0-9]|[2][0-3])[:]{1}[0-5][0-9]$/; if(!patt.test(value)) { mygrid.setCellTextStyle(id,ind,"background-color:yellow;"); if(err_str!="") err_str+="\n"+(ind+1)+".Enter numbers and : only.."; else err_str=(ind+1)+".Enter numbers and : only"; return false; } else { mygrid.setCellTextStyle(id,ind,"background-color:white;"); return true; } } } I have some asp:radiobutton lists that need to update a label with the sum of their values each time a user selects a new value. I am brand new to javascripting and would like some insight on how to get this done. I have inserted my code below. Code: <asp:Label runat="server" Text="Greeting:" /> <asp:Label runat="server" ForeColor="Red" ID="lbl_GreetingScore" Text="0" /> <asp:RadioButtonList ID="rdb1_1" runat="server" RepeatDirection="Horizontal"> <asp:ListItem Selected="True" Text="N/A" Value="4" /> <asp:ListItem Text="Yes" Value="4" /> <asp:ListItem Text="No" Value="0" /> </asp:RadioButtonList> Well I have an array and a button to select a random integer from the array, how do I make it so that it selects every integer once til all are selected, then it starts over again? For example, an array has these: A B 1 2 Q F So you press the button a number of times, and you get: Q B 1 F A 2 Instead of: 1 Q A 1 B A |