If you want to find the current time and timezone using JavaScript, you can use below example.
In this example, moment.js api has been used.
Code:
<!DOCTYPE html>
<html>
<title>TimeZone</title>
<script type="text/javascript" src="jquery-2.1.4.min.js"></script>
<script type="text/javascript" src="moment.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.4.0/moment-timezone.min.js"></script>
<script type="text/javascript" src="jstz-1.0.4.min.js"></script>
<script>
function timeFunction() {
var pay=moment.tz().zoneName();
var l=moment().utcOffset();
var q=moment.tz.Zone;
var timezone = jstz.determine();
var t=timezone.name();
var ty=moment().tz(t).format("h:mm:ss");
document.getElementById("mytime").value = ty;
document.getElementById("mytimezone").value = t;
}
</script>
<body>
<div id="time">
<table>
<tr>
<td>
TimeZone <input type="text" id="mytimezone">
</td>
</tr>
<tr>
<td>
Time <input type="text" id="mytime">
</td>
</tr>
<tr>
<td>
<button onclick="timeFunction()">Click for current time</button>
</td>
</tr>
</table>
</div>
</body>
</html>
0 Comment(s)