Total members 11892 |It is currently Sat Oct 12, 2024 2:06 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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>




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : convert timestamp using javascript
 java.util Calendar To TimeStamp     -  
 How to Convert WMV to AVI on Mac     -  
 Convert To Upper Case in C++     -  
 convert to binary number     -  
 convert pascal code to c ?     -  
 convert lowercase to uppercase     -  
 Convert String to Date     -  
 convert hexadecimal to decimal     -  
 Convert outputstream to inputstream     -  



Topic Tags

JavaScript Time






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
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