如何得到一个小的javascript HTML片段?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (45)

我需要一个java脚本HTML片段,它总是在下个星期天晚上11:59返回。

如果今天的日期是“星期三,七月十八日”,返回的日期是“星期日,七月二十二日”。

代码如下:

<p class="price-small">Don't miss this special offer<br />
  Offer ends Tues, Jul 22 at 11:59pm PST</p>
提问于
用户回答回答于

这样做是正确的:

// get two dates
var today = nextSunday = new Date();
// ajust Sunday
nextSunday.setDate(today.getDate() + (7 - today.getDay()));
nextSunday.setHours(23, 59, 0, 0) ;
// then  format it
var nextSundayFormatted = nextSunday.toLocaleFormat('%a, %b %d %I:%M%p') + ' PST';
// then use it
document.getElementsByClassName("price-small")[0].innerHTML= 
    "Don't miss this special offer<br>Offer ends "+ nextSundayFormatted;
<p class="price-small">Don't miss this special offer<br />
  Offer ends Tues, Jul 22 at 11:59pm PST</p>

用户回答回答于
            <script>
               var Week = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturnday","Sunday"]; 
           var date = new Date()
           var Distance = 7-(Week.indexOf(date.getDay())+1)        
    date.setDate(date.getDate() + Distance);

    document.getElementByClassName("price-small")[0].innerHtml = "Don't miss this special offer<br>
      Offer ends"+ date;
</script>

这是一个开始,你可以自定义它。

扫码关注云+社区

领取腾讯云代金券