Date time manipulation howtos

Erlang offers calendar module which provides computation of date, time and number of date time conversion functions. For more details, please refer to documentation for this module
Here are some examples:

Data types


Date = {Year, Month, Day}
Time = {Hour, Minute, Second}

where

Year = an integer and cannot be abbreviated. E.g.: 93 denotes year 93, not 1993
Month = 1..12
Day = 1..31
Hour = 0..23
Minute = 0..59
Second = 0..59

How to obtain local date time

{Date, Time} = calendar:local_time()


How to obtain current UTC time

{Date, Time} = calendar:universal_time()

How to convert time to seconds since midnight and via versa

Seconds = calendar:time_to_seconds(Time)
Time = calendar:seconds_to_time(Seconds)

How to verify if a year is leap year

Bool = calendar:is_leap_year(Year)

How to check if a date is valid

Bool = calendar:valid_date(Date)
Bool = calendar:valid_date(Year, Month, Day)

How to find out day of the week

DayNumber = calendar:day_of_the_week(Date)
DayNumber = calendar:day_of_the_week(Year, Month, Day)
1 = Monday, 2 = Tuesday, ….and 7 = Sunday

How to find out last day of a month

LastDay = calendar:last_day_of_the_month(Year, Month)

How to calculate date time difference

{Days, Time} = calendar:time_difference(DT1, DT2)
DT1 = {Date1, Time1}
DT2 = {Date2, Time2}
Logically equivalent to DT2 – DT1


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值