Rg786
Rg786

Reputation: 305

Convert DateTime format to Matlab Date Serial Number format

I want to be able to convert the DateTime (2012/12/31 10-21-17.617) to a decimal number that Matlab takes (Date Serial Number).

I have converted the Matlab date serial number to the Datetime format (yyy/mm/dd HH:mm:ss.fff) by using this:

DateTime conv = new DateTime(1, 1, 1).AddDays(734139.045000000040).AddYears(-1)

However i would like to be able to do the opposite of the above. I know the .NET date starts from 0001/01/01 where as for Matlab it is 0000/00/00.

Upvotes: 0

Views: 1436

Answers (1)

overclockwise
overclockwise

Reputation: 65

Unless I'm missing something, a simple approach would be TimeSpan.TotalDays (http://msdn.microsoft.com/en-us/library/system.timespan.totaldays.aspx)

Create a timespan from an arbitrary point to the DateTime you're trying to convert, then adjust as necessary with the offset to get it into matlab format. Added code example

    private DateTime MatlabToNET(double days)
    {
        return new DateTime(1, 1, 1).AddDays(days).AddYears(-1);
    }

    private double NETtoMatlab(DateTime dt)
    {
        TimeSpan ts = dt - new DateTime();
        return ts.TotalDays + 365;
    }

Upvotes: 0

Related Questions