当前位置:  首页>> 技术小册>> Julia入门教程

Julia 通过 Dates 模块提供了以下三个函数来处理日期和时间:

Date:表示日期,精确到日,只显示日期。
DateTime:表示日期和时间,精确到毫秒。
DateTime:表示日时间,精确到纳秒,代表一天 24 小时中的特定时刻。

使用前,我们需要先导入 Dates 模块:

  1. import Dates

Date 和 DateTime 类型可以通过整数或 Period 类型解析。

Period 基于日期值,表示年、月、日等:

  1. Period
  2. Year
  3. Quarter
  4. Month
  5. Week
  6. Day
  7. Hour
  8. Minute
  9. Second
  10. Millisecond
  11. Microsecond
  12. Nanosecond

Date 和 DateTime 都是抽象类型 TimeType 的子类型。

输出日期的时间:
实例

  1. julia> import Dates
  2. julia> rightnow = Dates.Time(Dates.now()) # 时间
  3. 08:41:15.917
  4. julia> theday = Dates.Date(2022,5,6) # 日期
  5. 2022-05-06
  6. julia> today_date = Dates.today()
  7. 2022-05-11
  8. julia> Dates.now(Dates.UTC)
  9. 2022-05-11T00:44:20.136
  10. # 格式化时间
  11. julia> Dates.DateTime("20220429 120000", "yyyymmdd HHMMSS")
  12. 2022-04-29T12:00:00
  13. julia> Dates.DateTime("19/04/2022 17:42", "dd/mm/yyyy HH:MM")
  14. 2022-04-19T17:42:00

下表给出了日期格式代码,通过它们可以格式化我们的日期:

  1. 字符 日期/时间 元素
  2. Y 表示年,例如: yyyy => 1984, yy => 84
  3. m 表示年,例如: m => 7 or 07
  4. u 表示月份简写名称,例如: Jun
  5. U 表示月份完整名称,例如: January
  6. e 表示简写星期几,例如: Mon
  7. E 表示完整星期几,例如: Monday
  8. d 表示日,例如: 1 or 01
  9. H 表示小时,例如: HH => 00
  10. M 表示分钟,例如: MM => 00
  11. S 表示秒,例如: S => 00
  12. s 表示毫秒,例如: .000

实例

  1. julia> Dates.Date("Sun, 27 Sep 2022", "e, d u y")
  2. 2022-09-27
  3. julia> Dates.DateTime("Sun, 27 Sep 2022 10:25:10", "e, d u y H:M:S")
  4. 2022-09-27T10:25:10

通过上面的实例我们常见了一些日期时间对象,接下来我们就可以使用这些对象来获取数据(包含年、月、日、分、秒、时等):

实例

  1. julia> theday = Dates.Date(2022,5,6) # 创建日期对象
  2. 2022-05-06
  3. # 接下来获取 theday 中的数据
  4. julia> Dates.year(theday)
  5. 2022
  6. julia> Dates.month(theday)
  7. 5
  8. # 获取当前时间的数据
  9. julia> rightnow = Dates.now()
  10. 2022-05-11T08:51:45.342
  11. julia> Dates.minute(rightnow)
  12. 51
  13. julia> Dates.hour(rightnow)
  14. 8
  15. julia> Dates.second(rightnow)
  16. 45
  17. # 获取月份及星期几
  18. julia> Dates.dayofweek(theday)
  19. 5
  20. julia> Dates.dayname(theday)
  21. "Friday"
  22. julia> Dates.yearmonthday(theday)
  23. (2022, 5, 6)
  24. julia> Dates.dayofweekofmonth(theday)
  25. 1

日期运算

我们可以对日期对象进行算术运算。

比如我们计算两个日期相差几天:

实例

  1. julia> day1 = Dates.Date(2022,1,17)
  2. 2022-01-17
  3. julia> day2 = Dates.Date(2022,3,23)
  4. 2022-03-23
  5. julia> day2 - day1
  6. 65 days
  7. # 使用不同时间单位
  8. julia> Dates.canonicalize(Dates.CompoundPeriod(day2 - day1))
  9. 9 weeks, 2 days

我们也可以对日期相加,比如计算 2 年 6个月后的日期:

实例

  1. julia> rightnow = Dates.now()
  2. 2022-05-11T09:01:07.946
  3. julia> rightnow + Dates.Year(20) + Dates.Month(6)
  4. 2042-11-11T09:01:07.946
  5. # 6 天后的日期
  6. julia> rightnow + Dates.Day(6)
  7. 2022-05-17T09:01:07.946

日期范围

Julia 可以通过可迭代的 range(区间范围)对象来创建指定区间的日期。 在下面给出的示例中,我们将创建一个生成每个月的第一天的迭代器。

实例

  1. date_range = Dates.Date(2011,1,1):Dates.Month(1):Dates.Date(2022,1,1)
  2. Dates.Date("2011-01-01"):Dates.Month(1):Dates.Date("2022-01-01")

在上的区间范围对象中,我们可以找出其中哪些属于工作日,这里需要为 filter() 创建一个匿名函数,它将根据给定的日期判断是否为工作日:

