Forum: Ruby on Rails TimeStamp conversion

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Steven D. (Guest)
on 2006-04-24 12:43
Hi,

I've got a TimeStamp field in MySql and want to use the hour and minutes
in the value.

for example:

StartTime = Schedule.find(action2.ScheduleID).DateTimeStart.to_s

That returns "Mon Feb 20 08:00:00 Romance Standard Time 2006"

How do I get 08:00 returned ??

Thanks !

Steven.
Alex W. (Guest)
on 2006-04-24 13:02
Steven De Ryck wrote:
> Hi,
>
> I've got a TimeStamp field in MySql and want to use the hour and minutes
> in the value.
>
> for example:
>
> StartTime = Schedule.find(action2.ScheduleID).DateTimeStart.to_s
>
> That returns "Mon Feb 20 08:00:00 Romance Standard Time 2006"
>
> How do I get 08:00 returned ??
>
> Thanks !
>
> Steven.

try "date.to_formatted_s format_string"

it uses the same syntax as strftime documented here:
http://www.rubycentral.com/ref/ref_c_time.html#strftime

I would write what you need like this:

start_time =
Schedule.find(action2.schedule_id).date_time_start.to_formatted_s("%H:%M")
Steven D. (Guest)
on 2006-04-24 13:13
I allready tried that:

Schedule.find(action2.ScheduleID).DateTimeStart.to_formatted_s("%H:%M")

Still returns something like "Fri Feb 24 20:00:00 Romance Standard Time
2006" ..

Steven.

Alex W. wrote:
> Steven De Ryck wrote:
>> Hi,
>>
>> I've got a TimeStamp field in MySql and want to use the hour and minutes
>> in the value.
>>
>> for example:
>>
>> StartTime = Schedule.find(action2.ScheduleID).DateTimeStart.to_s
>>
>> That returns "Mon Feb 20 08:00:00 Romance Standard Time 2006"
>>
>> How do I get 08:00 returned ??
>>
>> Thanks !
>>
>> Steven.
>
> try "date.to_formatted_s format_string"
>
> it uses the same syntax as strftime documented here:
> http://www.rubycentral.com/ref/ref_c_time.html#strftime
>
> I would write what you need like this:
>
> start_time =
> Schedule.find(action2.schedule_id).date_time_start.to_formatted_s("%H:%M")
Steven D. (Guest)
on 2006-04-24 13:36
Nevermind, I solved it with:

" Schedule.find(action2.ScheduleID).DateTimeStart.strftime("%H:%M").to_s
"
This topic is locked and can not be replied to.