fechas.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Funciones de validación de fechas
  3. */
  4. /*
  5. function validaFecha(fechaTxt){
  6. if(fechaTxt.charAt(4) == "-" && fechaTxt.charAt(7) == "-"){//yyyy-mm-dd
  7. var fechaArr = fechaTxt.split("-");
  8. var ano= fechaArr[0];
  9. var mes= fechaArr[1];
  10. var dia= fechaArr[2];
  11. }
  12. if(fechaTxt.charAt(2) == "/" && fechaTxt.charAt(5) == "/"){//dd-mm-aaaa
  13. var fechaArr = fechaTxt.split("/");
  14. var ano= fechaArr[2];
  15. var mes= fechaArr[1];
  16. var dia= fechaArr[0];
  17. }
  18. var d = new Date();
  19. var anoActual = d.getFullYear();
  20. if (isNaN(ano) || ano.length < 4 || parseInt(ano, 10) < (anoActual-1)){ return false; }
  21. if (isNaN(mes) || parseInt(mes, 10) < 1 || parseInt(mes, 10) > 12){ return false; }
  22. if (isNaN(dia) || parseInt(dia, 10) < 1 || parseInt(dia, 10) > 31){ return false; }
  23. if (mes == 4 || mes == 6 || mes == 9 || mes== 11) {
  24. if (dia > 30) { return false; }
  25. } else{
  26. if (mes == 2) {
  27. if(dia <= 28 )
  28. return true;
  29. else{
  30. if ((ano % 4 == 0) && dia == 29) return true;
  31. else return false;
  32. }
  33. }
  34. }
  35. return true;
  36. }*/
  37. function anoADia(fecha_str){//de yyyy-mm-dd a dd/mm/yyyy
  38. fecha_str = trim(fecha_str);
  39. if(fecha_str.charAt(4) == "-" && fecha_str.charAt(7) == "-"){//yyyy-mm-dd
  40. var fecha_arr = fecha_str.split("-");
  41. return fecha_arr[2]+"/"+fecha_arr[1]+"/"+fecha_arr[0];
  42. }
  43. return fecha_str;
  44. }
  45. function diaAAno(fecha_str){//de dd/mm/yyyy a yyyy-mm-dd
  46. fecha_str = trim(fecha_str);
  47. if(fecha_str.charAt(2) == "/" && fecha_str.charAt(5) == "/"){//dd/mm/yyyy
  48. var fecha_arr = fecha_str.split("/");
  49. return fecha_arr[2]+"-"+fecha_arr[1]+"-"+fecha_arr[0];
  50. }
  51. return fecha_str;
  52. }
  53. function cuentaDias(fechaI, fechaF) {//diferencia en días entre 2 fechas
  54. return Math.round((fechaF-fechaI)/(1000*60*60*24));
  55. }
  56. function cuentaMinutos(horaI, minI, horaF, minF) {//diferencia en minutos entre 2 horas
  57. var horaInicio = new Date();
  58. var horaFin = new Date();
  59. horaInicio.setHours(horaI);
  60. horaInicio.setMinutes(minI);
  61. horaInicio.setSeconds(0);
  62. horaFin.setHours(horaF);
  63. horaFin.setMinutes(minF);
  64. horaFin.setSeconds(0);
  65. return Math.round((horaFin - horaInicio) / (1000*60));
  66. }
  67. function cuentaMinutosStr(horaIStr, horaFStr) {//diferencia en minutos entre 2 horas
  68. var horaInicio = new Date();
  69. var horaFin = new Date();
  70. var horaI_arr = horaIStr.split(":");
  71. var horaF_arr = horaFStr.split(":");
  72. horaInicio.setHours(horaI_arr[0]);
  73. horaInicio.setMinutes(horaI_arr[1]);
  74. horaInicio.setSeconds(0);
  75. horaFin.setHours(horaF_arr[0]);
  76. horaFin.setMinutes(horaF_arr[1]);
  77. horaFin.setSeconds(0);
  78. return Math.round((horaFin - horaInicio) / (1000*60));
  79. }
  80. function fechaMayor(fechaI, fechaF) {//cual es mayor >0 I mayor <0 F mayor
  81. return (Date.parse(diaAAno(fechaI)) - Date.parse(diaAAno(fechaF)));
  82. }