Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove the zone from a DateTime value?

Tags:

ruby

I have this DateTime:

=> Fri, 03 Feb 2012 11:52:42 -0500 

How can I remove the zone(-0500) in ruby? I just want something like this:

=> Fri, 03 Feb 2012 11:52:42 
like image 541
Dagosi Avatar asked Feb 03 '12 16:02

Dagosi


People also ask

How do I remove timezone from DateTime?

To remove timestamp, tzinfo has to be set None when calling replace() function. First, create a DateTime object with current time using datetime. now(). The DateTime object was then modified to contain the timezone information as well using the timezone.

How do I remove the timestamp from a DateTime in Python?

Using strfttime to Remove the Time from Datetime in Python We can use strftime() to easily remove the time from datetime variables. For example, if you want to print out the date in the format “YYYY-MM-DD”, we pass “%Y-%m-%d” to strfttime() and no time is printed.

How do you parse time in Ruby?

Ruby | DateTime parse() function DateTime#parse() : parse() is a DateTime class method which parses the given representation of date and time, and creates a DateTime object. Return: given representation of date and time, and creates a DateTime object.


1 Answers

Time always has a zone (it has no meaning without one). You can choose to ignore it when printing by using DateTime#strftime:

now = DateTime.now puts now #=> 2012-02-03T10:01:24-07:00  puts now.strftime('%a, %d %b %Y %H:%M:%S') #=> Fri, 03 Feb 2012 10:01:24 

See Time#strftime for the arcane codes used to construct a particular format.

Alternatively, you may wish to convert your DateTime to UTC for a more general representation.

like image 118
Phrogz Avatar answered Oct 10 '22 02:10

Phrogz