Hive Date Functions – all possible Date operations

Date Functions are used to apply different transformations on DATE datatype in HIVE. DATE consists of primarily 3 components – YEAR , MONTH & DAY. Any function which help us in identifying, extract or modify value of these DATE components can be classified as DATE Function.

Hive Date Functions
Hive Date Functions

Handling DATE in HIVE is not as easy as compared to many traditional RDBMS in the market. One of the reason why I feel so is the inability of HIVE to throw proper error when dealing with wrong date formats. If any function or date format is not compatible or is not used correctly, it will give NULL as output in place of error unless it is syntax error. This makes identification of wrong date manipulation in HIVE more tricky.

hive date functions

There are many date functions available in hive which you can use and below is the list of most common hive date functions.

hive current_date : fetch today's date in hive

hive> select current_date;
OK
2021-04-27

hive date_add : add number of days to given date

hive> select current_date as today_date, date_add(current_date,10) as add_10_days;
OK
today_date      add_10_days
2021-04-27      2021-05-07
Time taken: 0.046 seconds, Fetched: 1 row(s)

Note : You can also give negative number to subtract days using date_add function.

hive> select current_date as today_date, date_add(current_date,-10) as `add_-10_days`;
OK
today_date      add_-10_days
2021-04-27      2021-04-17
Time taken: 0.045 seconds, Fetched: 1 row(s)

hive date_sub : subtract number of days from given date

hive> select current_date as today_date, date_sub(current_date,10) as sub_10_days;
OK
today_date      sub_10_days
2021-04-27      2021-04-17
Time taken: 0.045 seconds, Fetched: 1 row(s)

Note : If you will pass negative number to date_sub , it will work as date_add.

hive> select current_date as today_date, date_sub(current_date,-10) as `sub_-10_days`;
OK
today_date      sub_-10_days
2021-04-27      2021-05-07
Time taken: 0.045 seconds, Fetched: 1 row(s)

hive current day minus 1 day

hive> select current_date as today_date, date_sub(current_date,1) as sub_1_day;
OK
today_date      sub_1_day
2021-04-27      2021-04-26
Time taken: 0.039 seconds, Fetched: 1 row(s)

hive day of week : fetch weekday number – sunday is 1, monday is 2 …sat is 7

hive> select extract(dayofweek from current_date);
OK
_c0
3
Time taken: 0.044 seconds, Fetched: 1 row(s)

hive add_months : add months to given date

hive> select current_date as today_date, add_months(current_date,1) as add_1_month;
OK
today_date      add_1_month
2021-04-27      2021-05-27
Time taken: 0.042 seconds, Fetched: 1 row(s)

hive first day of month : use trunc (truncate) in hive

hive> select current_date as today_date, trunc(current_date,'MM') as first_day_month;
OK
today_date      first_day_month
2021-04-27      2021-04-01
Time taken: 0.04 seconds, Fetched: 1 row(s)

hive first day of year : use trunc (truncate) in hive

hive> select current_date as today_date, trunc(current_date,'YYYY') as first_day_year;
OK
today_date      first_day_year
2021-04-27      2021-01-01
Time taken: 0.041 seconds, Fetched: 1 row(s)

hive extract year from date

hive> select current_date as today_date, extract(year from current_date) as extract_year;
OK
today_date      extract_year
2021-04-27      2021
Time taken: 0.042 seconds, Fetched: 1 row(s)

hive get month from date

hive> select current_date as today_date, extract(month from current_date) as extract_month;
OK
today_date      extract_month
2021-04-27      4
Time taken: 0.042 seconds, Fetched: 1 row(s)

hive last day of month

hive> select current_date as today_date, last_day(current_date) as last_day_month;
OK
today_date      last_day_month
2021-04-27      2021-04-30
Time taken: 0.039 seconds, Fetched: 1 row(s)

hive datediff

Hive DATEDIFF function is used to calculate the difference between two dates. Datediff returns the number of days between two input dates. DATEDIFF function accepts two input parameters i.e. end date and start date. Make sure to pass end date as first parameter and start date as second parameter to DATEDIFF function in hive. Below is the example:

hive> select datediff('2021-04-27','2021-04-20') as days_diff;
OK
days_diff
7
Time taken: 0.037 seconds, Fetched: 1 row(s)

hive date format

In the below examples have used DATE_FORMAT which is available from Hive 1.2 onwards. There is more than one way to achieve same result and we have listed it where ever possible. Also you can use your own methods to get desired result like for adding a year you can try to add 12 months in place of a year. You can use most of the DATE FORMAT combinations together to change DATE into required format.

DATE_FORMAT will give correct output when your input date is in format 'yyyy-MM-dd'. If it is in any other format , it will result in NULL output or error. In order to handle such scenarios you have to use combination of from_unixtime & unix_timestamp.

select from_unixtime(unix_timestamp('05-JAN-20', 'dd-MMM-yy'),'yyyy-MM-dd');

