简体   繁体   English

使用moment.js在HTML5中插入日期 <time> 元件

[英]Use moment.js to insert date inside HTML5 <time> element

I want to use moment.js to insert a backdated RFC 3339 timestamp inside an HTML5 <time> tag, so it renders into the HTML this: 我想使用moment.js在HTML5 <time>标记内插入一个回溯的RFC 3339时间戳,因此它将呈现为HTML:

<time datetime="2016-01-14T13:22:11-08:00">Lorem Ipsum</time>

With moment.js, my .js is working correctly to generate the timestamp: 使用moment.js,我的.js正常工作以生成时间戳:

document.getElementById('fourDaysEarlier').innerHTML = moment().subtract(4, 'days').format("YYYY-MM-DDTHH:mm:ssZ")

which I can successfully write into the document with innerHTML like this: 我可以用innerHTML成功写入文档,如下所示:

<span id="fourDaysEarlier"></span>

But how do I insert the timestamp inside the <time> tag, like this?? 但是如何在<time>标签内插入时间戳,就像这样?

<time datetime="wantToInsertTimeStampHere">Lorem Ipsum</time>

thanks in advance! 提前致谢!

you can just use vanilla javascript setAttribute() method like 你可以像使用vanilla javascript setAttribute()方法一样

    document.getElementById('fourDaysEarlier').setAttribute("datetime", moment().subtract(4, 'days').format("YYYY-MM-DDTHH:mm:ssZ"));

or if you are using jQuery - attr() method 或者如果您使用的是jQuery - attr()方法

    $('#fourDaysEarlier').attr("datetime", moment().subtract(4, 'days').format("YYYY-MM-DDTHH:mm:ssZ") )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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