I have a start date and an end date.
I would like to know how to perform a function that validates if today's date is within that range of dates.
I have a start date and an end date.
I would like to know how to perform a function that validates if today's date is within that range of dates.
You can try with the moment, using a function that is called between
var moment = require('moment');
// el moment() seria la fecha actual, igual podrias asignarle una fecha diferente a la actual moment por ejemplo('2016-06-06')
if( moment().isBetween( moment('2016-01-01'), moment('2016-12-31') ) ){
res.json( 'la fecha de hoy esta en el rango')
}else{
res.json( 'la fecha actual no esta en el rango')
}
Instead of writing a function, you can use these excellent libraries that are well tested: moment.js and the plugin moment-range :
Also, to get today's date, you have the object Date
, that when you create it you get the current date.
Then you do something like this:
var moment = require('moment');
require('moment-range');
// en el momento de comparar
var inicioDelCurso = new Date(2016, 3, 7), // declaras las fechas
finDelCurso = new Date(2016, 11, 30),
rangoDelCurso = moment().range(inicioDelCurso, finDelCurso), // creas el rango del curso
ahoraMismo = new Date();
// esto retorna verdadero, desde noviembre próximo retornaría falso
rangoDelCurso.contains(ahoraMismo);
// esto retorna falso
rangoDelCurso.contains(new Date(2015, 1, 1));
Remember that you must install the modules:
npm install moment
npm install moment-range