Ruby Convert Date to DateTime

1. Introduction

In Ruby, handling dates and times often requires converting between different object types. While a Date object represents a specific day, a DateTime object includes both date and time, providing a more precise point in time. Converting Date to DateTime is a common task, especially when time-specific data is required. This blog post will demonstrate how to convert a Date object into a DateTime object in Ruby.

A Date object in Ruby holds date information, such as year, month, and day, without time. A DateTime object includes date and time information, providing a more complete representation of a moment. Converting from Date to DateTime involves specifying the time to associate with the date.

2. Program Steps

1. Ensure the Ruby Date library is available.

2. Create a Date object to be converted.

3. Specify the time to create a DateTime object from the Date.

4. Use the Ruby Date library to perform the conversion.

3. Code Program

# Step 1: Ensure the Date library is available
require 'date'

# Step 2: Create a Date object
date =, 4, 5)

# Step 3: Specify the time
time =, 4, 5, 12, 0, 0)

# Step 4: Convert the Date to a DateTime object
datetime =, date.month,, time.hour, time.min, time.sec,

# Output the DateTime object
puts datetime




1. require 'date' includes the Date library which contains the Date and DateTime classes.

2. date is a Date object representing the 5th of April, 2023.

3. time is a Time object for midday on the same date, used to extract time components.

4. creates a new DateTime object using the year, month, and day from the date object, and the hour, minute, and second from the time object. The provides the timezone offset.

5. puts datetime outputs the DateTime object to the console, confirming the conversion.