• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Pruebas Online
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

219
Vistas
if else not working correctly if both dates are the same day

I would like to display the deadline date if it is today or in the future and the word 'Closed' if the date has passed.

I have the following code which is working APART from when the deadline and today's date are the same.

HTML

<div class="deadline-inner">
              <p
                v-if="Date.now() > new Date(deadline)"
                class="showclosedtitle"
              >
                <i class="fa fa-calendar-check-o" aria-hidden="true"></i>Closed
              </p>
              <p v-else>
                <i class="fa fa-calendar-check-o" aria-hidden="true"></i>
                <strong>Deadline: </strong>{{ deadline }}
              </p>
            </div>

The deadline is in this format: "21 December 2021"

I have also tried:

 v-if="Date.now() >= new Date(deadline)"
 v-if="Date.now() > +1 new Date(deadline)"

but all of them show the 'else' part of the statement if the deadline and today's date are the same. Any help is appreciated.

over 3 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Try with "end of day" time

Typing your dates into the browser console (at the time of writing this):

> new Date('21 December 2021');
Tue Dec 21 2021 00:00:00

> new Date();
Tue Dec 21 2021 12:19:09

So new Date() is always "now" down to the second (millisecond, really).
Your compared date is always from "00:00:00".
So if you want to know if new Date() is later than deadline, add 23:59:59 as the time:

new Date() > new Date('21 December 2021 23:59:59');

This will now be false until tomorrow.

So in your case you could solve it in deadline itself or add the time manually:

v-if="Date.now() > new Date(deadline + ' 23:59:59')"
over 3 years ago · Santiago Trujillo Denunciar

0

@user13928344 Your deadline isn't specific enough. There's no hours/minutes/seconds/milliseconds, so it defaults to the very first moment that date exists, which is midnight, or 00:00:00:000. So your new Date() will always be greater than the deadline date and return true (barring the very slim chance your logic will run at exactly midnight and all of the zero's, in which case it would be equal to your deadline and not greater than, so would return false). Where are you getting your deadline data from? Are you transforming it into the current format yourself and getting it from a backend? If so, convert it to a more specific date/time. Otherwise if it's coming from user input, ie a form, convert your date to milliseconds and then add 86340000 which will take your deadline up to the the very end of that day

over 3 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda