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
本文介绍了Erlang中calendar模块的功能,包括获取本地及UTC时间、验证日期有效性、判断闰年、查找每月最后一天等实用操作。通过具体示例展示了如何使用这些函数进行日期时间的转换与计算。
586

被折叠的 条评论
为什么被折叠?



