How do you convert a Python datetime
to a Matlab datetnum
?
To serialize datetime as a string, strftime can be used on the Python side:
import datetime
d = datetime.datetime.now()
print (d.strftime("%d-%b-%Y %H:%M:%S"))
According to MatLab docs datenum knows how to parse it.
Based on bavaza's answer - now including microseconds:
def datetime2matlabdn(dt):
mdn = dt + timedelta(days = 366)
frac_seconds = (dt-datetime.datetime(dt.year,dt.month,dt.day,0,0,0)).seconds / (24.0 * 60.0 * 60.0)
frac_microseconds = dt.microsecond / (24.0 * 60.0 * 60.0 * 1000000.0)
return mdn.toordinal() + frac_seconds + frac_microseconds
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With