7
$\begingroup$

With Mathematica 9.0 for Mac OS X x86 (64-bit) (January 24, 2013) one could use

DateRange[{2000}, {2010}, "Year"]

to achive

{{2000}, {2001}, {2002}, {2003}, {2004}, {2005}, {2006}, {2007}, {2008}, {2009}, {2010}}

or

leapyears = Select[DateRange[{2000}, {2020}, "Year"], LeapYearQ[#] &]

with the result

{{2000}, {2004}, {2008}, {2012}, {2016}, {2020}}

With Mathematica 10.0 for Mac OS X x86 (64-bit) (June 29, 2014)

DateRange[{2000}, {2010}, "Year"]

one will get

{{2000, 1, 1}, {2001, 1, 1}, {2002, 1, 1}, {2003, 1, 1}, {2004, 1, 
  1}, {2005, 1, 1}, {2006, 1, 1}, {2007, 1, 1}, {2008, 1, 1}, {2009, 
  1, 1}, {2010, 1, 1}}

leapyears = Select[DateRange[{2000}, {2020}, "Year"], LeapYearQ[#] &]

{{2000, 1, 1}, {2004, 1, 1}, {2008, 1, 1}, {2012, 1, 1}, {2016, 1, 
  1}, {2020, 1, 1}}

The Function DateObject does not clear a bean to me

DateRange[DateObject[{2000}], DateObject[{2012}], "Year"]

enter image description here

So, how can i get rid of the additional month, day information on V10, i.e. using leapyears and DateObject returning Year only?

$\endgroup$

3 Answers 3

4
$\begingroup$

I would use:

DateRange[{2000}, {2010}, "Year"][[All, {1}]]
{{2000}, {2001}, {2002}, {2003}, {2004}, {2005}, {2006}, {2007}, {2008}, {2009}, {2010}}
% ~Select~ LeapYearQ
{{2000}, {2004}, {2008}}

Note the {1} in the Part parameters; see Head and everything except Head?

$\endgroup$
1
  • $\begingroup$ Thanks for the link & hint to use the marble in my head +1. $\endgroup$
    – user9660
    Aug 22, 2014 at 17:23
6
$\begingroup$

I'm afraid you now have to extract that information by yourself. This performs the same function as eldo's code, but instead of using Map twice I use Composition. It is also showcasing the new Mathematica 10 syntax.

Map[List@*First]@*Select[LeapYearQ]@DateRange[{2000}, {2020}, "Year"]

{{2000}, {2004}, {2008}, {2012}, {2016}, {2020}}

In order to extract the year from a DateObject you can use DateValue:

Map[List@*(DateValue[#, "Year"] &)]@Select[LeapYearQ]@DateRange[DateObject[{2000}], DateObject[{2012}], "Year"]

{{2000}, {2004}, {2008}, {2012}, {2016}, {2020}}

$\endgroup$
4
  • $\begingroup$ The first part of your answer is nothing but a V10 rephrasing of my earlier answer $\endgroup$
    – eldo
    Aug 21, 2014 at 20:54
  • 1
    $\begingroup$ @eldo No, it's faster. You iterate over the list twice because you use Map twice, but I use Composition so I only map over it once. You could do that in V9 as well. Anyway, I think the new V10 syntax should get some extra attention now in the beginning, and I was adding something else with my answer, it wasn't just consisting of that. $\endgroup$
    – C. E.
    Aug 21, 2014 at 20:55
  • $\begingroup$ Sorry, you're right, and the second part of your answer is +1 anyway. $\endgroup$
    – eldo
    Aug 21, 2014 at 20:57
  • $\begingroup$ @Pickett, Yep ... +1. $\endgroup$
    – user9660
    Aug 22, 2014 at 17:33
2
$\begingroup$

As to your first question:

List /@ First /@ Select[DateRange[{2000}, {2020}, "Year"], LeapYearQ]
{{2000}, {2004}, {2008}, {2012}, {2016}, {2020}}
$\endgroup$

Your Answer

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