簡體   English   中英

如何在 html 的兩個表格單元格中插入昨天和今天的日期?

[英]How do I insert yesterday's and today's dates in two table cells in html?

我有一個簡單的文檔,其 mwe 在下面的代碼中給出。 在左右表格單元格中,我想要“0800 hrs LT 27 Aug.”,在中間單元格中我想要“1800 hrs LT 26 Aug.”。 例如。 日期會每天變化。 我看過的所有帖子都沒有真正幫助我。 我會感謝幫助。 齊洛爾·蒙巴

<!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您可以以您需要的format('D MMM') )設置今天的日期( moment() )和昨天的日期( moment().subtract(1, "days") ),如下所示:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM