Skip to content

Commit

Permalink
Ruby
Browse files Browse the repository at this point in the history
  • Loading branch information
msatmod committed Aug 30, 2022
1 parent dd23f34 commit 2866366
Show file tree
Hide file tree
Showing 3 changed files with 203 additions and 0 deletions.
64 changes: 64 additions & 0 deletions Day 20/DateTIme_class.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
=begin
Various ways to get Time and Date
1. Time class
2. Date class
3. DateTime class
# Date.new(yyyy,mm,dd)
# DateTime.new(yyyy, mm, dd, hh, mm, ss,'TimeZone')
=end


require 'date'

d = Date.new(2022, 07, 20)

puts d


d = Date.new

puts d


d = Date.parse('2000-07-20')

puts d


d = Time.new(2000, 07, 20).to_date

puts d


d = Time.new(2000, 07, 20).to_date

puts d.year


d = Time.new(2000, 07, 20).to_date

puts d.month


d = Date.new(2000, 07, 20).to_date

d = d + 1

puts d


d = Date.parse('20th Juky 2000').to_date

d = d + 1

puts d


dt = DateTime.new(2000, 07, 20, 3, 30, 45, '+0530')

puts dt
94 changes: 94 additions & 0 deletions Day 20/Date_&_Time.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Various ways to initialize the time object

# Time.new(YYYY, mm, dd, hh, mm, ss, 'Time Zone')

time = Time.new(2022, 7, 20, 21, 30, 45, '+05:30')

puts time


time = Time.new(2022, 7, 20, 21, 30, 45)

puts time


time = Time.new(2022, 7, 20)

puts time


time = Time.new(2022)

puts time


time = Time.new

puts time


puts time.year

puts time.month

puts time.day

puts time.hour

puts time.min

puts time.sec

puts time.wday

# 0 - sunday
# 1 - monday
# 2 - tueday
# 3 - wednesday
# 4 - hursday
# 5 - friday
# 6 - saturday

puts time.yday

puts time.usec


puts time.thursday?

puts time.sunday?


puts time.dst? # Day light saving time


puts time.subsec


puts time.to_a


puts time.to_i


puts time.to_f


time2 = time + 259200

puts time2


puts time<=>time2

puts time2<=>time

puts time2<=>time2

puts time.eql?(time2)

puts time == time2

puts time < time2

puts time > time2
45 changes: 45 additions & 0 deletions Day 20/Formatting_Time_&_Date.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Formatting Time and Date

%a - The abbreviated weekday name (Sun)

%A - The full weekday name (Sunday)

%b - The abbreviated month name (Jan)

%B - The full month name (January)

%c - The preferred local date and time representation

%d - Day of the month (01 to 31)

%H - Hour of the day, 24-hour clock (00 to 23)

%I - Hour of the day, 12-hour clock (01 to 12)

%j - Day of the year (001 to 366)

%m - Month of the year (01 to 12)

%M - Minute of the hour (00 to 59)

%p - Meridian indicator (AM or PM)

%S - Second of the minute (00 to 60)

%U - Week number of the current year, starting with the first Sunday as the first day of the first week (00 to 53)

%W - Week number of the current year, starting with the first Monday as the first day of the first week (00 to 53)

%w - Day of the week (Sunday is 0, 0 to 6)

%x - Preferred representation for the date alone, no time

%X - Preferred representation for the time alone, no date

%y - Year without a century (00 to 99)

%Y - Year with century

%Z - Time zone name

%% - Literal % character

0 comments on commit 2866366

Please sign in to comment.