sábado, 13 de septiembre de 2014

Format.js - Formatear la fecha dependiendo del país seleccionado


Format.js puede formatear la fecha acorde a la fecha estándar del país.

Format.js toma como parametro las dos letras del país, al igual como se utiliza en los dominios de internet, por ejemplo, en Argentina es "ar" o en España "es". En Argentina y España el formato estándar es del tipo 13/09/2014 y en U.S.A "usa" es 13-09-14

Descargar: Format.js

En los archivos descargados viene un ejemplo, example.html ...

<html>

<head>
<title>Date formats</title>

<script language="javascript" src="Format.js" type="text/javascript"></script>

<script language="javascript" type="text/javascript">

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Test the validity of a French date (DD/MM/AAAA).
// Return "OK" or an error message
// You can interchange the lines A and B (with indices correction) for a format such as MM/DD/AAAA
function isValidDate(date) {
	var date, tab, reg = new RegExp("^[0-9]{2}[/]{1}[0-9]{2}[/]{1}[0-9]{4}$","g");
	if(reg.test(date)) {
		tab=date.split('/');
		if((tab[0]*1)<1 || (tab[0]*1)>31) { return "Incorrect day"; }   // A
		if((tab[1]*1)<1 || (tab[1]*1)>12) { return "Incorrect month"; } // B
		return "OK";
	}
	return "Incorrect format date";
}

function showFormat() {

	var thedate,tld,country,tab,j,m,a,msg;
	
	// get the values of the form
	thedate = document.test.date.value;
	tld     = document.test.fmt.options[document.test.fmt.options.selectedIndex].value;
	country = document.test.fmt.options[document.test.fmt.options.selectedIndex].text;
	
	tab = thedate.split('/');
	j=tab[0]*1; m=tab[1]-1; a=tab[2]*1;
	thedate = new Date(a, m, j);
	
	msg  = country+' = '+thedate.Format(tld);
	alert(msg);
}

function verif() {

	var thedate;

	// get the values of the form
	thedate = document.test.date.value;

	if(!thedate) {
		alert('Date is missing');
		document.test.date.focus();
		return false;
	}

	msg = isValidDate(thedate);
	if(msg != "OK") {
		alert("Date :\n"+msg);
		document.test.date.focus();
		return false;
	}

	return true;
}
</script>
</head>

<body>
<h3>Date formats</h3>
<form name="test" method="post" action="javascript:showFormat()" onsubmit="return verif()">
Choose a date format <select name='fmt'>
<option value='fr'>Default: France (France)</option>
<option value='al'>Albania (Albanie)</option>
<option value='ar'>Argentina (Argentine)</option>
<option value='am'>Armenia (Arménie)</option>
<option value='au'>Australia (Australie)</option>
<option value='at'>Austria (Autriche)</option>
<option value='az'>Azerbaijan (Azerbaïdjan)</option>
<option value='bh'>Bahrain (Bahreïn)</option>
<option value='be'>Belgium (Belgique)</option>
<option value='bo'>Bolivia (Bolivie)</option>
<option value='br'>Brazil (Brésil)</option>
<option value='bg'>Bulgaria (Bulgarie)</option>
<option value='ca'>Canada (Canada)</option>
<option value='cl'>Chile (Chili)</option>
<option value='cn'>China (Chine)</option>
<option value='co'>Colombia (Colombie)</option>
<option value='hr'>Croatia (Croatie)</option>
<option value='cy'>Cyprus (Chypre)</option>
<option value='cz'>Czech Republic (République Tchèque)</option>
<option value='dk'>Denmark (Danemark)</option>
<option value='dj'>Djibouti (Djibouti)</option>
<option value='do'>Dominican Republic (République Dominicaine)</option>
<option value='ec'>Ecuador (Équateur)</option>
<option value='er'>Eritrea (Érythrée)</option>
<option value='ee'>Estonia (Estonie)</option>
<option value='eu'>European Union (Union Européenne)</option>
<option value='fi'>Finland (Finlande)</option>
<option value='fr'>France (France)</option>
<option value='de'>Germany (Allemagne)</option>
<option value='gr'>Greece (Grèce)</option>
<option value='gy'>Guyana (Guyana)</option>
<option value='hk'>Hong Kong (Hong-Kong)</option>
<option value='hu'>Hungary (Hongrie)</option>
<option value='in'>India (Inde)</option>
<option value='iq'>Iraq (Iraq)</option>
<option value='ie'>Ireland (Irlande)</option>
<option value='ir'>Islamic Republic of Iran (République Islamique d'Iran)</option>
<option value='iso'>ISO-8601</option>
<option value='il'>Israel (Israël)</option>
<option value='it'>Italy (Italie)</option>
<option value='jp'>Japan (Japon)</option>
<option value='jo'>Jordan (Jordanie)</option>
<option value='kw'>Kuwait (Koweït)</option>
<option value='lv'>Latvia (Lettonie)</option>
<option value='lb'>Lebanon (Liban)</option>
<option value='ly'>Libyan Arab Jamahiriya (Jamahiriya Arabe Libyenne)</option>
<option value='lt'>Lithuania (Lituanie)</option>
<option value='mo'>Macao (Macao)</option>
<option value='mx'>Mexico (Mexique)</option>
<option value='ma'>Morocco (Maroc)</option>
<option value='mysql'>MySQL</option>
<option value='nl'>Netherlands (Pays-Bas)</option>
<option value='nz'>New Zealand (Nouvelle-Zélande)</option>
<option value='no'>Norway (Norvège)</option>
<option value='om'>Oman (Oman)</option>
<option value='py'>Paraguay (Paraguay)</option>
<option value='pe'>Peru (Pérou)</option>
<option value='pl'>Poland (Pologne)</option>
<option value='pt'>Portugal (Portugal)</option>
<option value='qa'>Qatar (Qatar)</option>
<option value='kr'>Republic of Korea (République de Corée)</option>
<option value='ro'>Romania (Roumanie)</option>
<option value='ru'>Russian Federation (Fédération de Russie)</option>
<option value='sa'>Saudi Arabia (Arabie Saoudite)</option>
<option value='sg'>Singapore (Singapour)</option>
<option value='sk'>Slovakia (Slovaquie)</option>
<option value='si'>Slovenia (Slovénie)</option>
<option value='za'>South Africa (Afrique du Sud)</option>
<option value='es'>Spain (Espagne)</option>
<option value='se'>Sweden (Suède)</option>
<option value='ch'>Switzerland (Suisse)</option>
<option value='sy'>Syrian Arab Republic (République Arabe Syrienne)</option>
<option value='tw'>Taiwan (Taïwan)</option>
<option value='th'>Thailand (Thaïlande)</option>
<option value='tr'>Turkey (Turquie)</option>
<option value='usa'>U.S.A.</option>
<option value='ua'>Ukraine (Ukraine)</option>
<option value='ae'>United Arab Emirates (Émirats Arabes Unis)</option>
<option value='gb'>United Kingdom (Royaume-Uni)</option>
<option value='uy'>Uruguay (Uruguay)</option>
<option value='ve'>Venezuela (Venezuela)</option>
<option value='ye'>Yemen (Yémen)</option>
</select><br/>
Write a date (DD/MM/AAAA) <input type="text" name="date" size="10">
<input type="submit" name="submit" value="Validate">
</form>

</body>

</html>