The class Time and Date in Ruby deals with the dates and times. It helps us to get the current time and date according to our system configurations. It also gives us the components of a date and time and also to format time and date.
The date is the day of a month or year that is represented by a number. Date consists of a month, year, date. The date objects are created with ::new, ::parse, ::today, ::jd, ::strptime, ::ordinal, ::commercial etc. All the date objects are unchangeable.
Getting current date and time.
Current Time : 2019-09-05 04:09:21 +0000
In above example inspect function is used to get current date and time.
Getting Components of Date and Time.
The Time object can be used to get various components of Date and Time.
Current Time :2019-09-05 04:08:27 +0000 2019 9 5 4 248 4 8 27 864801 UTC
There are following terms used in date:
- Calendar date: The calendar date is specific day of a calendar.
- Ordinal date: The ordinal date is a specific day of a calendar year.
- Week date: The week date is a date which represent week in the calendar.
- Julian day number: The Julian day number is in passed day since noon(Greenwich Mean Time) on January 1,4713 BCE(in the Julian calendar ).
- Modified Julian day number: The Julian day number is in passed day since midnight(Coordinated universal Time) on November 17,1858 CE(in the Gregorian calendar).
Below is the example to get date.
1999-07-17 2019-01-29 2019-04-06 2018-08-07 2018-01-15 2018-04-05
Time and date Directives:
The directives used in the strftime method are:
- %a: The abbreviated weekday name (for example: Sun ).
- %A: The full name of the weekday (for example: Sunday).
- %b: The abbreviated name of the month(for example: Jan)
- %B: The full name of the month(for example: January )
- %c: The selected local date and time representation
- %d: Day of the month (1 to 31).
- %H: 24-hour clock
- %I: 12-hour clock
- %j: day of the year
- %m: month of the year
- %M: minute
- %p: meridian indicator
- %S: seconds
- %%: % literal
- %z: time zone name
- %Y: year name with the century
- %y: year name without the century
- %X: selected representation of time only
- %x: selected representation of date only
- %w: weekday
- %U: week number of current year, starting with first Sunday
- %W: week number of current year, starting with first Monday
- How to get the current date and time in seconds?
- Ruby | Date ajd() method
- Ruby | Date === function
- Ruby | Date <=>() method
- Ruby | Date day() function
- Ruby | Date << function
- Ruby | Date >> method
- Ruby | Date + method
- Ruby | Date - function
- Convert timestamp to readable date/time in PHP
- Ruby | Date gregorian() function
- Ruby | Date cwyear() function
- Ruby | Date cweek() function
- Ruby | Date asctime() function
- Ruby | Date day_fraction function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.