5
$\begingroup$

I need to get the DateValues of the last 30 days (starting from yesterday, not today!) excluding saturdays and sundays. I want that the dates are german time (GMT + 1). The only solution I have found so far is to download with FinancialData the share prices of a german company for the last 30 days, since stock exchanges are closed during weekends.

For example:

yesterday = Take[DateList @ Today, 3] /. {a__, b_} :> {a, b - 1}

{2015, 3, 30}

FinancialData["F:VOW", {{2015, 2, 28}, yesterday}][[All, 1]]

(F:VOW is a german share) gives the wanted result:

{{2015, 3, 2}, {2015, 3, 3}, {2015, 3, 4}, {2015, 3, 5}, {2015, 3,
6}, {2015, 3, 9}, {2015, 3, 10}, {2015, 3, 11}, {2015, 3, 12}, {2015, 3, 13}, {2015, 3, 16}, {2015, 3, 17}, {2015, 3, 18}, {2015, 3, 19}, {2015, 3, 20}, {2015, 3, 23}, {2015, 3, 24}, {2015, 3, 25}, {2015, 3, 26}, {2015, 3, 27}, {2015, 3, 30}}

But - because of the download of share prices which I don't need - my "solution" takes seconds and seconds.

Please speed this up for me

$\endgroup$

2 Answers 2

14
$\begingroup$

This is how you can get the last 30 weekdays starting from yesterday:

days = DayRange[DayPlus[Yesterday, -30], Yesterday, "Weekday"]

To get {y,m,d} vectors, we might use

Take[#, 3] &@*DateList /@ days

Regarding the time zone, the only problem I can see would be that Yesterday will not produce the right result around midnight. To check this I used the Spelunking package, which told me that Yesterday is implemented as

Yesterday := DateObject[Take[DateList[], 3] - {0, 0, 1}]

DateList uses the time zone that is set on the computer you are using, so if your computer is set to the German time zone then you are alright. If not you can define a new function just like the one above but with DateList[TimeZone -> 1] instead.

$\endgroup$
5
  • $\begingroup$ great answer: It should be swedish or german time (gmt+1) only showing y,m,d $\endgroup$
    – eldo
    Commented Mar 31, 2015 at 20:26
  • $\begingroup$ I think Yesterday = DayPlus[DateList[], -1] for v9? $\endgroup$ Commented Mar 31, 2015 at 20:29
  • $\begingroup$ @bobthechemist That looks right; I've also added another alternative to the post. $\endgroup$
    – C. E.
    Commented Mar 31, 2015 at 21:13
  • $\begingroup$ @eldo I think I've resolved the time zone issue now, if I understand it correctly. $\endgroup$
    – C. E.
    Commented Mar 31, 2015 at 21:14
  • 1
    $\begingroup$ Yes,, you have,dear Pickett. Before I accept tomorrow I would like to understand the DayPlus-function, which is new to me. $\endgroup$
    – eldo
    Commented Mar 31, 2015 at 21:28
3
$\begingroup$
Select[DayPlus[DateList[TimeZone -> 2], #] & /@ 
Range[-30, -1], (! DayMatchQ[#, "Weekend"] &)]

I live in Germany TimeZone -> 2 is the correct Timezone for summertime and in winter i goes you have to take TimeZone-> 1

If you run the Kernel from Germany

 $TimeZone

should always have the correct value (Summer and Wintertime) so i suggest to leave this Option out.

$\endgroup$
2
  • $\begingroup$ Thank you too, sacratus,. You have - $TimeZone - solved my concern about different time zones:: My PC is in Berlin, so my time zone is Berlin. $\endgroup$
    – eldo
    Commented Mar 31, 2015 at 20:51
  • $\begingroup$ @eldo np, from the docs of $TimeTone: $TimeZone is set at the beginning of your Mathematica session to the time zone specified by your computer operating system. So you dont have to think about this ;-) $\endgroup$
    – sacratus
    Commented Mar 31, 2015 at 20:53

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.