Switch to full style
JavaScript code examples
Post a reply

convert timestamp using javascript

Wed Jan 09, 2013 1:01 am

convert time-stamp to database format, UTC format and Local Time format
javascript code
<html>
<head>
<title>Convert timestamp using Javascript</title>

<style type="text/css">
div.divTime {
display: table-cell;
border:1px solid blue;
background:#54F3F1;
text-align: center;
vertical-align: middle;
width: 100px;
}
</style>
<script>
function convertTimestamp() {
document.getElementById('time').value =
Math.floor((new Date()).getTime() / 1000);
var timestampField = document.getElementById('time');
var timestampValue = parseInt(timestampField.value);

if(isNaN(timestampValue) || timestampValue != timestampField.value) {

document.getElementById('databaseFormat').innerHTML =
"Can't convert this input";
document.getElementById('localTime').innerHTML =
"Can't convert this input";
document.getElementById('UTCformat').innerHTML =
"Can't convert this input";
}
else {

var dataTime = new Date(timestampValue*1000);

document.getElementById('databaseFormat').innerHTML =
dataTime.getFullYear() + '-' + padding(dataTime.getMonth()+1, 2) +
'-' + padding(dataTime.getDate(), 2) + ' ' + padding(dataTime.getHours(), 2)
+ ':' + padding(dataTime.getMinutes(), 2) + ':' + padding(dataTime.getSeconds(), 2);

document.getElementById('localTime').innerHTML =
dataTime.toLocaleString();
document.getElementById('UTCformat').innerHTML =
dataTime.toUTCString();
}
}
function padding (myNumber, lenghtOfField, paddingding) {
var
dim = '',
s = myNumber;

if (typeof myNumber === 'number') {
dim = myNumber < 0 ? '-' : '';
s = Math.abs (myNumber).toString ();
}

if ((lenghtOfField -= s.length) > 0)
s = Array (lenghtOfField + 1).join (paddingding || '0') + s;

return dim + s;
}


</script>
</head>

<body>
<table border="1px" >
<tr>
<th>
Format
</th>
<th>
Value
</th>
</tr>
<tr>
<td>Time-stamp:</td>
<td>
<input id="time" type="text" size="50" value="1357688698" />
</td>
</tr>
<tr>
<td>Local-time:</td>
<td><div id="localTime">No Value</div></td>
</tr>
<tr>
<td>UTC</td>
<td><div id="UTCformat">No Value</div></td>
</tr>
<tr>
<td>Database</td>
<td><div id="databaseFormat">No Value</div></td>
</tr>
</table>
<input type="submit" value="Convert"
onclick="javascript:convertTimestamp();" />
</body>
</html>




Post a reply
  Related Posts  to : convert timestamp using javascript
 java.util Calendar To TimeStamp     -  
 How to Convert WMV to AVI on Mac     -  
 convert hexadecimal to decimal     -  
 Convert outputstream to inputstream     -  
 convert string into binary     -  
 convert XMLGregorianCalendar to GregorianCalendar     -  
 convert word, Excel to PDF     -  
 Convert To Upper Case in C++     -  
 convert to binary number     -  

Topic Tags

JavaScript Time