Wed Jan 09, 2013 1:01 am
<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>
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.