Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,623 questions

55,358 answers

573 users

How to compute the day of the week for January 1st of any given year in Ruby

1 Answer

0 votes
require 'date'

=begin
    This program determines the day of the week for January 1st of a given year.

    Approach:
    ---------
    Ruby provides built‑in date/time handling through the Date class:
        - Date.new(year, month, day) : constructs a calendar date
        - date.wday                   : returns weekday number (0..6)
        - date.strftime("%A")         : returns full weekday name ("Thursday")

    wday meaning:
        0 = Sunday
        1 = Monday
        2 = Tuesday
        3 = Wednesday
        4 = Thursday
        5 = Friday
        6 = Saturday

    This avoids manual calendar arithmetic and uses efficient built‑in routines.
=end

# Convert a Date object's weekday to a readable string
def weekday_name(date_obj)
  date_obj.strftime("%A")   # "Monday", "Tuesday", ..., "Sunday"
end

# Compute weekday of January 1st for a given year
def jan1_weekday(year)
  date_value = Date.new(year, 1, 1)   # January 1st of given year
  weekday_name(date_value)
end

year = 2026

result = jan1_weekday(year)
puts "January 1st, #{year} falls on a #{result}."



=begin
run:

January 1st, 2026 falls on a Thursday.

=end

 



answered Jul 11 by avibootz

Related questions

...