I’m making a local time watch in jQuery. I wrote some piece of code but it’s not working. Here is my code:
$(document).ready(function(){
function addZero(i) {
if (i <= 9) {
i = "0" + i;
}
return i;
}
var d = setInterval(function(){
var z = new Date();
var h = addZero(z.getHours());
var m = addZero(z.getMinutes());
var s = addZero(z.getSeconds();
var a = '';
if (h > 11 ) a = "PM"
else a = "AM"
if (h == 16) h = '0'+4
$('pre').html(h + ":" + m + ":" + s + " " + "a");
},1000);
});
You made some mistakes - check code
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
var d,z,h,m,s,a
function addZero(i) {
if (i <= 9) i = "0" + i;
return i;
}
d = setInterval( function(){
z = new Date();
h = addZero(z.getHours());
m = addZero(z.getMinutes());
s = addZero(z.getSeconds());
a = '';
if (h > 11 ) a = "PM"
else a = "AM"
if (h == 16) h = '0'+4
$('#timer').html(h + ":" + m + ":" + s + " " + a);
},1000);
});
</script>
<pre id="timer"></pre>
You have missed one )
$(document).ready(function(){
function addZero(i) {
if (i <= 9) {
i = "0" + i;
}
return i;
}
var d = setInterval(function(){
var z = new Date();
var h = addZero(z.getHours());
var m = addZero(z.getMinutes());
var s = addZero(z.getSeconds());
var a = '';
if (h > 11 ) {
a = "PM" ;
}
else {
a = "AM";
}
if (h == 16) {
h = '0'+4;
}
$('pre').html(h + ":" + m + ":" + s + " " + a);
},1000);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<pre></pre>
Please use below code in your code there is a function var d which is declared for setInterval but I didnt see this function as a called. so from where it will call. above answer is also fine and correct but global variable declaration is not needed here for d,z,h,m,s,a in below code. Local variable is also working.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
//var d,z,h,m,s,a
function addZero(i) {
if (i <= 9) i = "0" + i;
return i;
}
setInterval( function(){
var z = new Date();
var h = addZero(z.getHours());
var m = addZero(z.getMinutes());
var s = addZero(z.getSeconds());
var a = '';
if (h > 11 ) a = "PM"
else a = "AM"
if (h == 16) h = '0'+4
$('pre').html(h + ":" + m + ":" + s + " " + a);
},1000);
});
</script>
<pre id="timer"></pre>
Related
I am needing to convert 5 columns to TEXT before the program writes the data to it because Excel changes my 8 digit formatted date (MM/DD/YY) that gets outputted to those columns to the Date format (MM/DD/YYYY). The outputted date needs to stay in the 8 digit format in order to rerun it as the next input file to update the system. Any suggestions would be greatly appreciated. I have tried using:
ExcelSheet.ActiveSheet.Range("E:E").TextToColumns;
but it did not work. Here's all of the code:
<script>
function js_yyyy_mm_dd_hh_mm_ss () {
now = new Date();
year = "" + now.getFullYear();
month = "" + (now.getMonth() + 1);
if (month.length == 1) { month = "0" + month; }
day = "" + now.getDate();
if (day.length == 1) { day = "0" + day; }
hour = "" + now.getHours();
if (hour.length == 1) { hour = "0" + hour; }
minute = "" + now.getMinutes();
if (minute.length == 1) { minute = "0" + minute; }
second = "" + now.getSeconds();
if (second.length == 1) { second = "0" + second; }
return month + "" + day + "" + year.substring(2) + "" + hour + "" + minute + "" + second; }
function pad(num, len) { var str = '' + num; while (str.length < len) { str = '0' + str; } return str; } function writeToExcel() { try { var loanStr; var FullRecs; var passFail; var Processed = 0; var Rejected = 0; var D9ID; var time; var date; var datetime; var row = 0; var col; var str = document.getElementById("outstr").value; var headerStr = "|Account|Stop_Code_1|SC_1_Date|Stop_Code_2|SC_2_Date|Stop_Code_3|SC_3_Date|Lockout_Code|Lock_Date|Warning_Code|Warning_Date|"; var ExcelSheet = new ActiveXObject("Excel.Application"); var excelBook = ExcelSheet.Workbooks.Add; var RSheet = excelBook.Worksheets(1); ExcelSheet.Worksheets.Activate; ExcelSheet.ActiveSheet.Name = "Removals-" + js_yyyy_mm_dd_hh_mm_ss (); var outputdate = month + "-" + day + "-" + year.substring(2); var outputtime = hour + "-" + minute; var writefilepath = document.getElementById("writepath").value; var filepath = writefilepath.substring(0, writefilepath.lastIndexOf("\\") + 1); filepath = filepath + "DEL LOAN CODE Removal" + "_" + outputdate + "_" + outputtime + "_" + "OUTPUT" + ".xlsx"; date = month + "/" + day + "/" + year.substring(2); time = hour + ":" + minute + ":" + second; RSheet.Cells(1, 3).ColumnWidth = 15; RSheet.Cells(1, 4).ColumnWidth = 15; RSheet.Cells(1, 5).ColumnWidth = 15; RSheet.Cells(1, 6).ColumnWidth = 15; RSheet.Cells(1, 7).ColumnWidth = 15; RSheet.Cells(1, 8).ColumnWidth = 15; RSheet.Cells(1, 9).ColumnWidth = 15; RSheet.Cells(1, 10).ColumnWidth = 15; RSheet.Cells(1, 11).ColumnWidth = 15; RSheet.Cells(1, 12).ColumnWidth = 15; RSheet.Cells(1, 13).ColumnWidth = 15; ExcelSheet.ActiveSheet.Cells(1, 1).value = ""; var headings = headerStr.split("|"); for(var k = 0; k < (headings.length - 1); k ++ ) { ExcelSheet.ActiveSheet.Cells(7, k + 2).value = headings[k]; } FullRecs = str.split("~~"); for(var m = 0; m < (FullRecs.length - 1); m = m + 2) { loanStr = FullRecs[m].split("^"); for(var i = 0; i < (loanStr.length - 1); i ++ ) { var fieldStr = loanStr[i].split("|"); for(var j = 0; j < (fieldStr.length); j ++ ) { ExcelSheet.ActiveSheet.Cells(row + 8, j + 2).value = fieldStr[j]; } row = row + 1; } } for(var n = 1; n <= (FullRecs.length - 1); n = n + 2) { passFail = FullRecs[n].split("|"); D9ID = passFail[0]; Processed = Processed + Number(passFail[1]); Rejected = Rejected + Number(passFail[2]); } var total = Number(Processed) + Number(Rejected) ; ExcelSheet.ActiveSheet.Cells(2, 2).value = "USER"; ExcelSheet.ActiveSheet.Cells(2, 4).value = "SUMMARY"; ExcelSheet.ActiveSheet.Cells(3, 2).value = "DATE"; ExcelSheet.ActiveSheet.Cells(3, 3).value = date; ExcelSheet.ActiveSheet.Cells(4, 2).value = "TIME"; ExcelSheet.ActiveSheet.Cells(4, 3).value = time; ExcelSheet.ActiveSheet.Cells(2, 3).value = D9ID; ExcelSheet.ActiveSheet.Cells(3, 4).value = "PROCESSED ITEMS"; ExcelSheet.ActiveSheet.Cells(3, 5).value = + Processed; ExcelSheet.ActiveSheet.Cells(4, 4).value = "REJECTED ITEMS"; ExcelSheet.ActiveSheet.Cells(4, 5).value = + Rejected; ExcelSheet.ActiveSheet.Range("B2, B3, B4, D2, D3, D4, B7, C7, D7, E7, F7, G7, H7,I7,J7,K7,L7,M7").Font.Bold = true; for( var rb = 2; rb < 5; rb ++ ) { for( var cb = 2; cb < 6; cb ++ ) { RSheet.Cells(rb, cb).BorderAround(1).LineStyle = 1; } } for( var datarow = 7; datarow <= (7 + Number(total)); datarow ++ ) { for( var datacolumn = 3; datacolumn < 14; datacolumn ++ ) { RSheet.Cells(datarow, datacolumn).BorderAround(1).LineStyle = 1; } } ExcelSheet.ActiveSheet.Columns("A:P").HorizontalAlignment = 2; ExcelSheet.ActiveSheet.Range("C2", "C4").HorizontalAlignment = 3; ExcelSheet.ActiveSheet.Range("E2", "E4").HorizontalAlignment = 3; excelBook.SaveAs(filepath); ExcelSheet.Visible = false; ExcelSheet.UserControl = true; ExcelSheet.Application.Quit(); return true; } catch(e) { if(e.number == - 2146827284) { alert(e.description + ". Please close the document."); ExcelSheet.Application.Quit(); ExcelSheet.Quit(); ExcelSheet = null; return false; } } } </script>
Please Help!
I want to load Time Over, Time Elapsed dan Time Countdown on web page when student do the exam, I always failed to show it when the web page (exam page) load.
are my script right?
<?php $waktu=60;?>
<script type="text/javascript">
var TimeOver = true;
function getJam(Tanggal)
{
Jam = (Tanggal.getHours() < 10) ? "0" + Tanggal.getHours() + ":" : Tanggal.getHours() + ":";
Jam += (Tanggal.getMinutes() < 10) ? "0" + Tanggal.getMinutes() + ":" : Tanggal.getMinutes() + ":";
Jam += (Tanggal.getSeconds() < 10) ? "0" + Tanggal.getSeconds() : Tanggal.getSeconds();
return Jam;
}
function dispJam()
{
TglCur = new Date();
var a = document.getElementById("Watch");
a.value = getJam(TglCur);
var b = document.getElementById("TimeTaken");
b.value = getWaktu(TglCur,TglStart);
var c = document.getElementById("TimeLeft");
if ((Tgl.getTime() - TglCur.getTime()) <= 0)
{
if(TimeOver) TimeOverWarn();
c.value = "Habis";
}
else
{
c.value = getWaktu(Tgl,TglCur);
setTimeout("dispJam()",1000);
}
}
function getWaktu(Tgl,TglCur)
{
TmLf = Tgl.getTime() - TglCur.getTime();
TmLfHours = Math.floor(TmLf/3600000) ;
TmLfMinutes = Math.floor((TmLf%3600000)/60000);
TmLfSeconds = Math.round((TmLf%60000)/1000);
TmLfStr = (TmLfHours < 10) ? "0" + TmLfHours + ":" : TmLfHours + ":";
TmLfStr += (TmLfMinutes < 10) ? "0" + TmLfMinutes + ":" : TmLfMinutes + ":";
TmLfStr += (TmLfSeconds < 10) ? "0" + TmLfSeconds : TmLfSeconds;
return TmLfStr;
}
function TimeOverWarn()
{
alert("\n Maaf " + <?php echo $nama?> + " ....\n Waktu Anda Habis");
TimeOver = true;
return true;
}
Tanggal = new Date();
Tgl = new Date();
TglStart = new Date();
ArrayBulan = new Array("Januari","Pebruari","Maret","April","Mei","Juni","Juli","Agustus","September","Oktober","Nopember","Desember");
Tahun = Tanggal.getYear();
TglStr = Tanggal.getDate() + " " + ArrayBulan[Tanggal.getMonth()] + " " + Tahun;
Tgl.setTime(Tgl.getTime() + 60 * 60 * 1000);
</script>
<script language="javascript">
function selesai() {
document.ljkform.submit.click();
clearTimeout(timeID);
}
function timer() {
timeID=setTimeout("selesai()",60000 * <?php echo $waktu;?>);
}
</script>
I use these script in body load.
Here is my script to load it.
<body onLoad="dispJam();timer();">
<table>
<FORM NAME="User">
<tr><td><center>Sisa Waktu<br><INPUT TYPE="text" NAME="TimeLeft" id="TimeLeft" SIZE="8"></td></tr>
<tr><td><center>Waktu<br><INPUT TYPE="text" NAME="TimeTaken" id="TimeTaken" SIZE="8"></td></tr>
<tr><td><center>Sekarang Jam<br><INPUT TYPE="text" NAME="Watch" id="Watch" SIZE="8"></td></tr>
</FORM>
</table>
<form name="ljkform" method="post" action="hasil_tes.php?idtest=<?php echo $idtest;?>&idanggota=<?php echo $idanggota;?>">
<!-- script to show the question form -->
</form>
</body>
Many thanks before...
I've been given some JavaScript that creates a digital clock to go onto a webpage. This is working perfectly, however I'm trying amend it to wrap the am/pm suffix (or diem in this code) in span or bold tags so that I can style it differently to the rest of the time in the CSS.
I'm sure this would be really simple for someone that knows what they're doing but I'm really struggling. Any help would be appreciated, the JavaScript is below:
function renderTime() {
var currentTime = new Date();
var diem = "AM";
var h = currentTime.getHours();
var m = currentTime.getMinutes();
var s = currentTime.getSeconds();
setTimeout('renderTime()',1000);
if (h == 0) {
h = 12;
} else if (h > 12) {
h = h - 12;
diem="PM";
}
if (m < 10) {
m = "0" + m;
}
if (s < 10) {
s = "0" + s;
}
var myClock = document.getElementById('clockDisplay');
myClock.textContent = h + ":" + m + " " + diem;
myClock.innerText = h + ":" + m + " " + diem;
var diem = document.createElement('span');
}
renderTime();
You have a few options:
Use innerHTML instead of innerText. That allows you to insert HTML tags into the string:
myClock.innerHTML = h + ":" + m + " <span>" + diem + "</span>;
Use appendChild and leave out the diem at the end of the innerText:
myClock.innerText = h + ":" + m + " ";
var diemSpan = document.createElement('span');
diemSpan.innerText = diem;
myClock.appendChild(diemSpan);
Finally, you could put a clockDisplay and a diem span within your markup and access them appropriately:
Markup:
<div id="clockDisplay">
<span id="clockDisplay-time"></span>
<span id="clockDisplay-suffix"></span>
</div>
Script:
document.getElementById("clockDisplay-time").innerText = h + ":" + m;
document.getElementById("clockDisplay-suffix").innerText = diem;
Try this:
var myClock = document.getElementById('clockDisplay');
var suffix = document.createElement('span');
suffix.innerText = diem;
myClock.innerText = h + ":" + m + " ";
myClock.append(suffix);
Or, if you want to be more efficient (pure DOM manipulation):
var myClock = document.getElementById('clockDisplay');
var suffix = document.createElement('span');
suffix.append(document.createTextNode(diem));
myClock.append(document.createTextNode(h + ":" + m + " "));
myClock.append(suffix);
Here's a JSfiddle for you. It should work the way you wanted.
A solution is to create a different span for the time and your "diem", like this:
HTML:
<p><span id="time"><!-- time will go here --></span><span id="diem"><!-- "diem" will go here --></span></p>
Then access them separately. You can style them differently too in CSS, like this:
CSS:
span#diem {
font-weight: bold;
// your styles
}
JS:
window.onload = function() {
renderTime();
}
function renderTime() {
// Insert time calculations here
var time_span = document.getElementById('time');
time_span.innerText = h + ":" + m;
var diem_span = document.getElementById("diem");
diem_span.innerText = " " + diem;
}
See the Fiddle for the complete code in renderTime().
Just need a quick tweak if thats ok. I got this code from the web and would just like to add an am/pm to be displayed at the end of the clock/time. Many thanks...
<script>
function startTime()
{
var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
// add a zero in front of numbers<10
m=checkTime(m);
s=checkTime(s);
document.getElementById('txt').innerHTML=h+":"+m+":"+s;
t=setTimeout(function(){startTime()},500);
}
function checkTime(i)
{
if (i<10)
{
i="0" + i;
}
return i;
}
</script>
<div id="txt"></div>
Or, if you also want it displayed in 12-hour format:
<script type="text/javascript">
function startTime()
{
var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
// add a zero in front of numbers<10
m=checkTime(m);
s=checkTime(s);
var hd=h;
document.getElementById('txt').innerHTML=(hd=0?"12":hd>12?hd-12:hd)+":"+m+":"+s+" "+(h<12?"AM":"PM");
t=setTimeout(function(){startTime()},500);
}
function checkTime(i)
{
if (i<10)
{
i="0" + i;
}
return i;
}
</script>
This should do the trick.
function startTime() {
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
// add a zero in front of numbers<10
m = checkTime(m);
s = checkTime(s);
var suffix;
if (h >= 12 && h < 24){
suffix = "pm";
} else {
suffix = "am";
}
document.getElementById('txt').innerHTML = h + ":" + m + ":" + s + " " + suffix;
t = setTimeout(function () {
startTime()
}, 500);
}
function checkTime(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
I have code that will generate a random unique id.. but is there a way I can edit this code so that it grabs a date in a specific way like yyyy-mm-dd-0001. the last 4 digits I want it to add 1 each time the generateid button is clicked. so it will change to 0002. Here is the current code I have. Is there a function that can grab the date automatically?
var counter = 0000;
function Counter() {
if((document.getElementById("generateid").clicked == true)
{
Counter++
return counter;
}
}
function Month() {
var m = new Date();
var mm = m.getMonth() + 1;
if (mm < 10) {
mm = '0' + mm;
return mm;
}
}
function Year() {
var y = new Date();
var yy = y.getFullYear();
return yy;
}
function Day() {
var d = new Date();
var dd = d.getDate();
return dd;
}
//generate id
function guidGenerator() {
var theID = (Year() + "-" + Month() + "-" + Day() + "-" + Counter);
return theID;
}
function generateID() {
var TheTextBox = document.getElementById("generateidtxt");
TheTextBox.value = TheTextBox.value + guidGenerator();
document.getElementById("generateid").disabled = true;
}
You can use the following object:
var idGenerator = {
seq: 0,
generateId: function () {
this.seq++;
return (new Date()).toISOString().substring(0, 10) + '-' + ('000' + this.seq).substr(-4)
}
}
after declaration like this, try
function generateID() {
var TheTextBox = document.getElementById("generateidtxt");
TheTextBox.value = TheTextBox.value + idGenerator.generateId();
document.getElementById("generateid").disabled=true;
}
If you are asking for a way to keep track of how many times an ID is generated by all your site visitors using javascript alone then, no it is not possible without tying in some back end to keep track. However, the following code will do what you ask per visitor.
jsfiddle
var ttlIds = 0;
function guidGenerator() {
var S4 = function () {
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}
return (S4() + S4() + S4());
}
function generateID() {
var TheTextBox = document.getElementById("generateidtxt");
TheTextBox.value = TheTextBox.value + guidGenerator().toString().toUpperCase();
//document.getElementById("generateid").disabled=true;
ttlIds++;
if(ttlIds < 10){
ttlIds_formatted = '000'+ttlIds;
}else if(ttlIds < 100){
ttlIds_formatted = '00'+ttlIds;
}else if(ttlIds < 1000){
ttlIds_formatted = '0'+ttlIds;
}
d = new Date();
var funkydate = d.getFullYear() +'-' + (d.getMonth()+1) + '-' + d.getDate() + '-' + ttlIds_formatted;
document.getElementById("funkydate").value = funkydate;
}