Julia/Dates:从“天"转换时间跨度到“年"或“Float64" [英] Julia/Dates : convert a timespan from "days" to "year" or "Float64"

查看:62
本文介绍了Julia/Dates:从“天"转换时间跨度到“年"或“Float64"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将时间跨度从天"转换为年"或Float64"

I would like to convert a timespan from "days" to "year" or "Float64"

假设时间跨度 t:

t = Date("2000-02-04") - Date("1996-06-04")

但是以下每一行都会给我一个错误

However each following lines give me an error

t/365
Float64(t)
parse(Float64,t)
convert(Dates.Year, t)

推荐答案

你可以得到存储在 t 中的值并除以 365

You can get the value stored in t and divide it by 365

julia> Dates.value(t)/365
3.671232876712329

但是请注意,这假设每年是 365 天,但这是不正确的.对于某些情况,更优雅的解决方案是假设日期从某个时间点开始计算年数,请查看以下示例:

Note however that this assumes that each year is 365 days which is not true. For some scenarios a more elegant solution would be to count the years assuming that the date starts at some point, have a look at the example below:

julia> d0 = Date("2000-01-01")
2000-01-01

julia> d1 = d0 + t
2003-09-02

julia> year(d1)-year(d0), month(d1)-month(d0), day(d1) - day(d0)
(3, 8, 1)

这篇关于Julia/Dates:从“天"转换时间跨度到“年"或“Float64"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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