I read timestamp from MySQL
db (is type timestamp
).
Like this: 2017-04-01 15:34:31
I want to format this date using jquery
and set up to some span
element.
I am new in jquery
and I don't know how to do it.
Thank you.
I read timestamp from MySQL
db (is type timestamp
).
Like this: 2017-04-01 15:34:31
I want to format this date using jquery
and set up to some span
element.
I am new in jquery
and I don't know how to do it.
Thank you.
Try the DATE_FORMAT(date,format)
function in MySQL
. Something like:
DATE_FORMAT(NOW(),'%m-%d-%Y')
would give you 04-01-2017. Then wrap that in a <span>
.
If you are using PHP then
strftime('%m-%d-%Y',$timestamp);
is an alternative. there are plenty of examples in Stack Overflow; Google will get you there.
If you would are receving the timestamp
on the client side you can Convert a Unix timestamp to time in JavaScript
// Create a new JavaScript Date object based on the timestamp
// multiplied by 1000 so that the argument is in milliseconds, not seconds.
var date = new Date(unix_timestamp*1000);
// Hours part from the timestamp
var hours = date.getHours();
// Minutes part from the timestamp
var minutes = "0" + date.getMinutes();
// Seconds part from the timestamp
var seconds = "0" + date.getSeconds();
// Will display time in 10:30:23 format
var formattedTime = hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);
To add the formatted text to a span
you could use javascript or jQuery
// Javascript
document.querySelector("span").text(formattedTime);
// jQuery
$("span").text(formattedTime);
For more information regarding the Date
object, please refer to MDN or the ECMAScript 5 specification.
Additionally:
Date(Date.parse(data.timestamp[i]))
This works assuming you are iterating through timestamps sent to the frontend from a Mysql query, which returns arrays. You could also write it like:
Date(Date.parse("2020-01-08 06:36:59"))