实例

  1. julia> weekdaysfromrange = filter(dy -> Dates.dayname(dy) != "Saturday" && Dates.dayname(dy) != "Sunday" , date_range)
  2. 94-element Vector{Dates.Date}:
  3. 2011-02-01
  4. 2011-03-01
  5. 2011-04-01
  6. 2011-06-01
  7. 2011-07-01
  8. 2011-08-01
  9. 2011-09-01
  10. 2011-11-01
  11. 2011-12-01
  12. 2012-02-01
  13. 2021-02-01
  14. 2021-03-01
  15. 2021-04-01
  16. 2021-06-01
  17. 2021-07-01
  18. 2021-09-01
  19. 2021-10-01
  20. 2021-11-01
  21. 2021-12-01

四舍五入日期和时间

我们常用 round()、floor() 和 ceil() 函数来对参数进行向上或向下舍入,同样这些函数也可用于对日期进行四舍五入,以便及时向前或向后调整日期。

实例

  1. julia> Dates.now()
  2. 2022-05-11T09:17:49.824
  3. julia> Dates.format(round(Dates.DateTime(Dates.now()), Dates.Minute(15)), Dates.RFC1123Format)
  4. "Wed, 11 May 2022 09:15:00"
  5. ceil() 函数将向前调整日期/时间,如下所示:

实例

  1. julia> theday = Dates.Date(2022,5,6)
  2. 2022-05-06
  3. # 下个月
  4. julia> ceil(theday, Dates.Month)
  5. 2022-06-01
  6. #明年
  7. julia> ceil(theday, Dates.Year)
  8. 2023-01-01
  9. #下周
  10. julia> ceil(theday, Dates.Week)
  11. 2022-05-09

重复日期

我们可以查找一个区间范围内的重复日期,比如每个周日:

实例

  1. # 创建区间日期
  2. julia> date_range = Dates.Date(2011,1,1):Dates.Month(1):Dates.Date(2022,1,1)
  3. Dates.Date("2011-01-01"):Dates.Month(1):Dates.Date("2022-01-01")
  4. # 使用 filter() 函数过滤出每个周日
  5. julia> filter(d -> Dates.dayname(d) == "Sunday", date_range)
  6. 20-element Vector{Dates.Date}:
  7. 2011-05-01
  8. 2012-01-01
  9. 2012-04-01
  10. 2012-07-01
  11. 2013-09-01
  12. 2013-12-01
  13. 2014-06-01
  14. 2015-02-01
  15. 2015-03-01
  16. 2015-11-01
  17. 2016-05-01
  18. 2017-01-01
  19. 2017-10-01
  20. 2018-04-01
  21. 2018-07-01
  22. 2019-09-01
  23. 2019-12-01
  24. 2020-03-01
  25. 2020-11-01
  26. 2021-08-01

Unix 时间

UNIX 时间,或称 POSIX 时间是 UNIX 或类 UNIX 系统使用的时间表示方式:从UTC 1970 年 1 月 1 日 0 时 0 分 0 秒起至现在的总秒数,不考虑闰秒。

time() 函数返回 Unix 值:

实例

  1. julia> time()
  2. 1.652232489777e9
  3. unix2datetime() 函数将 Unix 时间转化为日期/时间对象:

实例

  1. julia> Dates.unix2datetime(time())
  2. 2022-05-11T01:28:23.493

当下时刻

DateTimes 以毫秒为单位,我们可以使用 Dates.value 函数获取以毫秒计的时间:

实例

  1. julia> moment=Dates.now()
  2. 2022-05-11T09:31:31.037
  3. julia> Dates.value(moment)
  4. 63787944691037
  5. julia> moment.instant
  6. Dates.UTInstant{Millisecond}(Millisecond(63787944691037))

时间和监控

Julia 为我们提供了 @elapsed 宏,它将返回表达式执行所需的时间(秒数)。

计算以下代码需要执行的时间:

实例

  1. julia> function foo(n)
  2. for i in 1:n
  3. x = sin(rand())
  4. end
  5. end
  6. foo (generic function with 1 method)
  7. julia> @elapsed foo(100000000)
  8. 1.360567967
  9. julia> @time foo(100000000)
  10. 1.363258 seconds

更多实例

实例

  1. julia> DateTime(2013)
  2. 2013-01-01T00:00:00
  3. julia> DateTime(2013,7)
  4. 2013-07-01T00:00:00
  5. julia> DateTime(2013,7,1)
  6. 2013-07-01T00:00:00
  7. julia> DateTime(2013,7,1,12)
  8. 2013-07-01T12:00:00
  9. julia> DateTime(2013,7,1,12,30)
  10. 2013-07-01T12:30:00
  11. julia> DateTime(2013,7,1,12,30,59)
  12. 2013-07-01T12:30:59
  13. julia> DateTime(2013,7,1,12,30,59,1)
  14. 2013-07-01T12:30:59.001
  15. julia> Date(2013)
  16. 2013-01-01
  17. julia> Date(2013,7)
  18. 2013-07-01
  19. julia> Date(2013,7,1)
  20. 2013-07-01
  21. julia> Date(Dates.Year(2013),Dates.Month(7),Dates.Day(1))
  22. 2013-07-01
  23. julia> Date(Dates.Month(7),Dates.Year(2013))
  24. 2013-07-01

该分类下的相关小册推荐:

暂无相关推荐.