Monday, January 9, 2012

Function แปลงวันเดือนปีตาม formatที่ต้องการ

สร้าง report custom function ชื่อ DateTimeFunc

Function (datetimeVar p_date , stringvar p_formatdate )

//stringvar m_date:= totext("10/01/2012");    // Set Date = "MM/DD/YYYY" & Add Date
stringvar m_date:=  ToText(p_date);
datevar d_date:= date(m_date);
//stringvar m_format:="DD,MMMM-YYYY";   // Set Date format
//stringvar m_format:="วว ดดด ปปปป";   // Set Date format
stringvar m_format:= p_formatdate ;

numbervar vDay;
numbervar vMonth;
numbervar vYear;
numbervar vWeekDay;
stringvar yyyy;
stringvar yy;
stringvar Thaiyyyy;
stringvar Thaiyy;
numbervar idx;
stringvar FirstStr;
stringvar LastStr;
stringvar OutFormat;
stringvar Str1;
stringvar Str2;
stringvar array LongMth:=["มกราคม","กุมภาพันธ์","มีนาคม","เมษายน","พฤษภาคม","มิถุนายน","กรกฏาคม","สิงหาคม","กันยายน","ตุลาคม","พฤศจิกายน","ธันวาคม"];
stringvar array ShortMth:= ["ม.ค.","ก.พ.","มี.ค.","เม.ย.","พ.ค.","มิ.ย.","ก.ค.","ส.ค.","ก.ย.","ต.ค.","พ.ย.","ธ.ค."];
stringvar array ELongMth:=["January","February","March","April","May","June","July","August","September","October","November","December"];
stringvar array EShortMth:=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
stringvar array LongWeek:=["อาทิตย์","จันทร์","อังคาร","พุธ","พฤหัส","ศุกร์","เสาร์"];
stringvar array ShortWeek:=["อา","จ.","อ.","พ.","พฤ","ศ.","ส."];
stringvar array ElongWeek:=["Sunday","Monday","Tuesday","Webnesday","Thursday","Friday","Saturday"];
stringvar array EShortWeek:=["Sun","Mon","Tue","Wen","Thu","Fri","Sat"];
if trim(m_date) = "" then
    (
        "  /  /    "
    );
// Split date to day month year
vDay:= Day(d_date);
vMonth:=Month(d_date);
vYear:=Year(d_date);
vWeekDay:= WeekDay(d_date);
    OutFormat:= m_Format;