30+ hive date format examples

Title SQL Code Output Description
Current Date select current_date; 2018-06-15 How to select present date
Change Date Format select date_format(current_date,'MM/dd/YYYY'); 06/15/2018 Display date in some other date format
Change Date Format select date_format(current_date,'dd-MMM-YYYY'); 15-Jun-2018 Display Month Name in Date
Change Date Format select date_format(current_date,'dd-MMMM-YYYY'); 15-June-2018 Display full Month Name in Date
Day of the Week select date_format(current_date,'E'); Fri Display Day Name of the week
Day of the Week select date_format(current_date,'EEEE'); Friday Display Day Name(Full) of the week
Day of the Week select date_format(current_date,'u'); 5 Display Day Number of the week (1=Mon..5=Fri..7=Sun)
Day of the Year select date_format(current_date,'D'); 166 Display Day of the Year out of 365/366
Day Occurrence of the Month select date_format(current_date,'YYYY-MM-dd EEEE(F)'); 2018-06-15 Friday(3) Display occurrence of day in the month like 3rd Friday of month
Week of the year select date_format(current_date,'w'); 24 Display which week of the year date belongs to
Week of the month select date_format(current_date,'W'); 3 Display which week of the month date belongs to
TimeZone info select date_format(current_date,'z:Z'); UTC:+0000 Display current TIMEZONE info
Year select year(current_date); 2018 Display Year from the date
Month select month(current_date); 6 Display Month from the date
Day select day(current_date); 15 Display Day from the date
Week of the year select weekofyear(current_date); 24 Display which week of the year date belongs to
Add Days select date_add(current_date,2); 2018-06-17 Display date after adding days to it
Add Days select date_format(current_date + interval '2' day,'YYYY-MM-dd'); 2018-06-17 Display date after adding days to it
Add Months select add_months(current_date,2); 2018-08-15 Display date after adding months to it
Add Months select date_format(current_date + interval '2' month,'YYYY-MM-dd'); 2018-08-15 Display date after adding months to it
Add Year select date_format(current_date + interval '2' year,'YYYY-MM-dd'); 2020-06-15 Display date after adding year/s
Add Year select add_months(current_date,12); 2019-06-15 Display date after adding year (or 12 months)
Subtract Days select date_add(current_date,-2); 2018-06-13 Display date after subtracting days to it
Subtract Days select date_sub(current_date,2); 2018-06-13 Display date after subtracting days to it
Subtract Days select date_format(current_date – interval '2' day,'YYYY-MM-dd'); 2018-06-13 Display date after subtracting days
Subtract Days select date_format(current_date + interval '-2' day,'YYYY-MM-dd'); 2018-06-13 Display date after subtracting days
Subtract Months select add_months(current_date,-2); 2018-04-15 Display date after subtracting months
Subtract Months select date_format(current_date + interval '-2' month,'YYYY-MM-dd'); 2018-04-15 Display date after subtracting months
Subtract Months select date_format(current_date – interval '2' month,'YYYY-MM-dd'); 2018-04-15 Display date after subtracting months
Subtract Year select date_format(current_date + interval '-2' year,'YYYY-MM-dd'); 2016-06-15 Display date after subtracting Year
Subtract Year select add_months(current_date,-12); 2017-06-15 Display date after subtracting Year (12 months)
Subtract 2 dates select datediff('2018-12-31',current_date); 199 Display difference of 2 dates

For more details about different formats available kindly check our previous post: Handle Date and Timestamp in HIVE like a pro

I will continue to update this table with more operations like timestamp handling etc. However if you are looking for anything specific, let me know by dropping a comment. I will try to address that quickly.
Hope this helps.

