Python中Matlab的datenum(datestring)的等效函数 [英] Equivalent function of datenum(datestring) of Matlab in Python

查看:1003
本文介绍了Python中Matlab的datenum(datestring)的等效函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Matlab中,当我运行" datenum 函数作为正在关注;

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

datenum(1970, 1, 1);

我得到以下输出:

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.

我看过本教程: https://docs.python.org/2 /library/datetime.html ,但没有帮助.

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

您能告诉我,如何在python中获得相同的输出?

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

谢谢

推荐答案

我将使用datetime模块和toordinal()函数

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

from datetime import date

print date.toordinal(date(1970,1,1))

719163

要获取日期,请使用

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

719529

或者为简化转换

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

719529

我认为日期不对的原因是由于datenum从1月0日至0000年开始计数,这不被认为是有效日期.您必须通过在年份和日期中添加一个来抵消开始日期中的更改.该月份无关紧要,因为datetime中的第一个月等于datenum中的0

I believe the reason the date is off is due to the fact datenum starts its counting from january 0, 0000 which this doesn't recognize 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 doesn't matter because the first month in datetime is equal to 0 in datenum

这篇关于Python中Matlab的datenum(datestring)的等效函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