if InStr(OutFormat,"ป") > 0 Or InStr(OutFormat,"ด") > 0 Or InStr(OutFormat,"ว") > 0 then
(
    Thaiyyyy:= totext(vYear+543,"0000");
    Thaiyy:= Mid(Thaiyyyy,3,2);
    // ปี
    if InStr(OutFormat,"ปปปป") > 0 Then
    (
        idx:= InStr(OutFormat,"ปปปป");
        if idx = 1 then
            (FirstStr:= "")
        else
            (FirstStr:= Mid(OutFormat,1,idx-1));
        LastStr:= Mid(OutFormat,idx+4);
        OutFormat:= FirstStr & Thaiyyyy & LastStr;
    )
    else if InStr(OutFormat,"ปปป") > 0 then
    (
        idx:= InStr(OutFormat,"ปปป");
        if idx = 1 then
            (FirstStr:= "")
        else
            (FirstStr:= Mid(OutFormat,1,idx-1));
        LastStr:= Mid(OutFormat,idx+3);
        OutFormat:= FirstStr & Thaiyyyy & LastStr;
    )
    else if InStr(OutFormat,"ปป") > 0 then
    (
        idx:= InStr(OutFormat,"ปป");
        if idx = 1 then
            (FirstStr:= "")
        else
            (FirstStr:= Mid(OutFormat,1,idx-1));
        LastStr:= Mid(OutFormat,idx+2);
        OutFormat:= FirstStr & Thaiyy & LastStr;
    )
    else if InStr(OutFormat,"ป") > 0 then
    (
        idx:= inStr(OutFormat,"ป");
        if idx = 1 then
            (FirstStr:= "")
        else
            (FirstStr:= Mid(OutFormat,1,idx-1));
        LastStr:= Mid(OutFormat,idx+1);
        OutFormat:= FirstStr & Thaiyy & LastStr;
    ) ;
 
    // วัน
    if InStr(OutFormat,"วววว") > 0 then
    (
        idx:= InStr(OutFormat,"วววว");
        if idx = 1 then
            (FirstStr:= "")
        else
            (FirstStr:= Mid(OutFormat,1,idx-1));
        LastStr:= Mid(OutFormat,idx+4);
        OutFormat:= FirstStr & LongWeek[vWeekDay] & LastStr;
    )
    else if InStr(OutFormat,"ววว") > 0 then
    (
        idx:= InStr(OutFormat,"ววว");
        if idx = 1 then
            (FirstStr:= "")
        else          
            (FirstStr:= Mid(OutFormat,1,idx-1));
        LastStr:= Mid(OutFormat,idx+3);
        OutFormat:= FirstStr & ShortWeek[vWeekDay] & LastStr;
    );
   
    if InStr(OutFormat,"วว") > 0 then
    (
        idx:= InStr(OutFormat,"วว");  
        if idx = 1 then
            (FirstStr:="")
        else
            (FirstStr:= Mid(OutFormat,1,idx-1));
        LastStr:= Mid(OutFormat,idx+2);
        OutFormat:= FirstStr & totext(vDay,"00") & LastStr;
    )
    else if InStr(OutFormat,"ว") > 0 then
    (
        idx:= InStr(OutFormat,"ว");
        if idx = 1 then
            (FirstStr:="")
        else
            (FirstStr:= Mid(OutFormat,1,idx+1));
        LastStr:= Mid(OutFormat,idx+1);
        OutFormat:= FirstStr & totext(vDay,"#0") & LastStr;
    );
 
    // เดือน
    if InStr(OutFormat,"ดดดด") > 0 then
    (
        idx:= InStr(OutFormat,"ดดดด");
        if idx = 1 then
            (FirstStr:="")
        else          
            (FirstStr:= Mid(OutFormat,1,idx-1));
        LastStr:= Mid(OutFormat,idx+4);
        //ตัด ด ทิ้งทั้งหมดกรณีใส่เกินมาเช่น ดดดดด,ดดดดดด
        While InStr(FirstStr,"ด") > 0 do
        (
            Str1:= Mid(FirstStr,1,InStr(FirstStr,"ด")-1);
            Str2:= Mid(FirstStr,InStr(FirstStr,"ด")+1);                              
            FirstStr:= Str1 & Str2;    
        );        
        // ตัด ด ทิ้งทั้งหมด
        While InStr(LastStr,"ด") > 0 do
        (
            Str1:= Mid(LastStr,1,InStr(LastStr,"ด")-1);
            Str2:= Mid(LastStr,InStr(LastStr,"ด")+1);
            LastStr:= Str1 & Str2;
        );
        OutFormat:= FirstStr & LongMth[vMonth] & LastStr;
    )
    else if InStr(OutFormat,"ดดด") > 0 then
    (
        idx:= InStr(OutFormat,"ดดด");
        if idx = 1 then
            (FirstStr:="")
        else
            (FirstStr:= Mid(OutFormat,1,idx-1));
        LastStr:= Mid(OutFormat,idx+3);
        OutFormat:= FirstStr & ShortMth[vMonth] & LastStr;
    )
    else if InStr(OutFormat,"ดด") > 0 then
    (
        idx:= InStr(OutFormat,"ดด");
        if idx = 1 then
            (FirstStr:="")
        else
            (FirstStr:= Mid(OutFormat,1,idx-1));
        LastStr:= Mid(OutFormat,idx+2);
        OutFormat:= FirstStr & totext(vMonth,"00") & LastStr;
    )
    else if InStr(OutFormat,"ด") > 0 then
    (
        idx:= InStr(OutFormat,"ด");
        if idx = 1 then
            (FirstStr:="")
        else
            (FirstStr:= Mid(OutFormat,1,idx-1));
        LastStr:= Mid(OutFormat,idx+1);
        OutFormat:= FirstStr & totext(vMonth,"#0") & LastStr;
    );
)
// กรณีภาษาอังกฤษ
else
(
    OutFormat:= ucase(OutFormat);
    yyyy:= totext(vYear,"0000");
    yy:= Mid(yyyy,3,2);
    // Year
    if InStr(OutFormat,"YYYY") > 0 then
    (
        idx:= InStr(OutFormat,"YYYY");
        if idx = 1 then
            (FirstStr:="")
        else
            (FirstStr:= Mid(OutFormat,1,idx-1));
        LastStr:= Mid(OutFormat,idx+4);
        OutFormat:= FirstStr & yyyy & LastStr;
    )
    else if InStr(OutFormat,"YYY") > 0 then
    (
        idx:= InStr(OutFormat,"YYY");
        if idx = 1 then
            (FirstStr:="")
        else
            (FirstStr:= Mid(OutFormat,1,idx-1));
        LastStr:= Mid(OutFormat,idx+3);
        OutFormat:= FirstStr & yyyy & LastStr;
    )
    else if InStr(OutFormat,"YY") > 0 then
    (
        idx:= InStr(OutFormat,"YY");
        if idx = 1 then
            (FirstStr:="")
        else
            (FirstStr:= Mid(OutFormat,1,idx-1));
        LastStr:= Mid(OutFormat,idx+2);
        OutFormat:= FirstStr & yy & LastStr;
    )
    else if InStr(OutFormat,"Y") > 0 then
    (
        idx:= InStr(OutFormat,"Y");
        if idx = 1 then
            (FirstStr:="")
        else
            (FirstStr:= Mid(OutFormat,1,idx-1));
        LastStr:= Mid(OutFormat,idx+1);
        OutFormat:= FirstStr & yy & LastStr;
    );
    // Day
    if InStr(OutFormat,"DDDD") > 0 then
    (
        idx:= InStr(OutFormat,"DDDD");
        if idx = 1 then
            (FirstStr:="")
        else
            (FirstStr:= Mid(OutFormat,1,idx-1));
        LastStr:= Mid(OutFormat,idx+4);
        OutFormat:= FirstStr & ELongWeek[vWeekDay] & LastStr;      
    )
    else if InStr(OutFormat,"DDD") > 0 then
    (
        idx:= InStr(OutFormat,"DDD");
        if idx = 1 then
            (FirstStr:="")
        else
            (FirstStr:= Mid(OutFormat,1,idx-1));
        LastStr:= Mid(OutFormat,idx+3);
        OutFormat:= FirstStr & EShortWeek[vWeekDay] & LastStr;
    )
    else if InStr(OutFormat,"DD") > 0 then
    (
        idx:= InStr(OutFormat,"DD");
        if idx = 1 then
            (FirstStr:= "")
        else  
            (FirstStr:= Mid(OutFormat,1,idx-1));
        LastStr:= Mid(OutFormat,idx+2);
        OutFormat:= FirstStr & totext(vDay,"00") & LastStr;
    )
    else if InStr(OutFormat,"D") > 0 then
    (
        idx:= InStr(OutFormat,"D");
        if idx = 1 then
            (FirstStr:="")
        else
            (FirstStr:= Mid(OutFormat,1,idx-1));
        LastStr:= Mid(OutFormat,idx+1);
        OutFormat:= FirstStr & totext(vDay,"#0") & LastStr;
    );
 
    // Month
    if InStr(OutFormat,"MMMM") > 0 then
    (
        idx:= InStr(OutFormat,"MMMM");
        if idx = 1  then
            (FirstStr:="")
        else
            (FirstStr:= Mid(OutFormat,1,idx-1));
        LastStr:= Mid(OutFormat,idx+4);
        // ตัด  M   ทิ้งทั้งหมด กรณีใส่เกินมาเช่น MMMMM, MMMMMM
        While InStr(FirstStr,"M") > 0 do
        (
            Str1:= Mid(FirstStr,1,InStr(FirstStr,"M")-1);
            Str2:= Mid(FirstStr,InStr(FirstStr,"M")+1);
            FirstStr:= Str1 & Str2;
        );
        //  ตัด M ทิ้งทั้งหมด
        While InStr(LastStr,"M") > 0 do
        (
            Str1:= Mid(LastStr,1,InStr(LastStr,"M")-1);
            Str2:= Mid(LastStr,InStr(LastStr,"M")+1);
            LastStr:= Str1 & Str2;
        );
        OutFormat:= FirstStr & ELongMth[vMonth] & LastStr;
    )
    else if InStr(OutFormat,"MMM") > 0 then
    (
        idx:= InStr(OutFormat,"MMM");
        if idx = 1 then
            (FirstStr:="")
        else
            (FirstStr:= Mid(OutFormat,1,idx-1));
        LastStr:= Mid(OutFormat,idx+3);
        OutFormat:= FirstStr & EShortMth[vMonth] & LastStr;
    )
    else if InStr(OutFormat,"MM") > 0 then
    (
        idx:= InStr(OutFormat,"MM");
        if idx = 1 then
            (FirstStr:="")
        else
            (FirstStr:= Mid(OutFormat,1,idx-1));
        LastStr:= Mid(OutFormat,idx+2);
        OutFormat:= FirstStr & totext(vMonth,"00") & LastStr;
    )
    else if InStr(OutFormat,"M") > 0 then
    (
        idx:= InStr(OutFormat,"M");
        if idx = 1 then
            (FirstStr:="")
        else
            (FirstStr:= Mid(OutFormat,1,idx-1));
        LastStr:= Mid(OutFormat,idx+1);
        OutFormat:= FirstStr & totext(vMonth,"#0") & LastStr;
    );
);

วิธีเรียกใช้ DateTimeFunc(currentdatetime,"วววว ,วว ดดดด ปปปป");

output :
อังคาร ,10 มกราคม 2555

No comments:

Post a Comment