We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

DateTime from Double

Hi. I could not find any source examples to calculate DateTime value from Double value.

I have a custom formula, that links to cell with DateTime. But when i tried to receive value of this cell, Aspose.Cells ReferredArea returns Double value. So i unable to find any constructor with Double. Can you help me with it?

DateTime getDateTimeValue(Object object) {
        if (object instanceof DateTime) {
            return (DateTime) object;
        } else if (object instanceof Double) {
            //TODO There is code to calculate datetime from double
            return new DateTime();
        } else if (object instanceof ReferredArea) {
            return getDateTimeValue(((ReferredArea) object).getValue(0, 0));
        } else {
            throw new IllegalArgumentException("Unsupported type " + object);
        }
    }

    @Test
    void test() {
        Workbook workbook = new Workbook();
        Cells cells = workbook.getWorksheets().get(0).getCells();

        cells.get("A1").putValue(new DateTime(1900, 1, 1));
        cells.get("A2").setFormula("=MYDATEFUNC(A1)");

        CalculationOptions options = new CalculationOptions();
        options.setCustomEngine(new AbstractCalculationEngine() {
            @Override
            public void calculate(CalculationData calculationData) {
                if ("MYDATEFUNC".equalsIgnoreCase(calculationData.getFunctionName())) {
                    Object paramValue = calculationData.getParamValue(0);
                    DateTime dateTime = getDateTimeValue(paramValue);
                    DateTime newValue = new DateTime(dateTime.getYear(), dateTime.getMonth(), dateTime.getDay());
                    newValue.addDays(1.0d);
                    calculationData.setCalculatedValue(newValue);
                }
            }
        });
        options.setIgnoreError(false);

        workbook.calculateFormula(options);

        assertEquals("02.01.1900", cells.get("A2").getDisplayStringValue());
    }

Best regards. Alexey

@makarovalv,

For your requirements, you may get the DateTime value by getting the corresponding cell via the API:
e.g
workbook.Worksheets[referredArea.SheetName].Cells[referredArea.StartRow, referredArea.StartColumn].DateTimeValue

Hope, this help a bit.