## 4.1 Creating dates and times

There are generally 3 types of date / time data :

• date: Often specified by year, month and day. Tibble prints this as <date>
• time：A time within a day, specified by hour, minutes and seconds. Tibble prints this as <time>
• date-time: is a date plus a time. Tibbles print this as <dttm>. Elsewhere in R these are called POSIXct

• 通过字符串创建
• 通过日期时间的各个成分创建
• 通过现有的日期时间对象创建

### 4.1.1 From strings

ymd() 和其他类似函数可以创建日期数据。想要创建日期时间型数据，可以在后面加一个下划线，以及h、m、s之中的一个或多个字母（依然要遵循顺序），这样就可以得到解析日期时间数据的函数了：

### 4.1.2 From individual components

To create a date/time from this sort of input, usemake_date() for dates, or make_datetime() for date-times. Input vectors are silently recycled:

This is useful when individual components of data / time is seprated across multiple columns, as in flights:

sec in make_datetime() is unassigned so it defualts to base level 0. This is also how make_date() workds

flightshourtime 均是航班起飞时间的预计值。为了算出实际起飞、到达时间，我们需要使用 dep_timearr_time 这两个变量，不过，它们同时包括了小时和分钟数：

### 4.1.3 From other times

You may want to switch between a date-time and a date. That’s the job of as_datetime() and as_date():

Sometimes you’ll get date/times as numeric offsets from the “Unix Epoch”, 1970-01-01. If the offset is in seconds, use as_datetime(); if it’s in days, use as_date().

### 4.1.4 Exercises

Exercise 1.1 What happens if you parse a string that contains invalid dates?

If returns NA and throws a warning:

Exercise 4.1 Use the appropriate lubridate function to parse each of the following dates: