Convert String to Date Object Ruby - ruby

I'm trying to comvert a string in my logs to a date object.
My string is 2018-09-18 11:42:50,286000201 which is the format YYYY-MM-DD hh:mm:ss,nnnnnnnnn
I'm trying to convert in to an object using the time library in ruby. The function I am using is Time.strptime('2018-09-18 11:42:50,286000201, '%Y-%m-%d %H:%M:%S,%9N')
Ruby is giving me an invalid striptime format. Any help would be appreciated. Cheers.

remove the 9, %N expects 9 digits bt default
Time.strptime('2018-09-18 11:42:50,286000201, '%Y-%m-%d %H:%M:%S,%N')
doc: http://ruby-doc.org/stdlib-2.1.1/libdoc/time/rdoc/Time.html#method-c-strptime

Related

How to convert yyyy-mm-ddThh:mm:ss [+-] hh:mm to timestamp yyyy-mm-dd hh:mm:ss in Datastge?

How to convert yyyy-mm-ddThh:mm:ss [+-] hh:mm to timestamp yyyy-mm-dd hh:mm:ss in Datastge?
Eg:
From
2021-01-26T01:07:00-05:00
To
2021-01-26 01:07:00
Depending on the original data type you could use string functions
and StringToTimestamp
Otherwise you could check out Date and Time functions as well.
If the data type is string, convert the "T" to a space " " using Convert() function.
Use Left() function to get rid of the "+/- hh:mm".
If the data type is timestamp, do nothing until you need to display it; the internal representation of a timestamp does not have a "T" in it. If you're writing it to a text file, for example, use TimestampToString() function to specify your desired format, although the default format may well suit your need.

How to change Current Date to MinguoDate in Java8?

I have to use the MinguoDate in my program that I am writing in Java8.
I have current date in string format:
String currentDate="2018-07-20";
Can anyone tell me how to convert current date to the Minguo date using java8
Note: MinguoDate calendar system is primarily used in Taiwan (Republic of China)
You can parse the string into a LocalDate instance and then put the result of that into the MinguoDate.from method to yield a MinguoDate.
MinguoDate minguoDate = MinguoDate.from(LocalDate.parse(currentDate));

Correct strptime format for Input Type Time on Rails 4

Want to use the
<input name=attendance[something] type="time">
For time input. However cant make it seem to match with a Datetime object; for manipulation before insertion to ActiveRecord
myTimeIn = Time.strptime(params[attendance][something],"%H:%M")
keeps getting
invalid strptime format - `%H:%M'
What is the correct format for a input type=time field?
Looks like the value of params[attendance][something] may be blank or not of the correct format. You could do something like below to avoid the error:
t = params[attendance][something]
myTimeIn = Time.strptime(t,"%H:%M") if t =~ /\d{1,2}:\d{2}/
As per this HTML example, the value produced by <input/> of type time is HH:MM

Invalid date (ArgumentError) when trying to convert to epoch time

I have a DateTime string in the format 24/May/2015:06:51:33
How can I convert it to epoch time?
I tried converting it using the method mentionedin this stackoverflow question but it seems that DateTime format is wrong. Then I tried converting my Date to another format using
DateTime.parse("24/May/2015:06:51:33").strftime("%Y-%m-%dT%H:%M:S%%z")
but that doesn't seem to work as well. Any help on how to do this? Note that I don't have my date variable in the date format. So I have to convert it from a string.
UPDATE
By doesn't seem to work I mean that the following error is thrown:
/home/keshav/Desktop/a.rb:6:in `parse': invalid date (ArgumentError)
from /home/keshav/Desktop/a.rb:6:in `<main>'
You can convert your DateTime object to an epoch timestamp like so:
datetime = DateTime.strptime("24/May/2015:06:51:33", "%d/%b/%Y:%H:%M:%S")
epoch = datetime.to_time.to_i
# => 1432450293
Hope it helps!

Ruby - convert string to date

I have a string like "2011-06-02T23:59:59+05:30".
I want to convert it to date format and need to parse only the date, "2011-06-02".
For Ruby 1.9.2:
require 'date' # If not already required. If in Rails then you don't need this line).
puts DateTime.parse("2011-06-02T23:59:59+05:30").to_date.to_s
require 'date'
d = Date.parse("2011-06-02T23:59:59+05:30")
d.strftime("%F")
Simplies way is
require 'date'
date = "2011-06-02T23:59:59+05:30".gsub(/T.*/, '')
DateTime.parse(date)
Time.parse() should allow you to parse the whole date time. Then you can use time.strftime( string ) to format that as just a date in a string.
date = Time.parse("2011-06-02T23:59:59+05:30")
date_string = time.strftime("%y-%m-%d")
of
date_string = time.strftime("%F")
(see Ruby Doc for Time for more output string formats)
The above should work if you want a string; if you want a date object to handle then the ruby Date class can help you handle it but I belive that everything still needs to be done with Time objects; see Ruby Doc for Date for details of the Date class.
Hope that helps, let me know if I have headed off in the wrong direction with my answer.

Resources