Select Range of SQLDateTimes
table = {{ID1, SQLDateTime[{1978, 1, 10, 0, 0, 0.`}]},
{ID2, SQLDateTime[{1999, 1, 10, 0, 0, 0.`}]},
{ID3, SQLDateTime[{2010, 9, 10, 0, 0, 0.`}]},
{ID4, SQLDateTime[{2011, 1开发者_Go百科, 10, 0, 0, 0.`}]}}
I'd like to return all cases in table
in which the SQLDateTime is within the last year (DatePlus[{-1, "Year"}]
). How do I specify a search for those cases?
You could also use DateDifference
:
Cases[table, {a_, SQLDateTime[b_]} /;
DateDifference[b, DateList[], "Year"][[1]] <= 1]
Select[table, (AbsoluteTime[ DatePlus[{-1, "Year"}]] <=
AbsoluteTime[ #[[2, 1]]] <= AbsoluteTime[ ] &)]
(* ==> {{ID3, SQLDateTime[{2010, 9, 10, 0, 0, 0.}]},
{ID4,SQLDateTime[{2011, 1, 10, 0, 0, 0.}]}
}
*)
Small update (pre-caching of Date[], based on Leonid's comments):
With[
{date = Date[]},
Select[table,
(AbsoluteTime[ DatePlus[date, {-1, "Year"}]] <=
AbsoluteTime[ #[[2, 1]]] <= AbsoluteTime[date ] &)]
]
This also removes a problem with the original DatePlus[{-1, "Year"}]
which only takes today's date into account and not the current time.
精彩评论