To Convert Unix timestamp to DateTime using jQuery and JS, we use the JavaScript date object and other date functions to get the time and date with months.
Here are 4 steps to convert timestamp to DateTime using jQuery and JS,
- Convert timestamp to milliseconds.
- Place the millisecond value as an argument in the date object.
- By using
toLocaleString()
function, we convert it to the human-readable date format.
Live Example of Convert Unix timestamp to DateTime jQuery
In the below example, we create a paragraph with an ID attribute to print the date.
Then we assign a timestamp value to a variable, we convert the timestamp to milliseconds by multiplying with 1000.
Now we pass the values as an argument to the JS Date object which helps to convert.
At last, JavaScript converts the Unix timestamp to date formatyyyy-mm-dd
by using toLocaleString()
function.
1 |
<p id="datetime"></p> |
1 2 3 4 5 6 |
<script type="text/javascript"> var unixtimestamp = 1639327366; var dt = eval(unixtimestamp*1000); var myDate = new Date(dt); document.getElementById('datetime').innerHTML = myDate.toLocaleString(); </script> |
See the Pen Convert Unix timestamp to DateTime jQuery by Bikash Panda (@phpcodertech) on CodePen.
Convert Unix timestamp to DateTime with Month name Using JavaScript
1 2 3 4 |
<input type="text" value="1639327366" id="timestamp"> <input type="button" id="convert" value="Convert" onclick="unixToDatetime();"> <p id="datetime"></p> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
function unixToDatetime(){ // Unixtimestamp var unixtimestamp = document.getElementById('timestamp').value; // Months array var months_arr = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']; // Convert timestamp to milliseconds var date = new Date(unixtimestamp*1000); // Year var year = date.getFullYear(); // Month var month = months_arr[date.getMonth()]; // Day var day = date.getDate(); // Hours var hours = date.getHours(); // Minutes var minutes = "0" + date.getMinutes(); // Seconds var seconds = "0" + date.getSeconds(); // Display date time in MM-dd-yyyy h:m:s format var convdateTime = month+'-'+day+'-'+year+' '+hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2); document.getElementById('datetime').innerHTML = convdateTime; } |
Live View
See the Pen Convert Unix timestamp to datetime with month name jQuery by Bikash Panda (@phpcodertech) on CodePen.
Code Highlights:
HTML: we create an input box where we put the Unix timestamp value. Then we create an HTML button where we call our JavaScript function.
SCRIPT: In the JS script, we create a function named unixToDatetime()
.
Take the input box value by using the input box’s ID attribute and JS getElementById
function.
We create an array with the month name to print the date with the month name. Now we convert the timestamp to a millisecond.
By using the JS date object we get the year, month, and date time. At last, we concatenate and get the complete date and time with the month name.
Please let me know if you are facing any issues.
Happy Coding..!