What day is it?
R has a lot to offer in terms of dates and times. The two main classes of data for this are Date and POSIXct. Date is used for calendar date objects like "2015-01-22"
. POSIXct is a way to represent datetime objects like "2015-01-22 08:39:40 EST"
, meaning that it is 40 seconds after 8:39 AM Eastern Standard Time.
In practice, the best strategy is to use the simplest class that you need. Often, Date will be the simplest choice. This course will use the Date class almost exclusively, but it is important to be aware of POSIXct as well for storing intraday financial data.
In the exercise below, you will explore your first date and time objects by asking R to return the current date and the current time.
This exercise is part of the course
Intermediate R for Finance
Exercise instructions
- Type
Sys.Date()
to have R return the current date. - Type
Sys.time()
to have R return the current date and time. Notice the difference in capitalization of Date vs time. - Store
Sys.Date()
in the variabletoday
. - Use
class()
ontoday
to confirm its class.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# What is the current date?
___
# What is the current date and time?
___
# Create the variable today
today <- ___
# Confirm the class of today
___