Difference between revisions of "DOM Calendar"
Jump to navigation
Jump to search
Line 32: | Line 32: | ||
${today.toLocaleDateString('fr',{month:'long'})} <br> | ${today.toLocaleDateString('fr',{month:'long'})} <br> | ||
`; | `; | ||
</pre> | |||
</div> | |||
==Show 7 days from now== | |||
<div class=qu data-width=300> | |||
<pre class=usr> | |||
let n = new Date().getTime(); | |||
for(let i = 0; i<7; i++){ | |||
let dy = new Date(n + 1000*60*60*24*i); | |||
let d = document.createElement('div'); | |||
d.innerHTML = dy.toLocaleDateString(); | |||
document.body.append(d); | |||
} | |||
</pre> | |||
<pre class=ans> | |||
let n = new Date().getTime(); | |||
for(let i = 0; i<7; i++){ | |||
let dy = new Date(n + 1000*60*60*24*i); | |||
let d = document.createElement('div'); | |||
d.innerHTML = dy.toLocaleDateString(); | |||
document.body.append(d); | |||
} | |||
</pre> | </pre> | ||
</div> | </div> |
Revision as of 21:44, 9 January 2022
Display dates as specified
- For today's date show
- day of the week in English
- month in English
- day of the week in French
- month in French
The phrase:
let today = new Date()
sets the variable today
to today's date
The function toLocalDateString takes an options object. You can set:
- weekday to
long
short
narrow
- month to
long
short
narrow
- the first parameter can be a language such as "en", "fr", "de", "zh"
let today = new Date(); document.body.innerHTML = ` ${today.toLocaleDateString('en',{weekday:'long'})} <br> ${today.toLocaleDateString('en',{weekday:'short'})} <br> `;
let today = new Date(); document.body.innerHTML = ` ${today.toLocaleDateString('en',{weekday:'long'})} <br> ${today.toLocaleDateString('en',{month:'long'})} <br> ${today.toLocaleDateString('fr',{weekday:'long'})} <br> ${today.toLocaleDateString('fr',{month:'long'})} <br> `;
Show 7 days from now
let n = new Date().getTime(); for(let i = 0; i<7; i++){ let dy = new Date(n + 1000*60*60*24*i); let d = document.createElement('div'); d.innerHTML = dy.toLocaleDateString(); document.body.append(d); }
let n = new Date().getTime(); for(let i = 0; i<7; i++){ let dy = new Date(n + 1000*60*60*24*i); let d = document.createElement('div'); d.innerHTML = dy.toLocaleDateString(); document.body.append(d); }