invitation india
UnexpectedWeb
Connect

Popular Technology Blog where you'll find some interesting things around the web, that you never knew existed.

link Link copied

Convert a specific date and time into UNIX epoch time (JS)

Share on: link Link copied
Epoch time also known as Unix time or POSIX time, Defined as the number of seconds that have elapsed since midnight, 1 January 1970 (UTC). This tool is useful to convert a specific date and time into UNIX epoch time. Unix time also can checked by typing date +%s on the command line on Unix systems.


UNIX Epoch Time Convertor

The web tool has been developed using JavaScript, It's helps to convert human readable date and time into Epoch Time.



Javascript Code

Body


 Date:  <input id="t"type="date" name="bday">

 Time: <input type="time" id="myTime" value="22:15:00">

<button onclick="myFunction()">Get EPOCH Time</button>

<p id="display"></p>


Javascript

<script>
function myFunction() {
    var t = document.getElementById("t").value;  // Get Date input value
    var d = new Date(t);
    var n = d.getTime();  // Return the number of milliseconds since 1970/01/01
    var utd = n/1000;  // Convert into seconds
 
    var x = document.getElementById("myTime").value; // Get Time input value
    var times = x.split(":");
    var hour = times[0];
    var minutes = times[1];
    var o = (parseInt(hour, 10) * 3600) + (parseInt(minutes, 10) * 60);  // Convert time into seconds

    var tz = d.getTimezoneOffset();  // Timezoneoffset is the difference, in minutes, between UTC and local time
    var tzsec = tz*60;  // Convert time into seconds

    var epoch = utd+o+tzsec;  // Number of seconds that have elapsed since midnight, 1 January 1970
 
    document.getElementById("demo").innerHTML = epoch; // Print EPOCH Time
  }

</script>




No comments:

Powered by Blogger.