Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format datetime string

Tags:

datetime

ruby

I need my string "Wed, 26 May 2017 14:00:00 +0800" to be in format 2017-05-26 14:00:00 +0800 (it's ok to keep it as a string but not mandatory). What's the quickest way?

like image 834
Giovani Avatar asked May 22 '17 10:05

Giovani


2 Answers

parse the string and reformat it via strftime:

string = 'Wed, 26 May 2017 14:00:00 +0800'

Time.parse(string).strftime('%F %T %z')
#=> "2017-05-26 14:00:00 +0800"
like image 117
Stefan Avatar answered Sep 25 '22 01:09

Stefan


Use strftime to format date in ruby.

OR

Run Time.now

2017-05-22 15:56:51 +0530

OR

Try below code:

n="Wed, 26 May 2017 14:00:00 +0800"
m=n.to_datetime
m.strftime("%Y-%m-%d %H:%M:%S %z")

output: "2017-05-26 14:00:00 +0800"
like image 37
puneet18 Avatar answered Sep 22 '22 01:09

puneet18