Format.js: Format date according to country domain

Recommend this page to a friend!
     
  Info   Example   View files Files   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 139 All time: 357 This week: 4Up
Version License JavaScript version Categories
date-format 1.0GNU General Publi...1.0Time and Date
Description 

Author

This object can Format date according to country domain.

It extends the Date object with a function to return the current date value formatted to a given country standard.

The object takes as parameter the country 2 letter symbol as used in Internet domains.

Innovation Award
JavaScript Programming Innovation award winner
September 2014
Winner


Prize: One downloadable e-book of choice by O'Reilly
Dates are formatted differently depending on the country the dates are presented.

This object extends the Date object to return the format of dates for a country given the respective two letter country code.

Manuel Lemos
Picture of Pierre FAUQUE
Name: Pierre FAUQUE <contact>
Classes: 3 packages by
Country: France France
Age: 77
All time rank: 935 in France France
Week rank: 6 Up2 in France France Up
Innovation award
Innovation award
Nominee: 3x

Winner: 2x

Example

<html> <head> <title>Date formats</title> <script language="javascript" type="text/javascript"> Date.prototype.Format=function(tld) {var tld,d,D,m,M,y,Y,f,n;if(!tld){tld='fr';}d=this.getDate()*1;D=(d<10)?'0'+d:d;m=this.getMonth()+1;M=(m<10)?'0'+m:m;Y=''+this.getFullYear();y=Y.substr(2,2);f=new Array('al,ar,au,at,be,bo,br,bg,ca,cl,co,hr,es,ee,ec,fr,gr,gy,hk,in,ie,il,it,lv,mo,ma,mx,nz,py,nl,pl,pt,pe,ro,gb,ru,do,cz,sg,sk,si,th,tr,uy,ve','ch,ua','dk,no,se','usa','za,sa,am,az,bh,cn,cy,kr,dj,ae,er,hk,iq,ir,il,jp,jo,kw,lb,ly,lt,om,qa,sy,tw,ye,mysql','hu','de,fi,eu','iso');n=0;while(f[n].indexOf(tld.toLowerCase())<0){n++;}switch(n){case 0:return D+'/'+M+'/'+Y;break;case 1:return D+'.'+M+'.'+Y;break;case 2:return D+'/'+M+'-'+y;break;case 3:return M+'-'+D+'-'+y;break;case 4:return Y+'-'+M+'-'+D;break;case 5:return Y+'.'+M+'.'+D;break;case 6:return d+'.'+m+'.'+Y;break;case 7:return Y+M+D;break;}}; </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>

  Files folder image Files (3)  
File Role Description
Accessible without login Plain text file example.html Example Example of use
Plain text file Format.js Class Class itself
Plain text file Format.large.js Class The Class in large writing

 Version Control Unique User Downloads Download Rankings  
 0%
Total:139
This week:0
All time:357
This week:4Up