JavaScript DHTML/Development/Date Format — различия между версиями

Материал из Web эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 10:02, 26 мая 2010

Creating a Friendly Date String

/*
JavaScript Bible, Fourth Edition
by Danny Goodman 
John Wiley & Sons CopyRight 2001
*/

<HTML>
<HEAD>
<TITLE>Date String Maker</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function MakeArray(n) {
    this.length = n
    return this
}
monthNames = new MakeArray(12)
monthNames[1] = "January"
monthNames[2] = "February"
monthNames[3] = "March"
monthNames[4] = "April"
monthNames[5] = "May"
monthNames[6] = "June"
monthNames[7] = "July"
monthNames[8] = "August"
monthNames[9] = "September"
monthNames[10] = "October"
monthNames[11] = "November"
monthNames[12] = "December"
dayNames = new MakeArray(7)
dayNames[1] = "Sunday"
dayNames[2] = "Monday"
dayNames[3] = "Tuesday"
dayNames[4] = "Wednesday"
dayNames[5] = "Thursday"
dayNames[6] = "Friday"
dayNames[7] = "Saturday"
function customDateString(oneDate) {
    var theDay = dayNames[oneDate.getDay() + 1]
    var theMonth = monthNames[oneDate.getMonth() + 1]
    var theYear = oneDate.getYear()
    theYear += (theYear < 100) ? 1900 : 0
    return theDay + ", " + theMonth + " " + oneDate.getDate() + ", " + theYear
}
</SCRIPT>
</HEAD>
<BODY>
<H1> Welcome!</H1>
<SCRIPT LANGUAGE="JavaScript">
document.write(customDateString(new Date()))
</SCRIPT>
<HR>
</BODY>
</HTML>



Formate date in JavaScript

<html>
<head>
<title>DynAPI Examples - Functions</title>
<script language="JavaScript" src="./dynapisrc/dynapi.js"></script>
<script language="Javascript">
dynapi.library.setPath("./dynapisrc/");
dynapi.library.include("dynapi.library");
dynapi.library.include("dynapi.functions");
dynapi.library.include("dynapi.api");
</script>
</head>
<body>
<font face="arial" size="2">
<script>
var doc=document;
f=dynapi.functions;

doc.write("formatDate: "+f.formatDate(new Date(),"dddd, mmmm dd, yyyy"));
</script>
</font>
</body>
</html>


<A href="http://www.wbex.ru/Code/JavaScriptDownload/dynapi.zip">dynapi.zip( 791 k)</a>


Formats the current date and the "last modified" date of the document

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
  <HEAD>
    <TITLE>JsLib 1.3 - Exemple - date.js</TITLE>
    <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
    <META NAME="Author" CONTENT="Etienne CHEVILLARD">
    <SCRIPT TYPE="text/javascript" LANGUAGE="Javascript">
/* date.js
 * Role : formate la date du jour et la date de modification du document
 * Projet : JsLib
 * Auteur : Etienne CHEVILLARD (echevillard@users.sourceforge.net)
 * Version : 1.3
 * Creation : 22/04/2001
 * Mise a jour : 23/02/2005
 */
