Total members 11380 |It is currently Sat Nov 22, 2014 11:07 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R

All times are UTC [ DST ]



  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>


_________________
Please recommend / share my post if you found it helpful.


Author:
Mastermind
User avatar Posts: 2694
Have thanks: 73 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 outputstream to inputstream     -  
 convert string into binary     -  
 convert word, Excel to PDF     -  
 convert XMLGregorianCalendar to GregorianCalendar     -  
 Convert To Upper Case in C++     -  
 convert to binary number     -  
 convert pascal code to c ?     -  

All times are UTC [ DST ]



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