Dates & Times / Formating Times & Dates
Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.
Learn More
Subtitles of the Movie
In this video we're going to look a little bit more at formatting and printing out date time values. So the method that really should be used for doing that, uh, we've actually looked at in other videos. Let me call up RI here first and it's the, the STRF time and I guess you can think of it being string format time and you give it, you call that with whatever your time object. So this isn't time class. This would be your object that you instantiated that's of the time class and you call this method and then in the string here, you give the parameters, uh, using these things down here; what you want to actually see printed out. And there's a whole bunch of them here and I'll show you a few of them just to get you an idea how it works. So launch into IRB here and let's get a time. So we'll say now equals time dot now. And let's load up our little seconds method so I can use that and do some math. So I'm just going to say X equals now minus seconds 350 days ago. So that would put us, uh, let me do a little bit less. There. That puts us into March somewhere and now I can just straight up tell from here it's Monday, March 19, but maybe I want to know what day of the year it is. So I could call this and since this is a string, this is actually getting evaluated, I could put other stuff in here and print it out, you know, nicely so it looks good and everything. If I just want to see what day of the year it was, I can do that. I can also, if we want to call that on now, I can call that and if I want to see, well, how many days in this year are left, 365, I got to do a little bit of, I can do that. So there's 187 days left in the year. And you notice why this error came up was because this is coming back as a string. So you can see that because it's got the double quotes around here where I needed to be able to do integer math for these because this is an integer here. So that's getting a little bit what the, you know, the day of the year is. How about a date, different date representation? Let's move back here to X. Say I want to get this the, the date as represented by the two-digit things with no time. It's like use X. That'll give me typically what you're going to see in those type of dates like that represented in, in a typical fashion here and, and some of these will take from whatever the local machine setting is. I'm not quite sure of that one. In other words, in, in some places more European this ordering is different. It's day, month, year. This is our American setting where it's month, day, year, so there might be some variety in how that is actually pulled and I think this comes from a C function so it's going to pull from whatever the local machine is set up. There's some other ones in here that you might be interested in. We have, that gets us the year. That give us, gives us the two-digit one. Here we can get the time zone that this local machine is on and here I can get the offset from the, uh, universal coordinated time and you can see it's minus six and that's because we're in daylight savings time right now and MDT is mountain daylight time. So that's a little bit of how you can use this. You can build strings out of these because that's what you're getting back here is a string and concatenate that with other things or you can actually put string, more string stuff inside of here. Maybe I should demonstrate that real quick here. So there you can see I just embedded that. So this is getting interpreted by this method as hey, replace that with a value and it's a typical C-type function. So that's a little bit about formatting dates and times in Ruby.
Tutorial Information
| Course: | Programming With Ruby |
| Author: | Al Anderson |
| SKU: | 33788 |
| ISBN: | 1-934743-01-1 |
| Release Date: | 2007-08-21 |
| Duration: | 8.5 hrs / 113 lessons |
| Work Files: |
Yes |
| Captions: | For Online University members only |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 