Sé cómo codificar en PHP pero es cierto que soy nuevo en Javascript. Estoy tratando de crear un enlace al recoger la fecha. Cuando hago eco de la fecha del código JS, funciona, pero agregarlo dentro de un enlace de alguna manera resulta desafiante.
<?php if ( ! defined( 'ABSPATH' ) ) exit; $date = '<script type="text/javascript"> var event = new Date(); var options = { year: "numeric", month: "numeric", day: "numeric" }; document.write(event.toLocaleDateString("es-CL", options)); </script>'; echo $date; // 18-11-2021 //Unable to create a link echo '<a href="/example/'.$date.'"> Today </a>'; ?>No estoy seguro de qué falta en mi código. Gracias
Puede usar como a continuación para obtener la fecha de JavaScript en el enlace:
<a href="" id="mydate"> Today </a> <script type="text/javascript"> var event = new Date(); var options = { year: "numeric", month: "numeric", day: "numeric" }; var date=event.toLocaleDateString("es-CL", options); document.getElementById("mydate").href="/example/"+date; </script>Así es como puedes lograrlo usando jquery
Paso 1: Agregar Jquery CDN
Paso 2: HTML
<a href="javascript:void(0);" class="test">Test</a>Paso 3: agregar el código Jquery y obtener la fecha y hora actual
var currentdate = new Date(); var datetime = currentdate.getDate() + "/" + (currentdate.getMonth()+1); $('.test').attr('href',datetime); //this is how you can add to href tag $('.test').text(datetime) //this is how you can add to text of <a> tagAsí es como puedes lograrlo.