Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
The Ruby Date class is used to represent dates in Ruby and it has no concept of time. So, for example, you could represent the day you were born using the date class, but not the specific time.
Links
Date Formatting Strings
Date (Year, Month, Day):
%Y - Year with century if provided, will pad result at least 4 digits.
-0001, 0000, 1995, 2009, 14292, etc.
%C - year / 100 (rounded down such as 20 in 2009)
%y - year % 100 (00..99)
%m - Month of the year, zero-padded (01..12)
%_m blank-padded ( 1..12)
%-m no-padded (1..12)
%B - The full month name (``January'')
%^B uppercased (``JANUARY'')
%b - The abbreviated month name (``Jan'')
%^b uppercased (``JAN'')
%h - Equivalent to %b
%d - Day of the month, zero-padded (01..31)
%-d no-padded (1..31)
%e - Day of the month, blank-padded ( 1..31)
%j - Day of the year (001..366)
Code Samples
Instantiate a new date object:
Date.new
Date.new(2015, 3, 1)
Parsing dates from strings:
Date.parse("March 1st, 2015")
Date.parse("It's 2015, everyone!")
We can also pass in our own format string to the strptime
method:
Date.strptime("It's 2015, everyone!", "It's %Y, everyone!")
Dates can be manipulated with mathematical operators:
Date.today
Date.today + 1
Date.today - 1
Date.today << 1 (subtract month)
Date.today >> 1 (add month)
Format strings can be passed as well:
Date.today.strftime("%Y %m, %d")
Date.today.strftime("%m %d, %Y")
There are a few different ways to
represent dates and times in Ruby.
0:00
The Ruby date class is used to
represent dates in Ruby, and
0:05
it has no concept of time.
0:09
So for example,
0:12
you could represent the day that
you were born using the date class.
0:14
But not a specific time of day.
0:17
Let's go ahead and take a look at how
the date class works now using WorkSpaces.
0:20
Okay, now we're going to take
a look at the date library
0:26
as part of the Standard Library.
0:29
Now you'll notice that there are three
different classes in use here,
0:31
Date, DateTime, and Time.
0:35
In this video we're just going to
be concentrating on the Date class.
0:37
Now we're gonna use the Date class,
unsurprisingly,
0:43
when we're working with dates.
0:46
And this is gonna be
without a time component,
0:48
just to represent a certain date that
you would like to in your program.
0:50
Now here are all of the different methods
that are supported by the Date class.
0:55
Now we're just going to take a look at
a few of these, but let's go ahead and
1:01
get right into a workspace.
1:06
So here I am in a workspace,
I'm gonna launch irb.
1:08
Now, the first thing that we need to do
since we're working with something from
1:14
the standard library is require it.
1:16
So we require date, and now we have
this date class that we can work with.
1:19
If we were to just instantiate a date and
called date.new, let's see what happens.
1:25
We get this new date,
and the year is -4712,
1:31
on January 1st.
1:38
Now, we can also pass arguments
to Date.new if we want to,
1:42
and that's done in
the year month day format.
1:46
So we could do date.new, 2015, and
then we'll say we want January 1st.
1:50
And now we get a new date object that
has been instantiated to 2015-01-01.
1:58
If we go back to the documentation and
2:05
check out new, we can see
the arguments that are supported.
2:09
By default the year
starts out at -4712 and
2:15
the month is January and
the day is the 1st of the month.
2:21
So we can play around with that and create
dates pretty much anytime we want to.
2:26
2014, 7, 4, and
that will create a date in the past.
2:31
Now, creating dates is one thing.
2:38
It's also useful to parse dates.
2:41
There's a method called parse that we
can use that will take a string and
2:44
return a date object.
2:49
And if we look at parse, it says that
parse is the given representation of
2:55
a date and time and
creates just a date object.
2:58
So if we go back to our workspace and
say Date.parse("July 4th,
3:01
2014"), we get back a date object that's
3:08
exactly what we would expect,
July 4th, 2014.
3:12
And you'll notice if we use Date.parse,
3:23
we put in month, day, year,
3:28
and it parsed it out as year, month, day.
3:31
So that's something to keep in
mind when you're using parse.
3:36
So here's another example.
3:40
If we wanted to do Date.parse("March 1st,
2015"), that works.
3:43
But if we wanted to pull out something
like just the year, and say,
3:49
it's 2015 everyone,
it will say that that's an invalid date.
3:54
Now, fortunately the Date class gives
us another method that we can use
4:00
to parse more complicated dates.
4:04
So for example,
using what we had just a minute ago,
4:08
if we wanted to parse out a different
string, we can use the strptime method.
4:12
So where Date.parse didn't give us what we
4:18
were expecting, with 07/04/2014, that
gave us something we weren't expecting.
4:23
We could use this strptime method.
4:29
We pass in the string
as the first argument.
4:34
And then the format string
is the second argument.
4:36
And we get back exactly
what we were expecting.
4:43
And we're not limited to
exactly this kind of format.
4:45
For example,
we could do that other one where we tried
4:49
to parse "It's 2015, everyone!".
4:54
Invalid date, but if we tell this string
that we're just looking for the year.
4:58
And then if we actually spell that
correctly and take out the exclamation
5:19
point, this functions exactly
as we would expect it to.
5:23
Let me clear my screen here and
show you a couple other methods.
5:27
If you want to get today's date,
we can call the method Date.today,
5:30
and as of the date of this recording,
that is correct.
5:34
What's nice about dates is that we can
do addition and subtraction on them.
5:37
So if I instantiated a new
date to be today's date,
5:43
I could add 1 to it and
it would give me tomorrow's date.
5:48
Could also do date- 1,
which would give me yesterday's date.
5:52
Sadly, this does not
work with multiplication.
5:56
And we get undefined method star for
the date.
5:59
Now instead of adding one day we
could also add or subtract months
6:03
using two greater than symbols and
the number one that will add a month
6:10
to the date and then two less than symbols
will subtract a month from the date.
6:15
I'm going to clear my
screen one more time and
6:19
one other thing that we
can do is format date.
6:22
So if we say date.to_s
gives it one format.
6:26
But, using the strftime method,
we can pass in another format string.
6:30
And it will print it out,
just a little bit differently.
6:40
The format string I'm using right now
is the year with four characters,
6:43
the month, a comma, and the day.
6:49
Now we can flip that around if we want.
6:51
And it'll tell us the day in pretty
much any order that we want to.
6:55
If you're curious about the different
possible format strings,
6:59
check out the notes below the video and
you can see where they're coming from.
7:02
Try setting and manipulating dates
on your own now using WorkSpaces.
7:06
You need to sign up for Treehouse in order to download course files.
Sign up