简体   繁体   中英

Javascript get day from HTML date string

I have a div with a date within it as follows:

<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...

 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

 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>

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