Extracting Values From Date and Time Data

When you are working with data stored as a Date, a Time, or a Datetime, you can extract information such as the week number from a date, the month name from a date, or the number of minutes in a particular time. The type of data to be extracted is specified by the parameter.

Syntax

Source[{#}Parameter]

Arguments

  • Source—variable of data type Date or Time or Datetime, or $date, $datim, or $clock
  • Parameter—extraction parameter that is valid for the data type

Extraction Parameters

Note: When the extraction code is preceded by a pound sign (#, the compiler will give warnings if an incorrect extraction code is used.

Extraction Parameters for Date
Parameter Meaning
D Day of month
M Month number
Y Year (four digits)
X Fiscal year (four digits)
W Week number (according to ISO 2015)
  • Week 1 begins on a Monday.
  • January 1 falls in week 1 if it is a Monday, Tuesday, Wednesday, or Thursday.
  • January 1 falls in the last week of the previous year if it is a Friday, Saturday, or Sunday.
mmm* Month name spelled out, lowercase
Mmm* Month name spelled out, initial capital
mmm Three-letter month abbreviation, lowercase
Mmm Three-letter month abbreviation, initial capital
A Day of week (Monday = 1)
aa* Day name spelled out, lowercase
Aa* Day name spelled out, initial capital
aa Two-letter abbreviation for day name, lowercase
Aa Two-letter abbreviation for day name, initial capital
aaa Three-letter abbreviation for day name, lowercase
Aaa Three-letter abbreviation for day, initial capital
Extraction Parameters for Time
Parameter Data extracted
H Hour (24-hour clock).
N Minutes.
S Seconds.
T Ticks (1/100 of a second).

Extraction parameters for Datetime data include all those available for the Date and Time data types, plus the following:

Extraction Parameters for Datetime
Parameter Data extracted
date The date part of a Datetime.
clock The time part of a Datetime.

Extracting Date and Time From Other Data Types

If you want to extract date or time values from a general variable or from a component or global variable defined with another data type, you must first use $date, $datim, or $clock to convert the variable to a Date or Time.

For example:

$1 = "26-aug-2009"
$2 = $date($1)[#Aaa]     ;value is "Tue"

Internal and External Formats

Uniface converts data to the external format when you extract any of the date or time components from a date or time value. In other words, your extraction does not take place on the data in the format as shown in the table, but on the data as formatted according to the packing code and display format definition. Uniface handles the data in the internal format, remembering the source format and applying both that and the destination format when converting the data to the external format.

Internal Formats for Handling Date, Time, Datetime, and Boolean Data Types
Type Internal Format Example
Date ccyymmdd 19940325
Time hhnnsstt 23595900
Datetime ccyymmddhhnnsstt 1994032523595900
Boolean F or T F

When a ProcScript assignment is used to copy data to a field or variable, the display format of the destination is applied. The internal format for Date, Time, and Datetime fields is ccyymmddhhnnsstt. The external format is produced from the internal representation only when that data is displayed or used in substitution.

# Extraction Indicator

The compiler only gives warnings on the use of extraction codes when the extraction code is preceeded by a pound symbol (#). The # defines that the parameter is an extraction code rather than a field or variable name. Field or variables with the same name as an extraction code are not examined and will not give spurious warnings.

For example, in the following code the use of the # symbol instructs the compiler to expect an extraction code:

$date($1)[#L]

However, L is not a valid code for date or time extraction, so the following warning is issued:

warning: 1000 - Invalid extraction code 'L' ignored. Valid 
                options:D,M,Y,X,W,mmm*,Mmm*,mmm,Mmm,A,aa*,Aa*,aa,Aa,aaa,Aaa

The warning message takes the data type into account to advise correctly on the valid options. For example, the following code:

szTime = $clock  ;szTime is a local variable of type 'time'
$1 = szTime[#X]

results in:

warning:   1000 - Invalid extraction code 'X' ignored. Valid options:H,N,S,T

The table shows the result of different extraction statements, given the following code:

;code snippet
variables
    datetime vMyDateTime
    date     vMyDate 
endvariables

vMyDateTime = "1-jan-2010 22:45:59"
vMyDate = vMyDateTime[#date]    ;extracts the date from vMyDateTime:  "1-jan-2010"                                  
$vMyString$ = $datetime         ;assigns current date and time to component variable of type String

$1 = ExtractionStatement
Extraction Statements and Results
ExtractionStatement Result
vMyDate[D] 1
vMyDate[M]

1

vMyDate[Y] 2010
vMyDate[X] 2009
vMyDate[W] 53
vMyDate[mmm*] january
vMyDate[Mmm*] January
vMyDate[mmm] jan
vMyDate[Mmm] Jan
vMyDate[A] 1
vMyDate[aa*] friday
vMyDate[aa] fr
vMyDate[Aa] Fr
vMyDate[aaa] fri
vMyDate[Aaa] Fri
vMyDateTime[clock] 22:45:59
vMyDateTime[H] 22
vMyDateTime[N] 45
vMyDateTime[S] 59
vMyDateTime[T] 00
$datetime($vMyString$)[date] Current date

Note:  In 2010, January 1 falls on a Friday, so it is considered to be week 53 of 2009 and the fiscal year is also considered 2009.

Related Topics