首页 > 解决方案 > 我想格式化日期,我们现在只有 yyyyq ,我想得到 YYYY-MM-DD

问题描述

这是我的代码:

Dates.Year(div(19973,10)) # 1997 years
Dates.Month(round(mod(19973,10)*3)) # 9 months

以上是好的,但是......

Dates.lastdayofquarter(Date(Dates.Year(div(19973,10)),Dates.Month(round(mod(19973,10)*3)),1))
# MethodError: no method matching Int64(::Year)
Closest candidates are:
Int64(!Matched::Union{Bool, Int32, Int64, UInt32, UInt64, UInt8, Int128, #Int16, Int8, UInt128, UInt16}) at boot.jl:708,
Int64(!Matched::Ptr) at boot.jl:718,
Int64(!Matched::Float32) at float.jl:706,
...
Date(::Year, ::Month, ::Int64) at types.jl:368,
top-level scope at untitled-b0de772dbeef3476c50547132427f175:73
include_string(::Function, ::Module, ::String, ::String) at loading.jl:1088

标签: juliajulian-date

解决方案


这里是:

julia> lastdayofquarter(Date(19973 ÷ 10, (19973 % 10)*3))
1997-09-30

请注意,÷是整数除法,%是模运算符,并且Date可以通过传递年份和月份来构造对象。

或受@phipsgabler 评论启发的另一条单线:

julia> lastdayofquarter(Date(divrem(19973, 10).*(1,3)...))
1997-09-30

推荐阅读