Coder Social home page Coder Social logo

abap_date's Introduction

Класс для удобного использования даты в abap

Инициализация:

data: lo_date type ref to zcl_date.

lo_date = zcl_date=>create( '20200101' ). " 1 января 2020
" or
lo_date = zcl_date=>create( ). " текущая дата
" or
lo_date = zcl_date=>create( 
    iv_date = '20200101'
    iv_language = 'E'
). " инициализация с кастомным языком (default = sy-langu)

Геттеры и сеттеры

  1. get
    1. get_day -- получить номер дня в месяце (num2)
    2. get_month_number -- получить номер месяца (num2)
    3. get_year -- получить год (num4)
    4. get_language -- получить текущий язык (lang)
    5. get_date -- получить дату (dats)
    6. get_month_name -- получить название месяца на текущем языке (fcltx)
    7. get_short_month_name -- получить краткое название месяца на текущем языке (янв, фев...) (fcktx)
  2. set
    1. set_date -- установить дату (dats)
    2. set_day -- установить день (num2)
    3. set_year -- установить год (num4)
    4. set_month_number -- установить месяц (num2)
    5. set_language -- установить язык (lang)

Пример:

data: lo_date  type ref to zcl_date,
      lv_dats  type dats,
      lv_month type string.

lo_date = zcl_date=>create( ).
lo_date->set_year( 2001 ).
lo_date->set_month_number( 9 ).
lo_date->set_day( 11 ).

lv_dats = lo_date->get_date( ).
lv_month = lo_date->get_month_name( ).

write lv_dats. " 11 сентября 2001
write lv_month. " Сентябрь

Методы инстанса

  1. get_short_month_name_by_number
  2. get_month_name_by_number

(Получить имя месяца/короткое имя месяца по его номеру)

data: lv_month type string,
      lv_short type string.

" Эти методы никак не взаимодействуют с датой, которая хранится внутри объекта. 
" Лишь возвращают названия месяцев, переданных в аргументах 

lv_short = lo_date->get_short_month_name_by_number( 1 ). 
lv_month = lo_date->get_month_name_by_number( 1 ).

write lv_short. " Янв
write lv_month. " Январь

" Аналогично, с другими языками
lo_date->set_language( 'E' ).

lv_short = lo_date->get_short_month_name_by_number( 1 ). 
lv_month = lo_date->get_month_name_by_number( 1 ).

write lv_short. " Jan
write lv_month. " January
  1. minus
  2. plus

(Добавить/убавить день/месяц/год)

lo_date = zcl_date=>create( '20200101' ). " 01.01.2020

lo_date->minus( iv_years = 2 ). " 01.01.2018
lo_date->plus( iv_days = 3 ).   "04.01.2018
lo_date->plus( iv_months = 1 ). "04.02.2018

" Несколько аргументов
lo_date->plus( 
    iv_days  = 1
    iv_years = 2
). " 05.02.2020
  1. to_string *(Конвертировать дату в человеко-читаемую строку)

По умолчанию: в формате 01 Января 2020 ($dd $m $yyyy)*

Принимает один (опциональный) аргумент -- маску вывода Доступные ключевые слова в маске:

  1. $yyyy -- длинное представление года (2019)
  2. $yy -- короткое представление года (19)
  3. $m -- название месяца на текущем языке (Январь)
  4. $mmm -- короткое название месяца на текущем языке (Янв)
  5. $mm -- номер месяца (01)
  6. $dd -- день
data: lo_date     type ref to zcl_date,
      lv_formated type string.

lo_date = zcl_date=>create( '20200101' ). " 01.01.2020
lv_formated = lo_date->to_string( 'Месяц: $m Год: $yyyy День: $dd' ).

write lv_formated. " Месяц: Январь Год: 2020 День: 01

Статические методы

  1. obj_diff
  2. diff

Возвращает разницу в днях между двумя объектами dats(diff) / zcl_date (obj_diff)

data: lo_date1 type ref to zcl_date,
      lo_date2 type ref to zcl_date,
      lv_dats1 type dats,
      lv_dats2 type dats,
      lv_diff_dats type i,
      lv_diff_zcl_d type i.

lv_dats1 = '20200120'. " 20.01.2020
lv_dats2 = '20200101'. " 01.01.2020

lo_date1 = zcl_date=>create( lv_dats1 ).
lo_date2 = zcl_date=>create( lv_dats2 ).


" Разница в датах между двумя переменными dats
lv_diff_dats = zcl_date=>diff( iv_dats1 = lv_dats1
                               iv_dats2 = lv_dats2 ).

" Разница в датах между двумя объектами zcl_date
lv_diff_zcl_d = zcl_date=>obj_diff( iv_dat1 = lo_date1
                                    iv_dat2 = lo_date2 ).

write lv_diff_dats.  " 19
write lv_diff_zcl_d. " 19

abap_date's People

Contributors

mylittlecrab avatar

Watchers

James Cloos avatar  avatar

Forkers

zqhhn

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.