20 thoughts on “Hive Date Functions – all possible Date operations”

  1. I have a product table with manufacturing date (Prod_id, prod_name, mfg_date). I need for the following:

    I have created data and stored. And i have queried for the following.

    1. I have added 30 days expiry date from the manufacturer date.

    select prod_id, prod_name, mfg_date, date_add (from_unixtime(unix_timestamp(mfg_date,'dd-MM-yyyy')),30) as exp_date from Products;
    OK
    prod_id prod_name mfg_date exp_date
    1001 Biscuits 21-05-2021 2021-06-20
    1002 Dates 22-05-2021 2021-06-21
    1003 Ghee 23-05-2021 2021-06-22
    1004 CoffeePowder 24-05-2021 2021-06-23
    1005 Oil 25-05-2021 2021-06-24
    1006 Groundnut 26-05-2021 2021-06-25
    1007 VitaminSyrup 27-05-2021 2021-06-26
    1008 Dhal 28-05-2021 2021-06-27
    1009 Shampoo 28-05-2021 2021-06-27
    1010 CreamPowder 28-05-2021 2021-06-27
    Time taken: 0.212 seconds, Fetched: 10 row(s)
    hive>

    2. I need number of days for the following using date difference from current date to expiry date. Can any one suggest how to query for the above question.

    Thanks

    1. Hi Balaji
      try this :
      select datediff(exp_date,current_date) as days_pending.

      If you want this in same query then you may try this:
      datediff(date_add (from_unixtime(unix_timestamp(mfg_date,'dd-MM-yyyy')),30),current_date) as days_pending.

      You cannot use derived columns directly in hive. So either replace it with column definition or create derived table and use that on top of it.

      Also will suggest to keep all date columns in default format 'yyyy-MM-dd' else you may have to apply date format before applying most of the date transformations.

      Best,
      Raj

    1. Hi Ash
      try this:
      hive> select extract(day from last_day('2021-01-15')) as jan, extract(day from last_day('2021-02-15')) as feb,extract(day from last_day('2021-03-15')) as mar,extract(day from last_day('2021-04-15')) as apr;
      OK
      jan feb mar apr
      31 28 31 30
      Time taken: 0.046 seconds, Fetched: 1 row(s)

      Replace '2021-01-15' with the input date. Will this work ?

        1. I will recommend to identify if the problem is with last_day function or extract. So run below two commands :

          select last_day('2021-01-15');
          select extract(day from current_date);

          Replace the logic with traditional method for the one which is failing.

  2. Hi,
    How to get the output as Dec 31 of the Year prior to some pre-existing date value from a table.
    Example: Data in a table is 5th March of 2020, 03/05/2020
    And expecting an output as 31st of Dec 2019, 12/31/2019.
    I was able to think of adding dateadd function to get the previous year but not able to figure out of adding Dec 31 as the date.
    dateadd(year, -1, sp.start_date)

  3. Raj,

    What would be the best way to calculate the 1st of a month? For example, I want the 1st day of the month two months earlier. Today is 2020-09-02 minus 2 months is 2020-07-02 but I want 2020-07-01.

    Would something like the below work?

    select date_format( add_months( current_date, -2 ), 'YYYY-MM-01' );

    1. Hi Sam,
      Yes the approach you have mentioned will definitely work and thanks for sharing this.
      You can also try TRUNC function to truncate date to first of month.
      select trunc(add_months(current_date,-2),'MM');

      Best,
      Raj

  4. Hi,
    How can I convert day to week starting Monday/Sunday

    e.g. if I have dates(MM/DD/YYYY) as below –
    08/03/2020
    08/04/2020
    08/05/2020
    08/06/2020
    08/07/2020
    08/08/2020
    08/09/2020
    I expect 08/03/2020 for all of them, considering I am taking Monday as the start of the week.

    1. Hi Sourav
      Please try below:
      next_day(date_sub(from_unixtime(unix_timestamp(col1,'MM/dd/yyyy'),'yyyy-MM-dd'),7),'Mo')
      *col1 is column in the table defined as string which stores date in the MM/dd/yyyy format.
      So first you have to convert it into the preferred format which is yyyy-MM-dd and then apply date function on it.
      **Change Mo to Tu,Su etc depending on the day you want to consider as start of the week.


      Raj

  5. Fetching the 15th last working day date-yyyyMMdd (excluding only weekends) in Hive

    I have a table with date column (date in string format yyyyMMdd). My requirement is to design a logic to fetch data from the table where "date column value equals to the date of the 15th previous working day" (excluding only Saturdays and Sundays) without using a UDF or a shell script. For example it is 21st Feb 2020 today; the logic should produce an output: 20200203.

  6. Hi,
    How can I fetch the 15th previous working date (yyyyMMdd) – excluding all the Saturdays and Sundays.
    Lets say it is 20th feb 2020 today; I want 20200131 as output.
    I want this in HIVE.

    1. I am sorry, I missed the details over my requirement.

      I have a table with date column – date format is in yyyyMMdd (string). I want to filter data from this table everyday for the 15th previous working day. need to get the value from the date column in my table which satisfies this condition.

  7. Hi,
    How can I fetch the 15th previous working date (yyyyMMdd) – excluding all the Saturdays and Sundays.
    Lets say it is 20th feb 2020 today; I want 20200131 as output.

  8. how can i convert a date like '05-JAN-20' to 20200105 ('yyyymmdd')?

    i have been on this for days. This is not working

    CAST(date_Format('05-JAN-20', 'yyyymmdd') AS BIGINT)

    1. Hi Max

      Apologies for delay in reply.
      Please try below:
      select from_unixtime(unix_timestamp('05-JAN-20', 'dd-MMM-yy'),'yyyyMMdd');

      date_format will only work when you input date is in format 'yyyy-MM-dd'. If it is in any other format , it will result in NULL output or error. If input is in other format, you have to try from_unixtime & unix_timestamp.

      Hope this helps.

      Best,
      Raj

Leave a Reply

Your email address will not be published. Required fields are marked *