1

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.

Steve Danner
  • 21,818
  • 7
  • 41
  • 51
chastifr
  • 23
  • 3

3 Answers3

0

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.

Peter Smith
  • 5,528
  • 8
  • 51
  • 77
0

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:

MicFin
  • 2,431
  • 4
  • 32
  • 59
0
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"))