Seperate Date and Time strings into DateTime format


#1

Hi

I have a database with a seperate Date and Time fields.

I’m trying to work out how to search, return and combine the two,
turning them into one DateTime value so that I can then compare it to
the current date and time and see if it’s in the future or past.
However, I’m having no luck unfortunately.

Can anyone point me in the right direction?

I’ve searched for hours for this and having no real luck.

Thanks in advance


#2

Probably not the most elegant of solutions (and please let me know if
there are any better) but this is the best I have come up with!

dt = DateTime.parse("#{tapp.appdate}T#{tapp.apptime.strftime("%H:%M:
00")}")

On Dec 26, 8:00 pm, “Ruby on Rails: Talk” removed_email_address@domain.invalid


#3

Databases usually have a Date and Time set of functions and you can
use them to combine this two parts in one DateTime.

As suggested you can always use DateTime.strptime or Time.parse and
Time.to_datetime or any other way of string to DateTime
transformation.
You should ask your self do you need to make this transformation every
time you collect data from the table in question or you can make your
life a little easier with merging the two fields (Date and Time) in
one (DateTime).

There are a bunch of possibilities and the optimum one depends on your
overall usage of that data and plans for future usage.

On Dec 26, 9:08 pm, “Ruby on Rails: Talk” removed_email_address@domain.invalid


#4

Thanks for your response.

I need to keep the dates and times seperate really. It’s for an
appointment system and it’s easier if I keep things seperate at the
moment.