home PYTHONJAVA
 

Ruby Date & Time(Date & Time)

The

Time class is used in Ruby to represent date and time. It is based on the system date and time provided by the operating system. This category may not represent dates before 1970 or after 2038.

This tutorial will familiarize you with all the important concepts of date and time.

Create current date and time

The following is a simple example of getting the current date and time:

Instance

#!/usr/bin/ruby -w # -*- coding: UTF-8 -*- time1 = Time.new puts "current time : " + time1.inspect # Time.now Same function time2 = Time.now puts "current time : " + time2.inspect

The above example runs the output as:

Current time : 2019-09< /span>-17 15 :23:14 +0800
Current time : 2019-09-17 15:23:14 +0800

Get the Date & Time component

We can use Time objects to get components for various dates and times. Please see the example below:

Instance

#!/usr/bin/ruby -w # -*- coding: UTF-8 -*- time= Time< Span class="hl-code">.new # Components of Time puts "Current time: " + time.inspect puts time.year # => Year of the date puts time.month # => Month of the date (1 to 12) puts time.day # => The first few days of the month (1 to 31) puts time.wday # => The day of the week (0 is Sunday) puts time.yday # => 365: Day of the year puts time.hour # => 23:24-hour system puts time.min # => 59 puts time.sec # => 59 puts time.usec # => 999999: microseconds puts time.zone # => "UTC": Time Zone Name

The above example runs the output as:

Current time : 2019-09< /span>-17 15 :24:44 +0800
2019
9
17
4
260
15
24
44
921519
CST

Time.utc, Time.gm and Time.local functions

These functions can be used to format dates in a standard format as follows:

# July 8, 2008 Time.local< Span class="hl-brackets">(2008, 7, 8) # July 8, 2008, 09:10am, local time Time.local< Span class="hl-brackets">(2008, 7, 8, 9, 10< Span class="hl-brackets">) # July 8, 2008, 09:10 UTC Time.utc< Span class="hl-brackets">(2008, 7, 8, 9, 10< Span class="hl-brackets">) # July 8, 2008, 09:10:11 GMT (same as UTC)< Span class="hl-comment"> Time.gm< Span class="hl-brackets">(2008, 7, 8, 9, 10< Span class="hl-code">, 11)

The following example gets all the components in the array:

[sec ,min,hour ,day,month,year ,wday,yday ,isdst,zone]

Try the following example:

Instance

#!/usr/bin/ruby -w time= Time< Span class="hl-code">.new values = time< Span class="hl-code">.to_a p values

The above example runs the output as:

[39, 25, 15, 17< /span>, 9,  2019, 4,  260, false, "CST"]

This array can be passed to the Time.utc or Time.local function to get different formats for the date, as follows:

Instance

#!/usr/bin/ruby -w time= Time< Span class="hl-code">.new values = time< Span class="hl-code">.to_a puts Time.utc(*values)

The above example runs the output as:

2019-09-17 15< /span>:26: 09 UTC

Here is the way to get time, the number of seconds since the epoch (platform related):

# Returns the number of seconds since the epoch time= Time< Span class="hl-code">.now.to_i # Convert seconds to Time objects Time.at< Span class="hl-brackets">(time) # Returns the number of seconds since the epoch, including subtle time= Time< Span class="hl-code">.now.to_f

Time zone and daylight saving time

You can use the Time object to get all the information about the time zone and daylight saving time, as follows:

time = Time.new # Here is the explanation time.zone< Span class="hl-code"> # => "UTC": return time zone time.utc_offset< Span class="hl-code"> # => 0: UTC is 0 relative to UTC Second offset time.zone< Span class="hl-code"> # => "PST" (or other time zone)< /span> time.isdst< Span class="hl-code"> # => false: if UTC does not have DST (daylight saving time) ) time.utc< Span class="hl-code">? # => true: if in UTC time zone time.localtime< Span class="hl-code"> # Convert to local time zone time.gmtime< Span class="hl-code"> # Convert back to UTC time.getlocal< Span class="hl-code"> # Return a new Time object in the region time.getutc< Span class="hl-code"> # return a new Time object in UTC

Format time and date

There are several ways to format the date and time. The following example demonstrates some of them:

Instance

#!/usr/bin/ruby -w time= Time< Span class="hl-code">.new puts time.to_s puts time.ctime puts time.localtime puts time.strftime("%Y-%m-%d %H:%M:%S"< /span>)

The above example runs the output as:

2019-09-17 15< /span>:26: 42 +0800
Thu Sep 17 15 :26:42 2019
2019-09-17 15:26:42 +0800
2019-09-17 15:26:42

Time Formatting Instructions

The instructions listed in the table below are used with the method Time.strftime.

commandDescription
%a Abbreviation for the name of the day of the week, (for example, Sun).
%A The full name of the day of the week, (for example, Sunday).
%b Abbreviation for the month name, (for example, Jan).
%BThe full name of the month name, (for example, January).
%c Preferred nearby date and time documentation.
%dThe initial couple of days of the month (01 to 31).
%H The initial couple of hours of the day, 24 hours (00 to 23).
%I The initial couple of hours of the day, 12 hours (01 to 12).
%j The initial couple of days of the year (001 to 366).
%m The initial couple of months of the year (01 to 12).
%MThe initial couple of minutes of great importance (00 to 59).
%p Meridian sign (AM or PM).
%S The initial couple of moments of the moment (00 or 60).
%UThe number of weeks in the present year, beginning the principal Sunday (as the main day of the primary week) (00 to 53).
%WThe number of weeks in the present year, beginning the principal Monday (as the primary day of the main week) (00 to 53).
%wDay of the week (Sunday is 0,0 to 6).
%xPriority documentation where just the date has no time.
%XPriority documentation with just time and no date.
%y demonstrates the year without the century (00 to 99).
%YThe year with the century.
%ZTime zone name.
%%% characters.

Time Algorithm

You can do some simple arithmetic with time, as shown below:

now = Time.now < Span class="hl-comment"># Current time puts now past = now< Span class="hl-code"> - 10 # 10 seconds ago. Time - number => Time puts past future= now< Span class="hl-code"> + 10 # 10 seconds from now. Time + number => Time puts future diff= future< Span class="hl-code"> - now # => 10 Time - Time => seconds seconds puts diff

The above example runs the output as:

2019-09-17 15< /span>:27: 08 +0800
2019-09-17 15:26:58 +0800
2019-09-17 15:27:18 +0800
10.0





welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.