简体   繁体   中英

How to convert date into timeago

I am trying to convert a date field in a "timeago" format using jquery.timeago.js

$("time.timeago").timeago();

var userSpan = document.createElement("span");
userSpan.setAttribute("class", "text-muted");
userSpan.appendChild(document.createTextNode(message.usernameSender +" | "));
var timeTag = document.createElement("time");
timeTag.setAttribute("class", "timeago");
timeTag.setAttribute("datetime",document.createTextNode(message.date));
userSpan.appendChild(timeTag);

This javascript will genearate the following code

<span class="text-muted">user1 | <time class="timeago" datetime="[object Text]"></time></span>

My problem is that the result of datetime is [object Text]

What am I missing?

Thanks

My problem is that the result of datetime is [object Text]

Well, yes, because you tell JavaScript to do that:

timeTag.setAttribute("datetime",document.createTextNode(message.date));

Try

timeTag.setAttribute("datetime", message.date);

Attribute values are strings, whereas DOM nodes (including text nodes) are objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM