4
$\begingroup$

suppose I have lots of YYYYDDD date Integers which look like(for example):

dateslalala = 
Outer[1000 #1 + #2 &, {2003, 2005, 2004}, 
RandomInteger[{0, 365}, 20]] // Flatten

gives:

{2003067, 2003280, 2003362, 2003021, 2003223, 2003115, 2003049, 
2003233, 2003341, 2003074, 2003134, 2003182, 2003332, 2003061, 
2003184,...}

I want to transform them to the style YYYYMMDD like "20010101" or integer 20010101, Err... I didn't figure out have to do this by function like DateString quickly. In fact I use this ugly cumbersome code:

 StringJoin /@ ({ToString@#1, StringTake["00" <> ToString@#2, -2], 
 StringTake[
  "00" <> ToString@#3, -2]} & @@@ ((y = Floor[#/1000.]; 
   d = Mod[#, 1000]; DateList[{y, 1, d}][[1 ;; 3]]) & /@ 
 dateslalala))

gives:

{"20030308", "20031007", "20031228", "20030121", "20030811", 
"20030425", "20030218", "20030821", "20031207", "20030315", "20030514"...}

is there any more natural way to do this?

$\endgroup$
8
$\begingroup$

Perhaps, using:

dateslalala={2003364, 2003157, 2003314, 2003302, 2003181, 2003062, 2003254, \
2003070, 2003365, 2003338, 2003233, 2003073, 2003020, 2003010, \
2003238, 2003107, 2003310, 2003347, 2003204, 2003066, 2005364, \
2005157, 2005314, 2005302, 2005181, 2005062, 2005254, 2005070, \
2005365, 2005338, 2005233, 2005073, 2005020, 2005010, 2005238, \
2005107, 2005310, 2005347, 2005204, 2005066, 2004364, 2004157, \
2004314, 2004302, 2004181, 2004062, 2004254, 2004070, 2004365, \
2004338, 2004233, 2004073, 2004020, 2004010, 2004238, 2004107, \
2004310, 2004347, 2004204, 2004066};

then,

qr = QuotientRemainder[#, 1000] & /@ dateslalala;
DatePlus[{#1 - 1, 12, 31}, #2] & @@@ qr

yielding:

{{2003, 12, 30}, {2003, 6, 6}, {2003, 11, 10}, {2003, 10, 29}, {2003, 
  6, 30}, {2003, 3, 3}, {2003, 9, 11}, {2003, 3, 11}, {2003, 12, 
  31}, {2003, 12, 4}, {2003, 8, 21}, {2003, 3, 14}, {2003, 1, 
  20}, {2003, 1, 10}, {2003, 8, 26}, {2003, 4, 17}, {2003, 11, 
  6}, {2003, 12, 13}, {2003, 7, 23}, {2003, 3, 7}, {2005, 12, 
  30}, {2005, 6, 6}, {2005, 11, 10}, {2005, 10, 29}, {2005, 6, 
  30}, {2005, 3, 3}, {2005, 9, 11}, {2005, 3, 11}, {2005, 12, 
  31}, {2005, 12, 4}, {2005, 8, 21}, {2005, 3, 14}, {2005, 1, 
  20}, {2005, 1, 10}, {2005, 8, 26}, {2005, 4, 17}, {2005, 11, 
  6}, {2005, 12, 13}, {2005, 7, 23}, {2005, 3, 7}, {2004, 12, 
  29}, {2004, 6, 5}, {2004, 11, 9}, {2004, 10, 28}, {2004, 6, 
  29}, {2004, 3, 2}, {2004, 9, 10}, {2004, 3, 10}, {2004, 12, 
  30}, {2004, 12, 3}, {2004, 8, 20}, {2004, 3, 13}, {2004, 1, 
  20}, {2004, 1, 10}, {2004, 8, 25}, {2004, 4, 16}, {2004, 11, 
  5}, {2004, 12, 12}, {2004, 7, 22}, {2004, 3, 6}}
$\endgroup$
  • $\begingroup$ Thanks, was my reading of today. Seems to be simple, but it isn't, (already upvoted) :) $\endgroup$ – eldo Apr 26 '15 at 17:58
1
$\begingroup$

The thought is DatePlus.

split[x_Integer] := {{FromDigits@#[[1 ;; 4]], 1, 1}, 

    FromDigits@#[[5 ;; -1]]} &@IntegerDigits[x]

split[2003305]

(*
    {{2003,1,1},305}
*)

DatePlus[split[2003305][[1]], 305]

(*
    {2003,11,2}
*)

f = Block[{$DateStringFormat = {"Year", "Month", "Day"}, res}, 

    res = split[#]; DatePlus[res[[1]], res[[2]] - 1]] &;

f /@ dateslalala

(*
    {{2003,11,1},{2003,7,1},{2003,2,4},{2003,5,25},{2003,10,9},{2003,12,21},{2003,10,17},{2003
,10,30},{2003,9,18},{2003,1,14},{2003,3,12},{2003,10,14},{2003,11,4},{2003,1,14},{2003,12,
13},{2003,1,10},{2003,8,11},{2003,9,26},{2003,2,14},{2003,1,12},{2005,11,1},{2005,7,1},{20
05,2,4},{2005,5,25},{2005,10,9},{2005,12,21},{2005,10,17},{2005,10,30},{2005,9,18},{2005,1
,14},{2005,3,12},{2005,10,14},{2005,11,4},{2005,1,14},{2005,12,13},{2005,1,10},{2005,8,11}
,{2005,9,26},{2005,2,14},{2005,1,12},{2004,10,31},{2004,6,30},{2004,2,4},{2004,5,24},{2004
,10,8},{2004,12,20},{2004,10,16},{2004,10,29},{2004,9,17},{2004,1,14},{2004,3,11},{2004,10
,13},{2004,11,3},{2004,1,14},{2004,12,12},{2004,1,10},{2004,8,10},{2004,9,25},{2004,2,14},
{2004,1,12}}
*)
$\endgroup$
0
$\begingroup$

I am not sure I understand the constraints of this question if there are any. But would this work:

  Map[ Function[
  DateString[DatePlus[
    DateList[{ToExpression[StringTake[ToString[#1], 4]], 1, 
      1}], {(ToExpression[StringTake[ToString[#1], -3]] - 1), 
     "Day"}], {"Year", "Month", "Day"}]], dateslalala]
$\endgroup$

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.