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!
Related
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.
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
I've seen that you can use an ".isValid()" function to check that a given string is in a date format:
moment('2007-05-05', 'YYYY-MM-DD', true).isValid()
But is there a way to confirm that the format is correct? For example:
'YYYY-MM-DD' should return true, but
'YYYY-MM-DDsadsadl' should return false since the characters at the end of the string aren't valid DateTime chars.
We're working on a tool that allows a user to input an existing date format, and then a second input to enter the desired format, but we need validation to ensure the string can properly parse and convert, but they aren't entering a specific date.
The application must accept any and all possible date formats.
Use the following function to validate your format.
validFormat = function(inputFormat){
var validation = moment(moment('2017-06-17').format(inputFormat), inputFormat).inspect();
if(validation.indexOf('invalid') < 0)
return true;
else
return false;
}
Do spend some time to understand this. This simply does a reverse verification using inspect(). The date 2017-06-17 can be replaced by any valid date.
This Moment Js Docs will help you identify the valid formats.
Just make a call to this function as
validFormat('YYYY MM DD')
const getIsValid = inputFormat => moment(moment().format(inputFormat), inputFormat).isValid()
Explanation:
moment().format(inputFormat) - Create a date string from the current time from that format
This is then wrapped with moment() to make that string a moment date object, defining the format to parse it with. Finally we call the isValid() property on that moment date object. This ensures we are able to both create and parse a moment with our custom format.
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
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.