John Smith
John Smith

Reputation: 6259

Take date from Form Data but Time from Actual Time

In one Form the user also has to specify a date for example the 21.04.2014:

So when a post request is sent to the rails-application, to save a post there is also provided a created_at data in the request:

post[text]:"First Message"  
post[created_at]:21.04.2014 

So at the end a message is saved. Now when i try to get the created_at for the message:

Message.find(1).created_at
>> 2014-04-21 00:00:00

But i would like that the time corresponds to the actual time when the message was created, so the ouput must look something like this:

>> 2014-04-21 19:52:07

How do i have to save the message so that it meets my concept? Thanks

Actual i save a message like this:

@user.message.new(message_params) 

def diagnosis_params
  params[:message].permit(:created_at, :text)
end

Upvotes: 0

Views: 43

Answers (1)

NilColor
NilColor

Reputation: 3542

You can do something like this:

dt = Date.parse('21.04.2014')
# => Mon, 21 Apr 2014

tm = Time.zone.now
# => Tue, 22 Apr 2014 00:34:07 MSK +04:00

created_at = DateTime.new(dt.year, dt.month, dt.day, tm.hour, tm.min, tm.sec)
# => Mon, 21 Apr 2014 00:34:07 +0000

Not very elegant but you can create some utility method...

Upvotes: 1

Related Questions