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,
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) .
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.
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.
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)
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