format a date in to the format october 13, 2014 11:13:00.000 am in ruby

main.rb
date = DateTime.new(2014, 10, 13, 11, 13, 0, '-5')
formatted_date = date.strftime("%B %d, %Y %I:%M:%S.%L %p")
110 chars
3 lines

In this example, we create a new DateTime object with the DateTime.new method and pass in the year, month, day, hour, minute, and second. We also pass in a time zone offset of '-5' to account for any time zone differences.

We then use the strftime method on the date object to format it into the desired string format. %B represents the full month name, %d represents the day of the month, %Y represents the year, %I represents the hour in 12-hour format, %M represents the minute, %S represents the second, %L represents the milliseconds, and %p represents the AM/PM designation.

gistlibby LogSnag