4
$\begingroup$

DateObjects can be of varying physical dimensions.

CurrentDate[Today, #] & /@ {"Day", "Month", "Year"}

Mathematica graphics

How do you get the size of a DateObject? I tried AbsoluteOptions[Today] but it returns unevaluated. Ideas?

$\endgroup$
4
$\begingroup$

Here's a way to do it entirely in the FE (i.e. without a full Rasterize call, which is done in the FE anyway):

getBoxSize[c_Cell] :=
  {#[[1]], Total@#[[2 ;;]]} &@First@
    FrontEndExecute@GetBoundingBoxSizePacket[c];
getBoxSize[e_] := getBoxSize[Cell[BoxData@ToBoxes[e]]]

getBoxSize@CurrentDate[#] & /@ {"Day", "Month", "Year"}

{{148., 25.}, {121., 25.}, {90., 25.}}

Note that this is much faster than using a raw Rasterize:

getBoxSize@CurrentDate[#] & /@ {"Day", "Month", 
   "Year"} // RepeatedTiming

{0.04, {{151., 25.}, {121., 25.}, {90., 25.}}}

Rasterize[CurrentDate[#], "RasterSize"] & /@ {"Day", "Month", 
   "Year"} // RepeatedTiming

{0.24, {{151, 26}, {121, 26}, {90, 26}}}
$\endgroup$
7
$\begingroup$

Update

(After the clarification of the question, my original answer was not on target)

An alternative to using ImageDimensions is to use the "RasterSize" property with Rasterize:

dates = CurrentDate[Today,#]& /@ {"Day","Month","Year"};

Rasterize[#, "RasterSize"]& /@ dates

{{148, 26}, {121, 26}, {90, 26}}

Original answer

You can use DateValue to determine the granularity of a DateObject:

dates = CurrentDate[Today, #] & /@ {"Day", "Month", "Year"};

DateValue[#, "Granularity"]& /@ dates

{"Day", "Month", "Year"}

$\endgroup$
  • $\begingroup$ Thanks. But meant size as in dimensions. Not size as in granularity. Clarified in question. $\endgroup$ – Edmund Feb 27 '18 at 15:36
5
$\begingroup$

You can find the size in pixels by using ImageDimensions and Rasterize, like so:

Map[ImageDimensions@*Rasterize,
 CurrentDate[Today, #] & /@ {"Day", "Month", "Year"}]
(* {{148, 26}, {121, 26}, {90, 26}} *)
$\endgroup$
  • $\begingroup$ I am hoping there is a cleaner way than Rasterize. (+1) for now. $\endgroup$ – Edmund Feb 27 '18 at 15:39

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.