Convert DateTime to Double

2019-04-25 11:08发布

I've created a DateTime value from an item being clicked in a listBox. It's in the format dd/MM/yyyy hh:mm:ss. I'm want to zoom in on a ten minute period with the clicked event in the middle. My current code is as follows (where zoom_time is the DateTime to zoom to on my chart;

chart1.ChartAreas[0].AxisX.Minimum = (Convert.ToDouble(zoom_time.AddMinutes(-5)));
chart1.ChartAreas[0].AxisX.Maximum = (Convert.ToDouble(zoom_time.AddMinutes(5)));

This breaks saying

"invalid cast from DateTime to double"

Any ideas guys?

3条回答
Viruses.
2楼-- · 2019-04-25 11:45

You have to use the ToOADate() methode like the following:

chart1.ChartAreas[0].AxisX.Minimum = zoom_time.AddMinutes(-5).ToOADate();
chart1.ChartAreas[0].AxisX.Maximum = zoom_time.AddMinutes(5).ToOADate();

Edit:

Should have refreshed my page before answering. :)

查看更多
对你真心纯属浪费
3楼-- · 2019-04-25 11:52

You can use DateTime.ToOADate(), if you mean ole automation date by double

查看更多
走好不送
4楼-- · 2019-04-25 11:58

Thanks for that!

For reference, the following works best;

            double start = (zoom_time.AddMinutes(-1)).ToOADate();
            double end = (zoom_time.AddMinutes(1)).ToOADate();

            chart1.ChartAreas[0].AxisX.Minimum = start;
            chart1.ChartAreas[0].AxisX.Maximum = end;
查看更多
登录 后发表回答