image


Inicio » Foros » JavaScript

JavaScript

Participe, siéntase libre de escribir un mensaje en este foro de discusión o de responder a un mensaje del mismo. Aportando su opinión enriquece este sitio de manera que todos podemos aprender unos de otros. Los foros son el complemento ideal al curso para aprender compartiendo.

Restar fechas

Volver al foro | Escribir mensaje | Añadir nuevo tema


De: Benjamín Fidalgo Llorián
Fecha: 25/07/2008
Mensaje:

Hola, como me ha costado mucho dar con una función para restar fechas en javascript voy a postear la que yo mismo he hecho, de momento funciona, espero que sea de utilidad: function restarFechas(f0, f1) { var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/; var fecha0 = f0; var matchArray0 = fecha0.match(datePat); month0 = matchArray0[3]; day0 = matchArray0[1]; year0 = matchArray0[5]; var fecha1 = f1; var matchArray1 = fecha1.match(datePat); month1 = matchArray1[3]; day1 = matchArray1[1]; year1 = matchArray1[5]; var fechaIni = new Date(); fechaIni.setFullYear(year0, month0, day0) var fechaFin = new Date(); fechaFin.setFullYear(year1, month1, day1) var resta = fechaFin - fechaIni resta = resta/86400000 return(resta); } restarFechas('1/5/1974','12/5/1974')

Quizás le interese
De: Benjamín Fidalgo llorián
Fecha: 25/07/2008
Mensaje:

Hola,

como ha quedado una mierda formato pruebo a meter brs


function restarFechas(f0, f1) {
var datePat = /^(\d{1,2})(\/|-)(\d{1,2})
(\/|-)(\d{4})$/;
var fecha0 = f0;

var matchArray0 = fecha0.match(datePat);
month0 = matchArray0[3];
day0 = matchArray0[1];
year0 = matchArray0[5];


var fecha1 = f1;
var matchArray1 = fecha1.match(datePat);
month1 = matchArray1[3];
day1 = matchArray1[1];
year1 = matchArray1[5];

var fechaIni = new Date();
fechaIni.setFullYear(year0, month0, day0)


var fechaFin = new Date();
fechaFin.setFullYear(year1, month1, day1)

var resta = fechaFin - fechaIni
resta = resta/86400000
return(resta);

}

restarFechas('1/5/1974','12/5/1974')


De: tangok9
Fecha: 06/04/2009
Mensaje:

gracias amigo me salvaste la vida.


De: tangok9
Fecha: 06/04/2009
Mensaje:

Aqui la dejo pero para validar q una fecha ingresada no sea mayor a la de hoy

function validarFechas(pfechaIngresada){
var dia=new Date();
var fecha = dia.getYear()+"-"+(dia.getMonth()+1)+"-"+ dia.getDate();
var datePat = /^(\d{4})(\/|-)(\d{1,2})(\/|-)(\d{1,2})$/;
var fecha0 = fecha;

var matchArray0 = fecha0.match(datePat);
month0 = matchArray0[3];
day0 = matchArray0[5];
year0 = matchArray0[1];


var fecha1 = pfechaIngresada;
var matchArray1 = fecha1.match(datePat);
month1 = matchArray1[3];
day1 = matchArray1[5];
year1 = matchArray1[1];


if(parseInt(year1)==parseInt(year0)){
if(parseInt(month1)>parseInt(month0)){
return true;
}else if(parseInt(year1)==parseInt(year0) && parseInt(month1)==parseInt(month0) && parseInt(day1)>=parseInt(day0)){
return true;
}else{
return false;
}
}else if(parseInt(year1)>parseInt(year0)){
return true;
}



}

De: tangok9
Fecha: 06/04/2009
Mensaje:

ah!! el formato que use es 2009-4-6 o bien 2009-4-6

De: Manuel
Fecha: 27/01/2010
Mensaje:

Gracias Benjamín. A mi tambien me salvaste la vida


De: Peter
Fecha: 13/04/2010
Mensaje:

Bueno a mi no me resulto esa funcion por alguna razon pero me base en esa logica para hacer mi propia funcino, aki les dejo.


function validarFechas(pfechaIngresada){
var miFecha=pfechaIngresada.split("/");
var day1=parseInt(miFecha[0]);
var month1=parseInt(miFecha[1])-1;
var year1=parseInt(miFecha[2]);

var dia=new Date();
var fecha = dia.getFullYear()+"-"+(dia.getMonth())+"-"+ dia.getDate();
var day0=dia.getDate();
var month0=dia.getMonth();
var year0=dia.getFullYear();
if(parseInt(year1)<parseInt(year0)){
//alert("Ano: "+year1+" es menor");
return false;
}else{
if(parseInt(month1)<parseInt(month0)){
//alert("Mes: "+month1+" es menor");
return false
}else{
if(parseInt(day1)<parseInt(day0) && parseInt(month1)<=parseInt(month0)){
//alert("Dia: "+day1+" es menor");
return false
}else{
return true
}
}
}
}

algo a tomar en cuenta cuando queremos capturar el año: con fecha.getFullYear();

Espero les haya ayudado.

Slds desde Lima-peru



Volver al foro | Escribir mensaje | Añadir nuevo tema

Quizás le interese:
Si desea recibir los mensajes del foro sobre JavaScript por email
suscribir dar de baja

WebEstilo.com no se hace responsable de las opiniones que los usuarios puedan verter en cualquiera de los foros existentes.




WebEstilo.com - Introduzca su e-mail y conozca las novedades. No hacemos Spam.
Enlaces
Viagra Free Pills Sample
Cocina Facil IngenieroSoftware.com  
Google
  Web WebEstilo.com   
Última modificación:10 de Abril de 2008. Spain - España.
© 1998-2004 por Joaquin Gracia. Todos los derechos reservados.