Kỷ thuật xử lý ngày giờ với X++


19/06/2020- duocnt    895 Views    

NỘI DUNG

  1. Lấy ngày hôm nay của năm trước.
  2. Chuyển đổi UTCDateTime qua Date.
  3. Chuyển đổi Date qua UTCDateTime.
  4. Chuyển đổi DateTime qua String.
  5. Chuyển đổi Date qua String.
  6. Lấy và hiển thị giờ hiện tại.
  7. Tách thời gian từ DateTime và hiển thị ra string.
  8. Tạo thời gian cho Date.
  9. Chuyển qua thời gian của Local time.
  10. Lấy tuần trong năm theo ngày.
  11. Lấy ra số ngày giữa 2 ngày.
  12. Lấy ra ngày trước ngày được chỉ định.


CODE X++

1 - Lấy ngày hôm nay của năm trước

date     previousYear = prevYr(today());
Info(strFmt("Today: %1 - Today of last year: %2", any2Str(today()),any2Str(previousYear)));


2 - Chuyển đổi UCTDateTime qua Date

var test2 = DateTimeUtil::date(ProductionPlanTmp.PODate);


3 - Chuyển đổi Date qua UTCDateTime.

var test = DateTimeUtil::newDateTime(ProductionPlanTmp.SOReadyDate, 0, DateTimeUtil::getCompanyTimeZone());


4 - Chuyển đổi DateTime qua String.

datetime2Str(_ProdJournalTable.PostedDateTime,321)


5 - Chuyển đổi Date qua String.

date2Str(startDate,123,DateDay::Digits2,DateSeparator::Hyphen,DateMonth::Digits2,DateSeparator::Hyphen,DateYear::Digits4)


6 - Lấy và hiển thị giờ hiện tại.

       TimeInMS CurrentTime;
       str strTimeStart,strTimeEnd;
       CurrentTime =timeNow();
       strTimeStart =time2str(CurrentTime ,0,0);


7 - Tách thời gian từ DateTime và hiển thị ra string.

    TimeOfDay     _time;                  
_time  = DateTimeUtil::time(_ProdJournalTable.PostedDateTime);
time2Str(_time,TimeSeparator::Colon,TimeFormat::Hour24)


8 - Tạo thời gian cho Date.

        utcDateTime     rangeStart, rangeEnd;
        date            startDate,endDate;
        TimeOfDay       startTime,endTime;
        //get the day before current day
        startDate   =      14\03\2019;
        endDate     =      25\03\2019;
 
        //Set time
        startTime   =      str2Time("08:00:00 am");
        endTime     =      str2Time("23:00:00pm");
 
        // create new datetime objects to use in range
        rangeStart = DateTimeUtil::newDateTime(startDate,startTime);
        rangeEnd   = DateTimeUtil::newDateTime(endDate,endTime);


9 -Chuyển qua thời gian của Local Time.

    utcdatetime   localTime;
    localTime     = DateTimeUtil::applyTimeZoneOffset(this.CreatedDateTime,DateTimeUtil::getUserPreferredTimeZone());


10 - Lấy tuần trong năm theo ngày.

    int wkOfYr(date _date)


11 - Lấy ra số ngày giữa 2 ngày.

    date   _today         =      today();
    date  _date          =      30\05\2019;
    int   _dateDiff1      =      intvNo(_today,_date,IntvScale::Day);
    int   _dateDiff2      =      intvNo(_date,_today,IntvScale::Day);

12 - Lấy ra ngày trước ngày được chỉ định.

        int previousDate;
        previousDate=7;
        date specifiedDate;
        specifiedDate=29\04\2018;
        date NDate;
        NDate = specifiedDate-previousDate;







Góp ý kiến

;
;