// --- Variables globales ---
// tableau pour les jours de la semaine
var date_jours=new Array("Dimanche","Lundi","Mardi","Mercredi","Jeudi","Vendredi","Samedi");
// tableau pour les mois de l"annee
var date_mois=new Array("janvier","f&eacute;vrier","mars","avril","mai","juin","juillet","ao&ucirc;t","septembre","octobre","novembre","d&eacute;cembre");
// obtient la date de derniere modification du document
var date_der=new Date(document.lastModified);
if ((date_der.getFullYear()+50)<(new Date()).getFullYear()) {
  date_der.setFullYear(parseInt(date_der.getFullYear())+100);
}
// --- Fonctions ---
// retourne la date du jour au format JJ mmm AAAA
function dateJour() {
  var dcour=new Date();
  var dj=dcour.getDate();
  if (dj=="1") dj="1er";
  var dm=dcour.getMonth();
  dm=date_mois[parseInt(dm)];
  return (dj+" "+dm+" "+dcour.getFullYear());
} // fin dateJour()
// retourne la date du jour au format JJ/MM/AAAA
function dateJourAbr() {
  var dcour=new Date();
  var dm=parseInt(dcour.getMonth())+1;
  if (dm<10) dm="0"+dm;
  return (dcour.getDate()+"/"+dm+"/"+dcour.getFullYear());
} // fin dateJourAbr()
// retourne la date du jour au format Jjj JJ mmm AAAA
function dateJourLng() {
  var dcour=new Date();
  var dj=dcour.getDate();
  if (dj=="1") dj="1er";
  var dm=dcour.getMonth();
  dm=date_mois[parseInt(dm)];
  var dd=dcour.getDay();
  dd=date_jours[parseInt(dd)];
  return (dd+" "+dj+" "+dm+" "+dcour.getFullYear());
} // fin dateJourLng()
// retourne la date de derniere modification au format JJ mmm AAAA
function dateModif() {
  var dj=date_der.getDate();
  if (dj=="1") dj="1er";
  var dm=date_der.getMonth();
  dm=date_mois[parseInt(dm)];
  return (dj+" "+dm+" "+date_der.getFullYear());
} // fin dateModif()
// retourne la date de derniere modification au format JJ/MM/AAAA
function dateModifAbr() {
  var dm=parseInt(date_der.getMonth())+1;
  if (dm<10) dm="0"+dm;
  return (date_der.getDate()+"/"+dm+"/"+date_der.getFullYear());
} // fin dateModifAbr()
// retourne la date de derniere modification au format Jjj JJ mmm AAAA
function dateModifLng() {
  var dd=date_der.getDay();
  dd=date_jours[parseInt(dd)];
  return (dd+" "+dateModif());
} // fin dateModifLng()
// verifie la date passee en parametre
function verifierDate(jour, mois, annee) {
  var jours_mois = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
  if (!jour || !mois || !annee)
    return false;
  if (!parseInt(jour) || !parseInt(mois) || !parseInt(annee))
    return false;
  if ((annee < 1900) || (annee > 9999))
    return false;
  if ((mois < 1) || (mois > 12))
    return false;
  if (annee/4 == parseInt(annee/4))
    jours_mois[1] = 29;
  if ((jour < 1) || (jour > jours_mois[parseInt(mois)-1]))
    return false;
  return true;
} // fin verifierDate(jour, mois, annee)
    </SCRIPT>
  </HEAD>
  <BODY>
    <H1>JsLib 1.3</H1>
    <HR>
    <H2>Exemple - date.js</H2>
    <NOSCRIPT>
      <P><I>Erreur : votre navigateur ne reconnait pas le Javascript ou est configur&eacute; pour ne
      pas prendre en compte le code Javascript. Dans ce dernier cas, vous pouvez modifier la
      configuration dans les pr&eacute;f&eacute;rences/options de votre navigateur.</I>
      <HR>
    </NOSCRIPT>
    <P>Date du jour au format <I>JJ/MM/AAAA</I> :&nbsp;
      <SCRIPT TYPE="text/javascript" LANGUAGE="Javascript">document.write(dateJourAbr());</SCRIPT>
    <P>Date du jour au format <I>JJ mmm AAAA</I> :&nbsp;
      <SCRIPT TYPE="text/javascript" LANGUAGE="Javascript">document.write(dateJour());</SCRIPT>
    <P>Date du jour au format <I>Jjj JJ mmm AAAA</I> :&nbsp;
      <SCRIPT TYPE="text/javascript" LANGUAGE="Javascript">document.write(dateJourLng());</SCRIPT>
    <P>Date de derni&egrave;re modification de cette page au format <I>JJ/MM/AAAA</I> :&nbsp;
      <SCRIPT TYPE="text/javascript" LANGUAGE="Javascript">document.write(dateModifAbr());</SCRIPT>
    <P>Date de derni&egrave;re modification de cette page au format <I>JJ mmm AAAA</I> :&nbsp;
      <SCRIPT TYPE="text/javascript" LANGUAGE="Javascript">document.write(dateModif());</SCRIPT>
    <P>Date de derni&egrave;re modification de cette page au format <I>Jjj JJ mmm AAAA</I> :&nbsp;
      <SCRIPT TYPE="text/javascript" LANGUAGE="Javascript">document.write(dateModifLng());</SCRIPT>
    <FORM ACTION="GET" NAME="f1" onSubmit="return false">
        Date &agrave; v&eacute;rifier au format <I>JJ/MM/AAAA</I> :&nbsp;
        <INPUT NAME="jour" TYPE="TEXT" SIZE=2 VALUE="1"> /
        <INPUT NAME="mois" TYPE="TEXT" SIZE=2 VALUE="01"> /
        <INPUT NAME="annee" TYPE="TEXT" SIZE=4 VALUE="2001">
        <INPUT NAME="verifier" TYPE="BUTTON" VALUE="V&eacute;rifier"
        onClick="if (verifierDate(document.f1.jour.value, document.f1.mois.value, document.f1.annee.value))
          alert("Date valide.")
          else alert("Date non valide.")">
      </FORM>    
      
  </BODY>
</HTML>


<A href="http://www.wbex.ru/Code/JavaScriptDownload/JsLib13.zip">JsLib13.zip( 311 k)</a>