如何在 html 的两个表格单元格中插入昨天和今天的日期?



我有一个简单的文档,下面的代码中给出了它的mwe。在左表单元格和右表单元格中;8月27日上午8时";,并且在中间小区中;LT 8月26日1800时";例如日期每天都会变化。我看过的所有帖子都对我没有真正的帮助。我会感谢你的帮助。Zilore Mumba

<!DOCTYPE html>
<html>
<head>
</style>
<script language="JavaScript">
<!-- debut
var day="";
var month="";
var myweekday="";
var year="";
mydate = new Date();
myday = mydate.getDay();
mymonth = mydate.getMonth();
myweekday= mydate.getDate();
weekday= myweekday;
myyear= mydate.getYear();
if (myyear < 1900)
myyear = 1900 + myyear ;
year = myyear ;
if(myday == 0)
....
....
// End -->
</script>
</head>
<body>
<center><table class="a" id="table1" border ="2" cellpadding="10" cellspacing="0">
<tr>
<th><h3>Minimum Temperatures:<br>0800 hrs LT</h3></th>
<th><h3>Maximum Temperatures:<br>1800 hrs LT (D-1)</h3></th>
<th><h3>Rainfall (mm):<br>0800 hrs LT</h3></th>
</tr>

<tr>
<td class="td_size" align="center"> <a target="_self" href="Obs_Output/TminR.png"><img
src="Obs_Output/TminR.png"></a></td>
<td class="td_size" align="center"> <a target="_self" href="Obs_Output/TmaxR.png"><img 
src="Obs_Output/TmaxR.png"></a></td>
<td class="td_size" align="center"> <a target="_self" href="Obs_Output/RainfallR.png"><img
src="Obs_Output/RainfallR.png"></a></td>
</tr>
</table></center>
</body>
</html>

Ciao,通过使用moment,您可以将今天日期(moment()(和昨天日期(moment().subtract(1, "days")(设置为您需要的格式(format('D MMM')(,如下所示:

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<center><table class="a" id="table1" border ="2" cellpadding="10" cellspacing="0">
<tr>
<th><h3>Minimum Temperatures:<br>0800 hrs LT <div id="today"></div></h3></th>
<th><h3>Maximum Temperatures:<br>1800 hrs LT <div id="yesterday"></div></h3></th>
<th><h3>Rainfall (mm):<br>0800 hrs LT <div id="today"></div></h3></th>
</tr>

<tr>
<td class="td_size" align="center"> <a target="_self" href="Obs_Output/TminR.png"><img
src="Obs_Output/TminR.png"></a></td>
<td class="td_size" align="center"> <a target="_self" href="Obs_Output/TmaxR.png"><img 
src="Obs_Output/TmaxR.png"></a></td>
<td class="td_size" align="center"> <a target="_self" href="Obs_Output/RainfallR.png"><img
src="Obs_Output/RainfallR.png"></a></td>
</tr>
</table></center>
</body>
</html>
<script> 
(function() {
document.querySelectorAll("#today").forEach(el => {
el.innerHTML = moment().format('D MMM');
});
document.getElementById("yesterday").innerHTML = moment().subtract(1, "days").format('D MMM');
})();     
</script>

最新更新