Convert Integer to Date

Hi everyone,

 

I want to convert a number "19990101" to date format. What is the best way to do this?

 

PS. I've already done this code to get the date, but I want to know what is the best way.

local!year: left(19990101, 4),
local!month: mid(19990101, 5, 2),
local!day: right(19990101, 2),
date(local!year, local!month, local!day)

 

Thanks in advance

  Discussion posts and replies are publicly visible

Parents
  • I would say that the best is to first convert it to a string of text, extract the data as you are suggesting with your sample code.

    Working with a date as a integer might be a very bad idea, as you are mixing units, so from the scientifically point of view it makes no sense. The number should have an unit (year, month, day..).

    Anyways, if you really want to work with numbers, you should do math and not work with the numbers as strings. I would use divisions on based 10 and then convert it to integer

    tointeger(19990101/10000) = 1999 so you have the years
    now, if you do 1999 · 10000 = 19990000
    if you do that minus your original, you will have months and days
    19990000 - 19990101 = 0101 (which is your month an day)
    Following in a similar way tointegrer(0101/100) = 01 Month


Reply
  • I would say that the best is to first convert it to a string of text, extract the data as you are suggesting with your sample code.

    Working with a date as a integer might be a very bad idea, as you are mixing units, so from the scientifically point of view it makes no sense. The number should have an unit (year, month, day..).

    Anyways, if you really want to work with numbers, you should do math and not work with the numbers as strings. I would use divisions on based 10 and then convert it to integer

    tointeger(19990101/10000) = 1999 so you have the years
    now, if you do 1999 · 10000 = 19990000
    if you do that minus your original, you will have months and days
    19990000 - 19990101 = 0101 (which is your month an day)
    Following in a similar way tointegrer(0101/100) = 01 Month


Children
No Data