2011-03-31 13 views
24

¿Cómo mostrar la fecha de ayer en mi cuadro de texto la fecha de ayer y al mismo tiempo, la fecha de hoy en?Código de Javascript para mostrar la fecha de ayer y la fecha de hoy

Tengo este home.php donde muestro la fecha de ayer (el usuario no puede modificar esto-solo) y la fecha de hoy (el usuario DEBE ingresar la fecha hoy). Y cuando llegue el día de mañana y el usuario visite el hogar, PHP verá la fecha ingresada ayer e ingresará nuevamente la fecha para el día de mañana.

Ejem: Día 1 (Marzo 30 de, 2011) fecha de ayer: 29 de marzo de 2011. (No editable de texto) Introduzca la fecha de hoy: (Voy a Type ..) 30 de marzo de 2011.

Día 2 (31 de marzo de 2011) Fecha de ayer: 30 de marzo de 2011. (Cuadro de texto no editable) Automáticamente, esto aparecerá al tocar home.php Ingrese la fecha hoy: (escribiré ...) 31 de marzo de 2011.

y así sucesivamente ..

necesito una validación de que no aceptará el formato de fecha incorrecta y el formato debe ser: 01-Mar-11 ¿Cómo hacer esto? :(fecha

+0

si el usuario puede ingresar solo la fecha de hoy, ¿por qué es editable? ¿por qué no lo pones tú solo? –

+0

Hola Furgan, no tengo problemas para ingresar la fecha de hoy. Mi problema es que no puedo obtener la fecha de ayer. :( – catsgirl008

Respuesta

83

de ayer es simplemente la fecha de hoy menos uno, así:.

var d = new Date(); 
d.setDate(d.getDate() - 1); 

Si hoy es 1 de abril de entonces se pone a 0 abril, que se convirtió el 31 de marzo

Puesto que usted también quería hacer algunas otras cosas, aquí hay algunas funciones que hacerlo:

// Check if d is a valid date 
// Must be format year-month name-date 
// e.g. 2011-MAR-12 or 2011-March-6 
// Capitalisation is not important 
function validDate(d) { 
    var bits = d.split('-'); 
    var t = stringToDate(d); 
    return t.getFullYear() == bits[0] && 
     t.getDate() == bits[2]; 
} 

// Convert string in format above to a date object 
function stringToDate(s) { 
    var bits = s.split('-'); 
    var monthNum = monthNameToNumber(bits[1]); 
    return new Date(bits[0], monthNum, bits[2]); 
} 

// Convert month names like mar or march to 
// number, capitalisation not important 
// Month number is calendar month - 1. 
var monthNameToNumber = (function() { 
    var monthNames = (
    'jan feb mar apr may jun jul aug sep oct nov dec ' + 
    'january february march april may june july august ' + 
    'september october november december' 
    ).split(' '); 

    return function(month) { 
    var i = monthNames.length; 
    month = month.toLowerCase(); 

    while (i--) { 
     if (monthNames[i] == month) { 
     return i % 12; 
     } 
    } 
    } 
}()); 

// Given a date in above format, return 
// previous day as a date object 
function getYesterday(d) { 
    d = stringToDate(d); 
    d.setDate(d.getDate() - 1) 
    return d; 
} 

// Given a date object, format 
// per format above 
var formatDate = (function() { 
    var months = 'jan feb mar apr may jun jul aug sep oct nov dec'.split(' '); 
    function addZ(n) { 
    return n<10? '0'+n : ''+n; 
    } 
    return function(d) { 
    return d.getFullYear() + '-' + 
      months[d.getMonth()] + '-' + 
      addZ(d.getDate()); 
    } 
}()); 

function doStuff(d) { 

    // Is it format year-month-date? 
    if (!validDate(d)) { 
    alert(d + ' is not a valid date'); 
    return; 
    } else { 
    alert(d + ' is a valid date'); 
    } 
    alert(
    'Date in was: ' + d + 
    '\nDay before: ' + formatDate(getYesterday(d)) 
); 
} 


doStuff('2011-feb-08'); 
// Shows 2011-feb-08 is a valid date 
//  Date in was: 2011-feb-08 
//  Day before: 2011-feb-07 
+0

Gracias RobG! :) – catsgirl008

+0

excelente ... gracias. –

11

Un forro:

var yesterday = new Date(Date.now() - 864e5); // 864e5 == 86400000 == 24*60*60*1000 
+1

Puede valer la pena usar '8640000' en lugar de hacer el cálculo' 24 * 60 * 60 * 1000' cada vez. –

+1

Debe ser '86400000' –

+0

@danielcorin tienes razón. Solo quería que la respuesta fuera clara. Actualizaré la respuesta. '24 * 60 * 60 * 1000 ==> 86400000 ==> 864e5' – oriadam

Cuestiones relacionadas