16
$\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$

11 Answers 11

25
$\begingroup$

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

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

enter image description here

$\endgroup$
9
$\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
    Commented Feb 14, 2022 at 3:41
9
$\begingroup$

Yet another one-liner: FoldPairList + QuotientRemainder:

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

IntegerDigits + MixedRadix:

IntegerDigits[19001231, MixedRadix[{100, 100}]]
{1900, 12, 31}
$\endgroup$
7
$\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$ Commented Feb 14, 2022 at 6:57
  • 3
    $\begingroup$ f[#]&/@ ... could be written as f/@... $\endgroup$ Commented Feb 14, 2022 at 17:31
6
$\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$
5
$\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
    Commented Feb 14, 2022 at 3:41
  • $\begingroup$ Also very nice, indeed! $\endgroup$
    – user49048
    Commented Feb 14, 2022 at 3:45
  • $\begingroup$ @user1066 Beautiful! $\endgroup$ Commented Feb 14, 2022 at 3:51
4
$\begingroup$

An option using the Date functionality:

  DateValue[#, {"Year", "Month", "Day"}] &@DateObject@ToString@19001231
$\endgroup$
4
$\begingroup$
ToExpression @* StringJoin /@
 TakeList[Characters @ ToString[19001231], {4, 2, 2}]

{1900, 12, 31}

$\endgroup$
4
$\begingroup$

Using DateString and StringSplit:

dstr = DateString[ToString@19001231, "ISODate"]

"1900-12-31"

ToExpression@StringSplit[dstr, Except[DigitCharacter]]

{1900, 12, 31}

The shortest form (Thanks to @eldo!):

StringSplit[DateString[19001231, "ISODate"], "-"]
$\endgroup$
2
  • 1
    $\begingroup$ Very nice! Could be shortened to StringSplit[DateString[19001231, "ISODate"], "-"] $\endgroup$
    – eldo
    Commented Mar 18 at 17:47
  • $\begingroup$ Thanks, @eldo! Let me add the shortest path you suggest ;) $\endgroup$ Commented Mar 18 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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