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
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With