Visualizzazione dei risultati da 1 a 8 su 8
  1. #1

    Controllo su date esistenti

    Ciao,
    ho dei range di date (esempio 20-04-2008 / 25-04-2008) e dovrei controllare, via javascript, che un utente non inserisca in un modulo una data compresa in questi intervalli.
    nell'esempio precedente, l'utente dovrebbe ricevere un msg di errore se inserisce una data compresa tra il 20 e il 25 aprile.

    Come posso eseguire questo controllo, considerato che i range di date sono piu di uno?

  2. #2
    questi range in che formato li hai?
    I DON'T Double Click!

  3. #3
    Originariamente inviato da artorius
    questi range in che formato li hai?
    ah il formato non è un probelma, le estraggo da un database e le stampo sul javascript via php.. quindi posso averle in ofrmato inglese, italiano, timestamp.. come è meglio per lo script.

  4. #4
    Date Object, poi puoi anche creare un Oggetto apposito, tipo io ho fatto questo per un periodo di date

    codice:
    var Period = function(start, end){
    	if(!start || typeof start != 'object' || !start.getTime){
    		var e = new Error();
    		e.message = "The 'start' argument must be a valid Date object";
    		e.number = 10000001
    		throw e;
    	}
    	if(!end || typeof end != 'object' || !end.getTime){
    		var e = new Error();
    		e.message = "The 'end' argument must be a valid Date object";
    		e.number = 10000002
    		throw e;
    	}
    	
    	this._startDate = start;
    	this._endDate = end;
    }
    Period.prototype.inPeriod = function(d){
    	if(!d || typeof d != 'object' || !d.getTime)
    	{
    		var e = new Error();
    		e.message = "The argument must be a valid Date object";
    		e.number = 10000003
    		throw e;
    	}
    	
    	return d.getTime() >= this._startDate.getTime() && d.getTime() <= this._endDate.getTime();
    }
    Period.prototype.setStartDate = function(start){
    	if(!start || typeof start != 'object' || !start.getTime)
    	{
    		var e = new Error();
    		e.message = "The startDate must be a valid Date object";
    		e.number = 10000004
    		throw e;
    	}
    	this._startDate = start;
    }
    /**
     * Set the End Date in the Period
     * @param {Date} end : the new end Date
     */
    Period.prototype.setEndDate = function(end){
    	if(!end || typeof end != 'object' || !end.getTime)
    	{
    		var e = new Error();
    		e.message = "The endDate must be a valid Date object";
    		e.number = 10000005
    		throw e;
    	}
    	this._endDate = end;
    }
    Period.prototype.getStartDate = function(){
    	return this._startDate;
    }
    Period.prototype.getEndDate = function(){
    	return this._endDate;
    }
    Poi crei N Period per gli intervalli di date:

    codice:
    var sd, ed;
    var rangeDiDate = [];
    var index = 0;
    
    //Qui fai il ciclio PHP per generare gli oggetti nel formato
    sd = new Date();
    sd.setTime(<?=$startDateUnixTimeStamp * 1000?>);
    ed = new Date();
    ed.setTime(<?=$endDateUnixTimeStamp * 1000?>);
    rangeDiDate[index++] = new Period(sd, ed);
    //Chiudi il ciclo PHP
    Poi ottieni le date dall'utente, le trasformi in Date Object ed usi la funzione inPeriod dell'oggetto Period per controllare se non sono valide
    I DON'T Double Click!

  5. #5
    Grazie mille per il codice!

    ma funziona anche con il timestamp di mysql?
    perchè la funzione unix_timetamp() di mysql ritorna un valore diverso dal getTime() di javascript

  6. #6
    l'unix timestamp indica i secondi passati dalle ore 0:00 del 1 gennaio 1970, i Date object di JS invece utilizzano i millisecondi, quindi devo moltiplicare per 1000
    I DON'T Double Click!

  7. #7
    Originariamente inviato da artorius
    l'unix timestamp indica i secondi passati dalle ore 0:00 del 1 gennaio 1970, i Date object di JS invece utilizzano i millisecondi, quindi devo moltiplicare per 1000
    a parte quello, riportano valori diversi.

  8. #8
    dipende dal tipo del campo dal quale estrai il valore
    I DON'T Double Click!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.