Enas S3efan
Enas S3efan

Reputation: 81

display current date and time in Arabic format using as3

hi can I display current date and time in Arabic format using flash CS6 and as3

var now:Date=new Date()
    this["st_rep"+c]["date"].text=now.toDateString()
            this["st_rep"+c]["time"].text=now.toTimeString().slice(0,8)

thank's in advance

Upvotes: 0

Views: 884

Answers (4)

T Graham
T Graham

Reputation: 1339

Flash Player 10.1 added a DateTimeFormatter as part of the flash.globalization package. It should at least partially help you out.

import flash.globalization.DateTimeFormatter;

var d:Date = new Date();
// The locale for Jordan is "ar-JO"
var dtf:DateTimeFormatter = new DateTimeFormatter("ar-JO", DateTimeStyle.LONG, DateTimeStyle.LONG);
trace(dtf.format(d));

// Output on a PC running an English version of Windows:
// 11 آب, 2013 01:57:06 م

You can get right-to-left text if you use a TLF textfield. Note, though, that TLF has been removed from the current version of Flash Pro (Flash Pro CC 1.0), and it's not clear if/when there will be a replacement.

Upvotes: 0

Mahmoud Badri
Mahmoud Badri

Reputation: 1296

nice solution for the arabic numbers :). You can write a simpler version of the function like this:

function convert_arabic(str:String)
{
    var eng_num = ["0","1","2","3","4","5","6","7","8","9"];
    var ara_num = ["۰","۱","۲","۳","٤","٥","٦","۷","۸","۹"];

    for(var f:int = 0; f < eng_num.length; f++)
        str = str.replace(new RegExp(eng_num[f], "g"), ara_num[f]);

    return str;
}

regards,

Upvotes: 0

Enas S3efan
Enas S3efan

Reputation: 81

this is what I do

var eng_num=["0","1","2","3","4","5","6","7","8","9"]
var ara_num=["۰","۱","۲","۳","٤","٥","٦","۷","۸","۹"]

function convert_arabic(str:String)
{
var new_str=""

for(var f=0;f<str.length;f++)
{
    var temp=str.slice(f,f+1)
    var xx:int=int(temp)
    if(String(Number(temp)) == String(temp))
    {
    for(var d=0;d<=eng_num.length;d++)
    {
        if(temp == eng_num[d])
        {
            new_str+=ara_num[d]
        }

    }
    }
    else
    {
                        new_str+=temp

    }
}
return new_str

}

Upvotes: 1

Mahmoud Badri
Mahmoud Badri

Reputation: 1296

There is no magic way to do it, you have create a formatter function. Something like this:

var now:Date = new Date()

trace(formatDate(now.toDateString()));

function formatDate(dateStr:String)
{
    var dateParts:Array = dateStr.split(" ");
    var dayName:String = getDayName(dateParts[0]);
    var monthName:String = getMonthName(dateParts[1]);
    var day:String = dateParts[2];
    var year:String = dateParts[3];

    return year + " " + monthName + " " + day + " " + dayName;
}

function getMonthName(month:String):String
{
    switch (month)
    {
        case "Jan": return "يناير";
        case "Feb": return "فبراير";
        case "Mar": return "مارس";
        case "Apr": return "إبريل";
        case "May": return "مايو";
        case "Jun": return "يونيو";
        case "Jul": return "يوليو";
        case "Aug": return "أغسطس";
        case "Sep": return "سبتمبر";
        case "Oct": return "أكتوبر";
        case "Nov": return "نوفمبر";
        case "Dec": return "ديسمبر";
    }

    return null;
}

function getDayName(day:String):String
{
    switch (day)
    {
        case "Sat": return "السبت";
        case "Sun": return "الأحد";
        case "Mon": return "الإثنين";
        case "Tue": return "الثلاثاء";
        case "Wed": return "الأربعاء";
        case "Thu": return "الخميس";
        case "Fri": return "الجمعة";
    }

    return null;
}

note that you cannot make the numbers appear in arabic from flash as to what I know. If you must show the numbers in arabic you can create a 10 frames movieclip in the library each frame holds 1 arabic digit image and create a formatter function that attaches clips from from the library according to each arabic digit.

Upvotes: 0

Related Questions