# How to split a number

I have a number representing date (yyyymmdd):

19001231


I want to convert this number to

 {1900,12,31}


How to do this? There should be easy answer.

You can also use NumberDecompose with the basis {10000, 100, 1}:

NumberDecompose[19001231, 10^{4, 2, 0}]

{1900, 12, 31}

• (+1). I did not even know about NumberDecompose
– kcr
Feb 14 at 4:01
• This looks the most elegant way! Feb 14 at 4:04
DateList[{IntegerString @ #, {"Year", "", "Month", "", "Day"}}][[;; 3]] & @ 19001231

{1900, 12, 31}

DateObject[{IntegerString@#, {"Year", "", "Month", "", "Day"}}] & @ 19001231


Since the year is fixed 4 digits, and day and month are fixed 2 digits, one way is to do

n=19001231;
m=IntegerDigits[n];
FromDigits[#]&/@{m[[1;;4]],m[[5;;6]],m[[7;;8]]}


There might be a build in function that does this already related to date/time formatting. I have not looked.

• +1 Note that the last line can be written more succinctly as FromDigits /@ {m[[;; 4]], m[[5 ;; 6]], m[[7 ;;]]} Feb 14 at 3:41

Yet another one-liner: FoldPairList + QuotientRemainder:

FoldPairList[QuotientRemainder, 19001231, 10^{4, 2, 0}]

{1900, 12, 31}

IntegerDigits[19001231, MixedRadix[{100, 100}]]

{1900, 12, 31}


Interpret the string as a "Date".

Interpreter["Date"]["19001231"][#] & /@ {"Year", "Month", "Day"}


{1900, 12, 31}

EDIT

I should have said: convert the number to string; interpret the string as a date and then extract parts (to answer the question as posed and also to avoid repeating the caculations).

str = IntegerString@19001112
dobj = Interpreter["Date"][str]
{y, m, d} = dobj[#] & /@ {"Year", "Month", "Day"}


Interpreter is slow, but it is a built-in function designed for the purpose.

• Thank you for letting me know such a nice built-in function. Feb 14 at 6:57
• f[#]&/@ ... could be written as f/@... Feb 14 at 17:31

Processing dates by integers is extremely error-prone. Here is an example from the excellent book by Paul Wellin (p.598).

Compare this

DateList[19001231];
Take[%,3]

(* Out: {1900, 8, 8} *)


with this

DateList[ToString[19001231]]
Take[%, 3]

(* Out: {1900, 12, 31} *)


Similar to the response by @Nasser

MapAt[FromDigits,
TakeList[IntegerDigits[19001231], {4, 2, 2}], {{1}, {2}, {3}}]


which yields

{1900, 12, 31}

• (+1) Alternatively FromDigits/@TakeList[IntegerDigits[19001231],{4,2,2}] (which I was going to post!) Feb 14 at 3:41
• Also very nice, indeed!
– kcr
Feb 14 at 3:45
• @user1066 Beautiful! Feb 14 at 3:51