【问题标题】:Is there something like get DaysInMonth for in elixir?在长生不老药中是否有类似获得 DaysInMonth 的东西?
【发布时间】:2016-12-05 19:02:54
【问题描述】:

我在 Ecto.DateTime 上进行了搜索,但遗憾的是没有找到任何有用的信息。有没有办法得到一个月的最后一天?

我有一个查询,我从昨天(7 月 31 日)到今天(8 月 1 日)从 psql 加载订单,今天我只使用 1,但昨天它可以是 31、30、29或者 28. 有没有办法知道上个月有多少天?

提前致谢,感谢任何建议!

【问题讨论】:

  • 所以你的最终目标是找到昨天的日期?
  • 一般来说,检查 Erlang OTP 文档以了解此类问题通常是明智的。 Elixir 不会费心去复制这个库中已经内置了很多好东西。

标签: datetime elixir phoenix-framework ecto


【解决方案1】:

:calendar.last_day_of_the_month/2 接受年份和月份作为参数。

iex(1)> :calendar.last_day_of_the_month(2000, 2)
29
iex(2)> :calendar.last_day_of_the_month(2001, 2)
28

last_day_of_the_month(Year, Month) -> LastDay

类型:

Year = year()
Month = month()
LastDay = ldom()

计算一个月的天数。

【讨论】:

  • 哇!非常感谢!我现在就试试。
【解决方案2】:

您可以在 Elixir 的 Calendar.ISO 实现中使用 days_in_month/2 函数:

Calendar.ISO.days_in_month(year, month)

例如,Calendar.ISO.days_in_month(2019, 7) 将返回 31

您可以在此处的文档中找到更多信息:https://hexdocs.pm/elixir/Calendar.ISO.html#days_in_month/2

【讨论】:

    猜你喜欢
    • 2019-04-23
    • 1970-01-01
    • 2016-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-16
    • 1970-01-01
    相关资源
    最近更新 更多