javascript - Javascript 从 HTML 日期字符串中获取日期
问题描述
我有一个包含日期的 div,如下所示:
<div id="am-event-sub-info">
<div> February 27, 2021 11:30 am - 1:00 pm</div>
</div>
我试图从这个字符串中提取日期然后得到日期?
解决方案
我希望你不介意 2021 年 2 月 27 日是星期六……
console.log(
new Intl.DateTimeFormat('en-US', { weekday: 'long'}).format(
new Date(
document.querySelector('#am-event-sub-info > div')
.innerHTML
.split(' - ')[0] // only interested in the "February 27, 2021 11:30 am" part
)
)
);
<div id="am-event-sub-info">
<div> February 27, 2021 11:30 am - 1:00 pm</div>
</div>
更新:在 div 中添加日期之后的“天”
var day = new Intl.DateTimeFormat('en-US', { weekday: 'long'}).format(
new Date(
document.querySelector('#am-event-sub-info > div')
.innerHTML
.split(' - ')[0] // only interested in the "February 27, 2021 11:30 am" part
)
);
document.querySelector('#am-event-sub-info').innerHTML += '<div class="day">'+day+'</div>';
.day { color: green; }
<div id="am-event-sub-info">
<div> February 27, 2021 11:30 am - 1:00 pm</div>
</div>
推荐阅读
- tensorflow - 如何向神经网络添加额外的数据?
- java - 我不明白私有访问修饰符的使用
- memory - 如何检查 x86 PTE 在 Linux 中是否有效?
- django - django (DRF) 的 update_or_create 中的使用条件
- vespa - 没有会话 id,如何找到应用程序的内容 url。我只知道应用程序名称
- java - 在 RestTemplate 代码中得到 400 Request Bad Error 但在 SOAPUI 中得到正确的响应
- python - 如何将时间序列转换为从 00:00.00 开始的 %M:%S.%f
- java - MuteSetup 命令未设置有效的静音命令
- flutter - Dart @macro 在评论中
- node.js - Typegoose 类定义的静态方法返回空数组