简体   繁体   English

Javascript 从 HTML 日期字符串中获取日期

[英]Javascript get day from HTML date string

I have a div with a date within it as follows:我有一个包含日期的 div,如下所示:

<div id="am-event-sub-info">
 <div> February 27, 2021 11:30 am - 1:00 pm</div>
</div>

I am trying to extract the date from this string and then get the day?我试图从这个字符串中提取日期然后得到日期?

I hope you don't mind that Februrary 27th 2021 is a Saturday...我希望你不介意 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>

Update: Add the "day" after date in a 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>

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

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