No sé nada sobre codificación o desarrollo, así que si alguien pudiera volver a pegar mi código con las correcciones incluidas, sería increíblemente útil. Estoy agregando un widget de fecha actual a mi sitio web, pero no puedo centrarlo, cambiar el color de la fuente a blanco o cambiar la fuente en absoluto. ¿Alguien podría ayudarme?
Aquí está el código que estoy usando en su totalidad:
let date = new Date(); let day = String(date.getDate()); let weekday = date.getDay(); let month = date.getMonth(); let year = String(date.getFullYear()); const monthNames = ["January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] const weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"] month = monthNames[month]; weekday = weekdays[weekday] let fullDate = (weekday + " " + day + ", " + month + ", " + year); document.write(fullDate);Como esto
Tenga en cuenta que envolví el script en un evento de carga de página
Cambie document.body.innerHTML += a, por ejemplo
document.getElementById("myDateContainer").innerHTML =
si tienes otro lugar donde ponerlo
myDateContainer podría estar <div id="myDateContainer"></div> en algún lugar de su página
<!doctype html> <html> <head> <title>Some title</title> <style> .date { display: table; margin: 0 auto; color: red; font-family: Arial, Helvetica, sans-serif; } </style> <script> window.addEventListener("load", function() { let date = new Date(); let day = String(date.getDate()); let weekday = date.getDay(); let month = date.getMonth(); let year = String(date.getFullYear()); const monthNames = ["January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] const weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"] month = monthNames[month]; weekday = weekdays[weekday] let fullDate = (weekday + " " + day + ", " + month + ", " + year); document.body.innerHTML += `<span class="date">${fullDate}</span>`; }) </script> </head> <body> </body> </html>**Para centrar agregue ** su etiqueta de estilo después de "color: rojo"
<style> .date { display: flex; justify-content: center; align-items: center; } </style> <html> <head> <title>Some title</title> <style> .date { display: flex; justify-content: center; align-items: center; color: red; font-family: Arial, Helvetica, sans-serif; } </style> </head> <script> window.addEventListener("load", function() { let date = new Date(); let day = String(date.getDate()); let weekday = date.getDay(); let month = date.getMonth(); let year = String(date.getFullYear()); const monthNames = ["January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] const weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"] month = monthNames[month]; weekday = weekdays[weekday] let fullDate = (weekday + " " + day + ", " + month + ", " + year); document.body.innerHTML += `<span class="date">${fullDate}</span>`; }) </script> </head> <body> </body> </html>