![]() ![]() To use the Date class you need to require 'date'. This class stores everything internally in terms of days. The Date class has no concept of minutes, seconds or hours. If you are using Rails then you can do this: Now with this number we can sustract that from the current date: If you want to get something like yesterday’s day then you will need to calculate how many seconds there are in a day. Then you can check if that time has passed yet. In this example you get a time object that is set 10 seconds from the current time. Remember that the internal representation for Time is in seconds, so you can do this: Sometimes you don’t want the current time, but a time in the future or the past. This timestamp is a number of seconds in UTC (Coordinated Universal Time). You can generate a time-zone independent timestamp to date specific events. You can find more info on the different formats available on the following links: You can get the time without the date, or a nicely formatted date with the year, day & name of the current month. Time.strftime("Unix time is %s") # "Unix time is 1449336630"Īs you can see, this method is very flexible. Time.strftime("Today is %A") # "Today is Sunday" If you have ever used the printf method the idea is very similar to that. ![]() It works by passing a string with format specifiers, these specifiers will be replaced by a value. This method is strftime, which basically means ‘format time’. You can use a method to get almost any format you need. Ruby formats Time objects in a specific way by default. You can also get the current time in UTC: The output for this method is in seconds, but you can divide by 3600 to get it in hours. If you want the time zone offset you can use the utc_offset method. This will give you the time zone abbreviation. You can check the current time zone for a Time object using the zone method. These are predicate methods, they will return either true or false.Ī Time object has a time zone associated with it. In addition, you can also ask if this date corresponds to a certain day of the week. You can ask what day, month or hour a time object is representing: You can ask a time object for any of its components.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |