Showing posts with label JavaScript Tutorial. Show all posts
Showing posts with label JavaScript Tutorial. Show all posts

How to get a timestamp in JavaScript

Below are the ways to get a timestamp in Javascript.

Date.now returns the UTC timestamp in milliseconds.

If you are using IE you can use the below code.

if (!Date.now) {
    Date.now = function now() {
        return new Date().getTime();
    };
}
To convert the timestamp in seconds use below code

Math.floor(Date.now() / 1000)

JavaScript: Dynamically Add/Remove rows in HTML table

This article shows you how to add rows to a HTML table dynamically, using DHTML and JavaScript - without requiring a round trip to the web server. I used this technique in a project to build a data entry grid, where the users wanted the flexibility to add or remove rows easily before doing a final submit/save. 
Source Code:

<HTML>
<HEAD>
     <TITLE> Dynamically Add/Remove rows in HTML table TITLE>
     <SCRIPT language="javascript">
          FUNCTION addRow(tableID) {
 
               VAR table = document.getElementById(tableID);
 
               VAR rowCount = table.rows.length;
               VAR row = table.insertRow(rowCount);
 
               VAR cell1 = row.insertCell(0);
               VAR element1 = document.createElement("input");
               element1.type = "checkbox";
               cell1.appendChild(element1);
 
               VAR cell2 = row.insertCell(1);
               cell2.innerHTML = rowCount + 1;
 
               VAR cell3 = row.insertCell(2);
               VAR element2 = document.createElement("input");
               element2.type = "text";
               cell3.appendChild(element2);
 
          }
 
          FUNCTION deleteRow(tableID) {
               TRY {
               VAR table = document.getElementById(tableID);
               VAR rowCount = table.rows.length;
 
               FOR(VAR i=0; i<rowCount; i++) {
                    VAR row = table.rows[i];
                    VAR chkbox = row.cells[0].childNodes[0];
                    IF(NULL != chkbox && TRUE == chkbox.checked) {
                         table.deleteRow(i);
                         rowCount--;
                         i--;
                    }
 
               }
               }CATCH(e) {
                    ALERT(e);
               }
          }
 
     SCRIPT>
HEAD>
<BODY>
 
     <INPUT type="button" value="Add Row" onclick="addRow('dataTable')" />
 
     <INPUT type="button" value="Delete Row" onclick="deleteRow('dataTable')" />
 
     <TABLE id="dataTable" width="350px" border="1">
          <TR>
               <TD><INPUT type="checkbox" NAME="chk"/>TD>
               <TD> 1 TD>
               <TD> <INPUT type="text" /> TD>
          TR>
     TABLE>
 
BODY>
HTML>

How to create a simple JavaScript countdown timer? |A Simple JavaScript Countdown - JavaScript / Java

This JavaScript tutorial shows up the algorithm to make a simple countdown timer on your web pages, only with a few code lines.

<script type="text/javascript">
var millisecs_per_day=86400000
// set countdown time in milliseconds
// put desired day as arguments to Date.UTC
// in the order:
// year, month (remember January is 0), day of month, offset from GMT
// NOTE: we are using the offset because that represents midnight
// (beginning of the day) in a specific timezone
var countdown_time=Date.UTC(2008,7,8,-8);
// get the current time and convert to milliseconds
var now=new Date();
var now_millisecs=now.valueOf();

var day_cnt= Math.ceil(( countdown_time - now_millisecs)/86400000 )

// display the number of days left (or since)
if ( day_cnt > 1 )
{
// multiple days to go
document.write( day_cnt + " days to go")
}
else if ( day_cnt == 1 )
{
// one day to go
document.write( day_cnt + " day to go")
}
else if ( day_cnt == 0 )
{
// it's today
document.write( "today")
}
else if ( day_cnt == -1 )
{
// one day ago
document.write( day_cnt + " day ago")
}
else
{
// multiple days ago
document.write( day_cnt + " days ago")
}
</script>