Assign current date and time to DOM element

2

I'm trying to perform a horizontal slider in which, I have a 'p' tag to which I want to assign the current time and date, but if I do it in the jquery ready, which is when our page is already loaded or document, only the date of that moment would be shown, but it would not continue updating, I hope I understand, I leave the code that I have.

  

MY HTML

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="css/protexa.css">
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

    <title>Grupo Protexa</title>
  </head>
  <body>
	<div class="container-fluid">
		<div class="row"> 
			<div class="wrapper">
				<div class="slides" id="slide1"></div>
				<div class="slides" id="slide2"></div>
				<div class="slides" id="slide3"></div>
				<div class="hora"><p class="h1">Aqui vamos a poner la hora</p></div>
			</div>
		</div>
	</div>
    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
    <script src="js/protexa.js"></script>
  </body>
</html>
  

MY CSS

body {
    overflow:hidden;
}

.wrapper {
	min-height: 100%;
	height: auto !important;
	width: 100%;
	margin: auto;
	background: #000000
        position: absolute;
        overflow: hidden;
}

.hora{
	position:relative;
	top:10px;
}

.slides {
	position: absolute;
	min-height: 100%;
	height: auto !important;
	width: 100%;
}

#slide1{
	background: transparent url("http://www.protexa.com.mx/wp-content/uploads/2018/03/Edificio_Mty.jpg") no-repeat center;
	background-size: cover;
	animation: left1 15s infinite;
}

#slide2 {
	background: transparent url("http://www.protexa.com.mx/wp-content/uploads/2015/05/Sol.jpg") no-repeat center;
	background-size: cover;
	animation: left2 15s infinite;
	left: 100%;
}
#slide3 {
	background: transparent url("http://www.protexa.com.mx/wp-content/uploads/2018/03/protexa.jpg") no-repeat center;
	background-size: cover;
	animation: left3 15s infinite;
	left: 200%;
}

@keyframes left1 {
0% {left: 0%; right:0%;}
26.8% {left: 0%; right:0%;}
33.5% {left: -100%; right: 100%;}
93.8% {left: -200%; right: 200%;}
100% {left: 0%; right: 0%;}
}

@keyframes left2 {
0% {left: 100%; right:-100%;}
26.8% {left: 100%; right:-100%;}
33.5% {left: 0%; right: 0%;}
60.3% {left: 0%; right: 0%;}
67% {left: -100%; right: 100%;}
93.8% {left: -100%; right: 100%;}
100% {left: 100%; right: -100%;}
}

@keyframes left3 {
0% {left: 200%; right:-200%;}
26.8% {left: 200%; right:-200%;}
33.5% {left: 100%; right: -100%;}
60.3% {left: 100%; right: -100%;}
67% {left: 0%; right: 0%;}
93.8% {left: 0%; right: 0%;}
100% {left: 200%; right: -200%;}
}

And here is where I plan to put the javascript code to assign the current time to the DOM element.

  

ARCHIVE JS

// Shorthand for $( document ).ready()
$(function() {
    console.log( "ready!" );
    var fecha = new Date();
//alert("Día: "+fecha.getDate()+"\nMes: "+(fecha.getMonth()+1)+"\nAño: "+fecha.getFullYear());
//alert("Hora: "+fecha.getHours()+"\nMinuto: "+fecha.getMinutes()+"\nSegundo: "+fecha.getSeconds()+"\nMilisegundo: "+fecha.getMilliseconds());
});
    
asked by Rico 21.03.2018 в 17:20
source

1 answer

0

Imagine that this element is where you want to show your data.

<h2 id="reloj"></h2>

All you have to do is in JavaScript to use setInterval.

var reloj = document.getElementById('reloj');
setInterval(function(){
    var data = new Date();
    reloj.innerHTML = data.getDate()+ "/" + "/" +data.getMonth()+1 + "/"+data.getFullYear()+"/ hora: "+data.toLocaleTimeString();
},1000);
    
answered by 21.03.2018 / 17:30
source