Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent function of datenum(datestring) of Matlab in Python

Tags:

python

matlab

In Matlab, when I run "datenum" function as the following;

datenum(1970, 1, 1);

I get the following output:

719529

I'm trying to find the equivalent function or script which is gonna give me the same output. But, unfortunately I couldn't find an enough explanation on the internet to do this.

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

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

Thanks,

like image 872
yusuf Avatar asked Oct 07 '15 12:10

yusuf


People also ask

How to convert date to datenum?

If the format of date string S is known, use the syntax N = datenum(S, F) . N = datenum(S, P) converts date string S , using pivot year P . If the format of date string S is known, use the syntax N = datenum(S, F, P) .

What is Datenum Matlab?

DateNumber = datenum( t ) converts the datetime or duration values in the input array t to serial date numbers. A serial date number represents the whole and fractional number of days from a fixed, preset date (January 0, 0000) in the proleptic ISO calendar.

How do I convert a timestamp to a number in Python?

Example 1: Integer timestamp of the current date and timeConvert the DateTime object into timestamp using DateTime. timestamp() method. We will get the timestamp in seconds. And then round off the timestamp and explicitly typecast the floating-point number into an integer to get the integer timestamp in seconds.


1 Answers

The previous answers return an integer. MATLAB's datenum does not necessarily return an integer. The following code retuns the same answer as MATLAB's datenum:

from datetime import datetime as dt

def datenum(d):
    return 366 + d.toordinal() + (d - dt.fromordinal(d.toordinal())).total_seconds()/(24*60*60)

d = dt.strptime('2019-2-1 12:24','%Y-%m-%d %H:%M')
dn = datenum(d)
like image 176
T.C. Helsloot Avatar answered Oct 28 '22 08:10

T.C. Helsloot