12
$\begingroup$

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.

$\endgroup$

8 Answers 8

21
$\begingroup$

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

NumberDecompose[19001231, 10^{4, 2, 0}]
{1900, 12, 31}
$\endgroup$
2
  • 2
    $\begingroup$ (+1). I did not even know about NumberDecompose $\endgroup$
    – kcr
    Feb 14 at 4:01
  • 1
    $\begingroup$ This looks the most elegant way! $\endgroup$ Feb 14 at 4:04
10
$\begingroup$
DateList[{IntegerString @ #, {"Year", "", "Month", "", "Day"}}][[;; 3]] & @ 19001231
{1900, 12, 31}
DateObject[{IntegerString@#, {"Year", "", "Month", "", "Day"}}] & @ 19001231

enter image description here

$\endgroup$
8
$\begingroup$

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]]}

Mathematica graphics

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

$\endgroup$
1
  • 2
    $\begingroup$ +1 Note that the last line can be written more succinctly as FromDigits /@ {m[[;; 4]], m[[5 ;; 6]], m[[7 ;;]]} $\endgroup$
    – Bob Hanlon
    Feb 14 at 3:41
8
$\begingroup$

Yet another one-liner: FoldPairList + QuotientRemainder:

FoldPairList[QuotientRemainder, 19001231, 10^{4, 2, 0}]
{1900, 12, 31}
$\endgroup$
6
$\begingroup$

IntegerDigits + MixedRadix:

IntegerDigits[19001231, MixedRadix[{100, 100}]]
{1900, 12, 31}
$\endgroup$
6
$\begingroup$

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.

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

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} *)
$\endgroup$
4
$\begingroup$

Similar to the response by @Nasser

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

which yields

{1900, 12, 31}

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.