Skip to main content
hic
Former Employee
Former Employee

One Qlik function that occasionally causes confusion is the Date function. I have often seen errors caused by an incorrect usage of it, so today I will try to explain what the function does – and what it does not.

Interpretation vs Formatting

The first thing you should be aware of is that there are two different functions: Date#() and Date(). The first is an Interpretation function and the second is a Formatting function.

 

  • Interpretation functions use the textual value of the input, and convert this to a number.
  • Formatting functions use the numeric value of the input, and convert this to a text.

 

In both cases, the output is a dual, i.e. it has both a textual value and a numeric value. The textual value is displayed, whereas the numeric value is used for all numerical calculations and sorting.

 

The table below shows how to use the interpretation function Date#(). Note that the format code must match the input parameter.

 

Interpretation.png

 

This is very different from the formatting function Date(). Next table shows how to use this function. Note that the format code matches the format of the output text.

 

Formatting1.png

 

In real life, it is often useful to nest an interpretation function inside a formatting function:

 

Nested.png

 

Formatting vs Rounding

The second thing you should be aware of is that the Date() function and other formatting functions never change the numeric value of the input value.

 

This means that you can format a timestamp as a date only, without the time information. This can sometimes be confusing since there is a “hidden” value. In the table below, you can see that the input value corresponds to 12:00 in the middle of the day, but the Date() function effectively hides this from the textual output - but it remains in the the numeric value.

 

Formatting2.png

 

So what should you do if you want to remove the time part of the field, and just keep the date part? Well, obviously you must use a function that changes the numeric value: You need a Rounding function, e.g. DayStart() or Floor().

 

In the table below, you can compare the output of the Date() function with a couple of different rounding and formatting options.

 

Rounding.png

 

Summary

The above discussion is not relevant to dates only. It is just as relevant for Years, Weeks, hours, seconds and any other time interval. Further, it is relevant to a number of other functions:

 

Interpretation functions: Date#(), TimeStamp#(), Time#(), Interval#(), etc.

Formatting functions: Date(), TimeStamp(), Time(), Interval(), etc.

Rounding functions: Round(), Floor(), Ceil(), DayStart(), WeekStart(), MonthStart(), etc.

 

Combine these functions sensibly, and you will be able to round or format any way you want.

 

HIC

 

Further reading related to this topic:

Get the Dates Right

Why don’t my dates work?

39 Comments
mr_ramshini
Contributor III
Contributor III

Hi

How do I convert the current date to the shamsi date?

for example the current date is :'2016/16/5' and i want change it to shamsi date that it is :'1395/02/27'

thanks

0 Likes
740 Views
Not applicable

Very helpful. Thank you so much!

0 Likes
740 Views
hic
Former Employee
Former Employee

mr.ramshini

Regarding Shamsi date: Use the files found on Non-Gregorian calendars and replace

   Dual(Month &' '& Day &', '& Year, GDate) as SDate,

with

   Dual(Year &'/'& Num(Month,'00') &'/'& Num(Day,'00'), GDate) as SDate,

HIC

740 Views
qlikviewwizard
Master II
Master II

Very nice article and helpful HIC.

0 Likes
740 Views
neerajthakur
Creator III
Creator III

I have dimension field  [Invoice Date ] in my Line chart currently dates are in DD/MM/YYYY I want them in format of 14 Feb 2014, how can I achieve this with expression in field.

493 Views
Gabriel_Araya
Partner - Contributor III
Partner - Contributor III

Hi,

You can use Date([Invoice Date],'DD MMM YYYY')

 

Regards

Gabriel

467 Views
neerajthakur
Creator III
Creator III

I have already tried it, it does not work in dimension field for the desired results.

 

424 Views
barnabyd
Partner - Creator III
Partner - Creator III

G'day,

It may be that your [Invoice Date] is in text format and has not been converted into a date with the Date#() function. If that's the case then this might be what you need ...

Date( Date#( [Invoice Date], 'DD/MM/YYYY'' ), 'DD MMM YYYY')

If this fixes the problem then you probably should apply the Date#() in the load script when you first load up the date.

Cheers,

Barnaby.

390 Views
LeonardoSomoza
Partner - Contributor II
Partner - Contributor II

Today I saw this lesson almost 8 year laters than you made it and helped me so much!! Thank you Henric, brillant work !!

270 Views