我有一個日期 - 時間,我創造這樣的:如何將日期添加到Ecto.DateTime?
Ecto.DateTime.from_erl({{2015, 3, 10}, {0, 0, 0}})
這是一個鳳凰的應用程序。我想在沒有任何額外的第三方庫的情況下添加幾天。怎麼樣?
我有一個日期 - 時間,我創造這樣的:如何將日期添加到Ecto.DateTime?
Ecto.DateTime.from_erl({{2015, 3, 10}, {0, 0, 0}})
這是一個鳳凰的應用程序。我想在沒有任何額外的第三方庫的情況下添加幾天。怎麼樣?
您可以使用erlang的:calendar
模塊來操作日期而無需其他依賴項。
添加日期的標準方法是使用:calendar.date_to_gregorian_days/1
做相加並將其轉換回:calendar.gregorian_days_to_date/1
的元組格式。
正確的靈藥語法
weekday= :calendar.gregorian_days_to_date(:calendar.date_to_gregorian_days({2011, 7, 14}) - 90)
IO.inspect weekday
{2011, 4, 15}
的日期時間 讓說NO_OF_DAYS是天ü要添加的數量。
{{a,b,c},{hh,mm,ss}} = :calendar.universal_time()
{x,y,z} = :calendar.gregorian_days_to_date(:calendar.date_to_gregorian_days({a,b,c}) +no_of_days)
time = Ecto.DateTime.from_erl({{x,y,z},{hh,mm,ss}})
是不是一個額外的依賴?實際上,我有DateTime。 –
不,它出現在erlang標準庫中。 – michalmuskala