Gestion des dates

Soyez le premier à donner votre avis sur cette source.

Snippet vu 10 319 fois - Téléchargée 26 fois

Contenu du snippet

Quelques fonctions qui aident à lire, modifier, formater les dates en javascript.
Ca ne fait pas tout, ca peut surement etre simplifié, mais le jour ou j'ai pondu ca, c'etait surtout pour me faciliter la vie dans mon travail vu que je manipulait enormement les dates. Si mes souvenirs sont bons, il reste qqs petits bugs mineurs.
C'est quand meme bien pratique

DateChange (DateIn, Nb, Type): Permet d'ajouter/enlever du temps (jour, mois, année) à une date.

DateFormat (DateIn, Mask, debug): Format une chaine de caractere en un objet date valide.
(ex:
DateFormat ("1969/06/20", "%YYYY%/%MM%/%DD%")
=> Fri Jun 20 00:00:00 EDT 1969
)
Dateinfo (DateIn): retourne toutes les infos d'un objet date.

en esperant que ca puisse aider quelqu'un
A+
Philippe

Source / Exemple :


// ***********************************************
function DateChange (DateIn, Nb, Type)
{
/***********************************************
    return a object Date ... up or down a date value
    DateIn is a Date Object; Nb is a integer; Type is a choice: ["DAY","MONTH","YEAR"]

                                                                                              • /
var Year = DateIn.getFullYear(); var Month = DateIn.getMonth() + 1; var Day = DateIn.getDate(); if (Type.toUpperCase().substring(0,1) == "D") { var DateMili = DateIn.setTime(DateIn.getTime() + (86400000 * Nb)); return DateIn; } if (Type.toUpperCase().substring(0,1) == "Y") Year += Nb; if (Type.toUpperCase().substring(0,1) == "M") { Month += Nb; if (Month < 1) { Month += 12; Year -= 1; } if (Month > 12) { Month -= 12; Year += 1; } } if (Day > NbDaysOfMonth(Year,Month)) Day = NbDaysOfMonth(Year,Month); return new Date(Year,Month-1,Day,DateIn.getHours(),DateIn.getMinutes(),DateIn.getSeconds()); } // *********************************************** function DateFormat (DateIn, Mask, debug) { /*********************************************** return a object Date Possible value for the Mask are ["YYYY", "YY", "MM", "mm", "mmmm", "DD", "HH", "HM", "HS", "/", "AMPM"]: The "%/" replace all separator in the date [ie: "1969/06/20,16:20:06", "%YYYY%/%MM%/%DD%/%HH%/%HM%/%HS" "june 20 1969 04:20:06 PM", "%mmmm%/%DD%/%YYYY%/%HH%/%HM%/%HS%/%AMPM"; ]
                                                                                              • /
var Month = new Array("JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE","JULY","AUGUST","SEPTEMBER","OCTOBER","NOVEMBER","DECEMBER" ); var Day= new Array("SUNDAY","MONDAY","TUESDAY","WEDNESDAY","THURSDAY","FRIDAY","SATURDAY"); var TypeOfMask = new Array(); var DateResult = new Array(); var MaskRes, RegResult, DateOut; var RegMask = ""; var DebugMsg = ""; // DEBUG LINE DebugMsg += "Date In= " + DateIn + "\n"; // DEBUG LINE DebugMsg += "Mask In= " + Mask.replace(/\%+/g," ") + "\n"; // DEBUG LINE TypeOfMask.YYYY = "(\\d{4})"; TypeOfMask.YY = "(\\d{2})"; TypeOfMask.MM = "(\\d{1,2})"; TypeOfMask.mm = "(\\w{3})"; TypeOfMask.mmmm = "(\\w{3,9})"; TypeOfMask.DD = "(\\d{1,2})"; TypeOfMask.HH = "(\\d{1,2})"; TypeOfMask.HM = "(\\d{1,2})"; TypeOfMask.HS = "(\\d{1,2})"; TypeOfMask.AMPM = "(A|P)"; TypeOfMask.SEP = "(\\W)"; Mask = Mask.replace(/\//g, "SEP"); MaskRes= Mask.match(/\w+/g); for (Y = 0; Y < MaskRes.length; Y++) RegMask += TypeOfMask[MaskRes[Y]]; RegMask = new RegExp ("^" + RegMask,"i") DebugMsg += "RegMask= " + RegMask + "\n"; // DEBUG LINE RegResult= RegMask.exec(DateIn) DebugMsg += "RegResult= " + RegResult + "\n"; // DEBUG LINE if (RegResult) { for (Y = 0; Y < MaskRes.length; Y++) if(MaskRes[Y] != "sep") DateResult[MaskRes[Y]]= RegResult[Y+1]; DateResult.YYYY = (DateResult.YY)?"20" + DateResult.YY:DateResult.YYYY; if (DateResult.mm) DateResult.MM = GetMonthNum (DateResult.mm)+1; else if (DateResult.mmmm) DateResult.MM = GetMonthNum (DateResult.mmmm)+1; DateResult.AMPM =(DateResult.AMPM)?DateResult.AMPM.toUpperCase():""; if (DateResult.HH) { if (DateResult.AMPM && DateResult.HH > 12) { DebugMsg += "Hours > 12 for a Time set with the format Am/Pm \n"; // DEBUG LINE if (debug) alert (DebugMsg); // DEBUG LINE return false; } else if (DateResult.AMPM == "P") DateResult.HH = parseInt(DateResult.HH) + 12; } else DateResult.HH = ""; DateResult.HM = (DateResult.HM)?DateResult.HM:""; DateResult.HS = (DateResult.HS)?DateResult.HS:""; DateOut = new Date (DateResult.YYYY, DateResult.MM -1, DateResult.DD, DateResult.HH, DateResult.HM, DateResult.HS); DebugMsg += "DateResult.YYYY = " + DateResult.YYYY + "\n"; // DEBUG LINE DebugMsg += "DateResult.MM = " + DateResult.MM + "\n"; // DEBUG LINE DebugMsg += "DateResult.DD = " + DateResult.DD + "\n"; // DEBUG LINE DebugMsg += "DateResult.HH = " + DateResult.HH + "\n"; // DEBUG LINE DebugMsg += "DateResult.HM = " + DateResult.HM + "\n"; // DEBUG LINE DebugMsg += "DateResult.HS = " + DateResult.HS + "\n"; // DEBUG LINE DebugMsg += "DateResult.AMPM = " + DateResult.AMPM + "\n"; // DEBUG LINE if (debug) alert (DebugMsg); // DEBUG LINE } else { if (debug) alert (DebugMsg); // DEBUG LINE return false; } return (DateResult.DD > NbDaysOfMonth(DateResult.YYYY,DateResult.MM))?false:DateOut; } // *********************************************** function Dateinfo (DateIn) { /*********************************************** return a Array. GMT is a true/false arguments resultat[0] = resultat.YYYY resultat[1] = resultat.YY resultat[2] = resultat.MM resultat[3] = resultat.mm resultat[4] = resultat.mmmm resultat[5] = resultat.DD resultat[6] = resultat.dd resultat[7] = resultat.dddd resultat[8] = resultat.HH resultat[9] = resultat.HM resultat[10] = resultat.HS resultat[11] = resultat.AMPM resultat[12] = resultat.SEC70 resultat[13] = resultat.GMT
                                                                                              • /
var TwoDigit = function (Num){Num = Num.toString(); return (Num.length == 1)?"0" + Num:Num;}; var RegE = /(\w+)\W+(\w+)/; var DateResult = new Array(); if (new Date(DateIn) == "NaN" || DateIn.toString().substring(DateIn.length-3) == "UTC" || !DateIn) { alert("Sungard EMS: Web Integration Debug. \n Error: functions infoDate(DateIn) in Functions.js . \n\n Not a good Date or a UTC (GMT) Date."); return false; } DateResult[0] = DateResult.YYYY = DateIn.getFullYear(); DateResult[1] = DateResult.YY = TwoDigit(DateIn.getYear().toString().substring(2)); DateResult[2] = DateResult.MM = TwoDigit(DateIn.getMonth() + 1); res = RegE.exec(DateIn); DateResult[3] = DateResult.mm = RegExp.$2; res = RegE.exec(DateIn.toLocaleString()); DateResult[4] = DateResult.mmmm = RegExp.$2; DateResult[5] = DateResult.DD = TwoDigit(DateIn.getDate()); res = RegE.exec(DateIn); DateResult[6] = DateResult.dd = RegExp.$1; res = RegE.exec(DateIn.toLocaleString()); DateResult[7] = DateResult.dddd = RegExp.$1; DateResult[8] = DateResult.HH = TwoDigit(DateIn.getHours()); DateResult[9] = DateResult.HM = TwoDigit(DateIn.getMinutes()); DateResult[10] = DateResult.HS = TwoDigit(DateIn.getSeconds()); res = /(\w+)$/.exec(DateIn.toLocaleString()); DateResult[11] = DateResult.AMPM = RegExp.$1; DateResult[12] = DateResult.SEC70 = DateIn.getTime(); DateResult[13] = DateResult.GMT= DateIn.toGMTString(); return DateResult; } // *********************************************** function NbDaysOfMonth (Year, Month) { switch(parseFloat(Month)) { case 2: return ( ((Year % 4 == 0) && ((! (Year % 100 == 0)) || (Year % 400 == 0) ) ) ? 29 : 28); case 4:; case 6:; case 9:; case 11: return 30; case 1:; case 3:; case 5:; case 7:; case 8:; case 10:; case 12: return 31; default: return -1; } } // *********************************************** function GetMonthName(Month, little) { var MonthName = new Array("January","February","March","April","May","June","July","August","September","October","November","December" ); return (little)?MonthName[parseInt(Month)].substring(0,3):MonthName[parseInt(Month)]; } // *********************************************** function GetMonthNum(Month) { var MonthName = new Array("JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"); for (Y=0; Y<12; Y++) if (MonthName[Y] == Month.substring(0,3).toUpperCase()) return Y; } // *********************************************** function GetDayName (Day, little) { var DayName= new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"); return (little)?DayName[parseInt(Day)].substring(0,3):DayName[parseInt(Day)]; } // *********************************************** function GetDayNum (Day) { var DayName= new Array("SUN","MON","TUE","WED","THU","FRI","SAT"); for (Y=0; Y<7; Y++) if (DayName[Y] == Day.substring(0,3).toUpperCase()) return Y; }

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.