# Time - str\_to\_date

**URL:** <https://www.ruby-forum.com/t/time-str-to-date/185038>\
**Category:** Rails\
**Created:** [March 12, 2010, 10:00am UTC](https://www.ruby-forum.com/t/time-str-to-date/185038 "2010-03-12T10:00:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [March 12, 2010, 10:00am UTC](https://www.ruby-forum.com/t/time-str-to-date/185038/1 "2010-03-12T10:00:06Z")

</div>

i have a time field in my table. it is of varchar data type.

i want compare the time between given times.

my stored time format is 09:25:01 am.

is there any way to compare my time field ?

i don t have any idea about mysql STR\_TO\_DATE function…  
will it be usefull for my above problem…

please help me up to try it out…

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [March 12, 2010, 10:12am UTC](https://www.ruby-forum.com/t/time-str-to-date/185038/2 "2010-03-12T10:12:09Z")

</div>

On 12 March 2010 09:00, Newb N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> i have a time field in my table. it is of varchar data type.  
> i want compare the time between given times.  
> my stored time format is 09:25:01 am.  
> is there any way to compare my time field ?

You asked this two days ago…

> i don t have any idea about mysql STR\_TO\_DATE function…  
> will it be usefull for my above problem…

Probably - if your DB is MySQL. The function turns a string into a  
date and you have a string that you need turned into a date.

> please help me up to try it out…

Have you tried it? What happens? If you’ve got some way with it, and  
have a code example that doesn’t quite work, then maybe we can work  
from there.

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [March 12, 2010, 10:27am UTC](https://www.ruby-forum.com/t/time-str-to-date/185038/3 "2010-03-12T10:27:13Z")

</div>

Colin L. wrote:

> On 12 March 2010 09:00, Newb N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > i have a time field in my table. it is of varchar data type.
> > 
> > i want compare the time between given times.
> 
> As was previously suggested you should at least consider changing the  
> data type of the field in the db. I did not see any comment from you  
> on this suggestion in the previous thread.
> 
> Colin

now i m going to store the time 24 hour format…

so is there any funtion to convert the 12hour time to 24 hours

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [March 12, 2010, 10:19am UTC](https://www.ruby-forum.com/t/time-str-to-date/185038/4 "2010-03-12T10:19:46Z")

</div>

On 12 March 2010 09:00, Newb N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> i have a time field in my table. it is of varchar data type.
> 
> i want compare the time between given times.

As was previously suggested you should at least consider changing the  
data type of the field in the db. I did not see any comment from you  
on this suggestion in the previous thread.

Colin

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [March 12, 2010, 10:35am UTC](https://www.ruby-forum.com/t/time-str-to-date/185038/5 "2010-03-12T10:35:34Z")

</div>

On 12 March 2010 09:27, Newb N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> now i m going to store the time 24 hour format…
> 
> so is there any funtion to convert the 12hour time to 24 hours

You haven’t got a “Time” you’ve got a “STRING”… anything you do to  
it, you’re going to have to do some work - and if your choice is to  
change a 12hr clock string into a 24hr clock string, I don’t see that  
makes any difference to your problem, which was about how to search  
based on time…

Either:

a) Look at the SQL commands for the DB you’re using which manipulate  
strings and dates.  
b) Change how you store the data into something that can be compared  
easily and reliably (so either a DB time/datetime field, or an integer  
of “minutes from midnight”) - anything but a string.

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [March 12, 2010, 11:03am UTC](https://www.ruby-forum.com/t/time-str-to-date/185038/6 "2010-03-12T11:03:41Z")

</div>

On 12 March 2010 09:27, Newb N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Colin
> 
> now i m going to store the time 24 hour format…
> 
> so is there any funtion to convert the 12hour time to 24 hours

You might be able to use DateTime.strptime to convert to DateTime  
which you can then convert to whatever format you want. See

> **[Parsing time in custom format in Ruby](http://www.stephenchu.com/2007/11/parsing-time-in-custom-format-in-ruby.html)**
>
> I am sure you have used Time#strftime (or Date, DateTime) to format your date/time into your own custom format for pretty time display. This...

for an example.

Colin
