- La funzione FormatDateTime in JavaScript -
 
COSA SERVE PER QUESTO TUTORIAL
Download | Chiedi sul FORUM | Glossario Qualche conoscenza di base dell'HTML e di Javascript - Blocco Note o un qualsiasi editor di codice HTML.
Trasposizione della funzione FormatDateTime di VB in JavaScript

CODICE SORGENTE

<html>

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

function FormatDateTime(DateTime, FormatType)
{
    var gg;
    var mm;
    
    if (DateTime == null)
        return (false);
    
    if (FormatType < 0)
        FormatType = 1;
    
    if (FormatType > 2)
        FormatType = 2;
    
    var strDate = new String(DateTime);
    
    if (strDate.toUpperCase() == "NOW")
    {
        var myDate = new Date();
        strDate = String(myDate);
    }
    else
    {
        var myDate = new Date(DateTime);
        strDate = String(myDate);
    }
    
    var Day = new String(strDate.substring(0, 3));
    if (Day == "Sun") Day = "Sunday";
    if (Day == "Mon") Day = "Monday";
    if (Day == "Tue") Day = "Tuesday";
    if (Day == "Wed") Day = "Wednesday";
    if (Day == "Thu") Day = "Thursday";
    if (Day == "Fri") Day = "Friday";
    if (Day == "Sat") Day = "Saturday"; 
    
    var Month = new String(strDate.substring(4, 7)), MonthNumber = 0;
    if (Month == "Jan") { Month = "January"; MonthNumber = "01"; }
    if (Month == "Feb") { Month = "February"; MonthNumber = "02"; }
    if (Month == "Mar") { Month = "March"; MonthNumber = "03"; }
    if (Month == "Apr") { Month = "April"; MonthNumber = "04"; }
    if (Month == "May") { Month = "May"; MonthNumber = "05"; }
    if (Month == "Jun") { Month = "June"; MonthNumber = "06"; }
    if (Month == "Jul") { Month = "July"; MonthNumber = "07"; }
    if (Month == "Aug") { Month = "August"; MonthNumber = "08"; }
    if (Month == "Sep") { Month = "September"; MonthNumber = "09"; }
    if (Month == "Oct") { Month = "October"; MonthNumber = "10"; }
    if (Month == "Nov") { Month = "November"; MonthNumber = "11"; }
    if (Month == "Dec") { Month = "December"; MonthNumber = "12"; }
    
    var curPos = 11;
    var MonthDay = new String(strDate.substring(8, 10));
    if (MonthDay.charAt(1) == " ")
    {
        MonthDay = "0" + MonthDay.charAt(0);
        curPos--;
    } 
    
    var Year = new String(strDate.substring(strDate.length - 4, strDate.length));
    
    // Format Type decision time!
    if (FormatType == 1)
        strDate = Day + ", " + Month + " " + MonthDay + ", " + Year;
    else if (FormatType == 2)
        strDate = MonthDay + "/" + MonthNumber + "/" + Year;
    
    return strDate;
}

</script>
</head>
<body>

<p>
<input type="text" name="txtDate" value="21-09-1979">
<select name="lstDateType">
<option value="0">vbGeneralDate</option>
<option value="1">vbLongDate</option>
<option value="2">vbShortDate</option>
</select>
<input type="button" value="FormatDateTime" 
 onclick="alert(FormatDateTime(txtDate.value, lstDateType.item(lstDateType.selectedIndex).value))">
</p>

</body>
</html>

<< INDIETRO by Vallerugo Luca