Convert Millisecond to Date Time in Blazor Charts Component

17 Feb 20221 minute to read

Chart converts the datetime to milliseconds to calculate the bounds, so all events for datetime axis returns the value in milliseconds. For example, after zoom completion, the ranges for axis will be in the milliseconds. By using the OnZoomEnd event, you can convert millisecond value to date time format.

To convert millisecond value to date time format, follow the given steps:

Step 1:

Using OnZoomEnd event, you can get the axis range in milliseconds. By using the following code, you can get the equivalent date value.

<ChartEvents OnZoomEnd="RangeSelectionCompleted"></ChartEvents>

public void RangeSelectionCompleted(ZoomingEventArgs args)
{
    var zoomData = args?.AxisCollection?.FirstOrDefault();
    Console.WriteLine(new DateTime(1970, 1, 1).AddMilliseconds(zoomData.AxisRange.Min));
    Console.WriteLine(new DateTime(1970, 1, 1).AddMilliseconds(zoomData.AxisRange.Max));
}