Equivalent Matlab datestring function in Python

In Matlab, when I run " datenum " like the following:

datenum(1970, 1, 1); 

I get the following output:

 719529 

I am trying to find an equivalent function or script that will give me the same result. But, unfortunately, I could not find enough explanation on the Internet to do this.

I looked through this tutorial: https://docs.python.org/2/library/datetime.html , but that didn't help.

Could you tell me how can I get the same output in python?

Thanks,

+6
source share
2 answers

I would use the datetime module and the toordinal () function

 from datetime import date print date.toordinal(date(1970,1,1)) 719163 

To get the date you received, you will use

 print date.toordinal(date(1971,1,2)) 719529 

or to simplify the conversion

 print date.toordinal(date(1970,1,1))+366 719529 

I believe that the reason the date is disabled is due to the fact that the date-date starts counting down from January 0, 0000, which is not recognized as a valid date. You will have to counteract the change in the starting date by adding one to the year and day. The month does not matter, because the first month in datetime is 0 in money

+3
source

You can highlight date objects in Python:

 >>> date(2015, 10, 7) - date(1, 1, 1) datetime.timedelta(735877) >>> (date(2015, 10, 7) - date(1, 1, 1)).days 735877 

Just make sure to use an era that is useful for your needs.

+2
source

All Articles