I am using jquery.datetime that inputs a date as a string into simpleform. It is supplying the date as %d %m %Y which is what I want but I am getting %m %d %Y because that is apparently the default for Simple Form:
The solution which is the same as for jquery datepicker is to add a custom input:
class DatePickerInput < SimpleForm::Inputs::StringInput
def input
value = object.send(attribute_name) if object.respond_to? attribute_name
input_html_options[:value] ||= I18n.localize(value) if value.present?
input_html_classes << "datepicker"
super # leave StringInput do the real rendering
end
end
This appears to be fine but it uses i118n to read the format I have a en.yml and a simple_form.en.yml initializer files under locales. I have tried adding:
datetime:
formats:
default: %d %m %Y
To both separately but when I do I get the following error message that says;
CAN NOT LOAD TRANSLATIONS
How can I modify my date time format?
Looks like you have problem with yml formatting - it's one space instead of two. Try to change it to:
datetime:
formats:
default: %d %m %Y
The Answer is partially correct but the format also needs to be quoted:
datetime:
formats:
default: '%d %m %Y'
Related
I have a LaTeX document that I convert to HTML using Pandoc. My template references the $date$ variable like so:
$if(date)$
<p>$date$</p>
$endif$
As usual, the value of $date$ is the ISO formatted date (e.g., 2022-06-17 for today). I want to format it so that it has a human-readable format, so that I can get this output:
<p>17 Jun 2022</p>
Usually, one can achieve this using format strings (for the above example: %d %b %Y). Is there a way to do that for the Pandoc's output?
A simple Lua filter can achieve this.
The following filter code is adapted from Setting the date in the metadata from the Pandoc documentation.
-- filters/date-format.lua
function Meta(meta)
if meta.date then
local format = "(%d+)-(%d+)-(%d+)"
local y, m, d = pandoc.utils.stringify(meta.date):match(format)
local date = os.time({
year = y,
month = m,
day = d,
})
local date_string = os.date("%d %b %Y", date)
meta.date = pandoc.Str(date_string)
return meta
end
end
---
# test.md
title: My Title
date: 2022-06-18
---
<!-- templates/date.html -->
$if(date)$
<p>$date$</p>
$endif$
Compile with the command pandoc --data-dir=. --template=date --lua-filter=date-format.lua test.md -o test.html.
The output should be <p>18 Jun 2022</p>.
Hi I would like to subtract time from a CSV array using Ruby
time[0] is 12:12:00AM
time[1] is 12:12:01AM
Here is my code
time_converted = DateTime.parse(time)
difference = time_converted[1].to_i - time_converted[0].to_i
p difference
However, I got 0
p time[0].to_i gives me 12
is there a way to fix this?
You can use Time#strptime to define the format of the parsed string.
In your case the string is %I:%M:%S%p.
%I = 12 hour time
%M = minutes
%S = seconds
%p = AM/PM indicator
So to parse your example:
require 'time'
time = %w(12:12:00AM 12:12:01AM)
parsed_time = time.map { |t| Time.strptime(t, '%I:%M:%S%p').to_i }
parsed_time.last - parsed_time.first
=> 1
Use the Ruby DateTime class and parse your dates into objects of that class.
My project is supposed to fetch specific values from multiple hashes a put those values in a text file. Ideally what I need my code to do is to have every date for the employees be seven days apart, so the text file would look something like this:
"Rachel Thorndike
2017-10-09-T04:29:46-05:00
Stacie Smith
2017-10-16-T04:29:46-05:00"
What this is supposed to do is fetch employee's names and put the time of their "handoff" on the line under them. I looked online and found the DateTime that Ruby features but it looks like whatever I do isn't working. My code is this:
require 'date'
jsonUser["users"].each do |user|
somefile.puts user["user"]["summary"]
print 'Handoff Date + Time: '
parsed = DateTime.strptime(jsonUser["start"], '%d-%m-%Y %H:%M')
utc = parsed.next_day(7).strftime('%d-%m-%Y %H:%M')
puts utc
end
But terminal returns this code with an error 'strptime': invalid date (ArgumentError). Would anybody help me get this code to work the way I want to? Anything that points me to the right direction? With explanations, if it isn't too much.
Thank you so much!
Update
I was able to get the iso8601 to appear under their name. My new code is
require 'date'
jsonUser["users"].each do |user|
somefile.puts user["user"]["summary"]
print 'Handoff Date + Time: '
parsed = DateTime.iso8601(jsonUser["start"])
utc = parsed.next_day(7).iso8601
somefile.puts utc
end
BUT the .next_day method isn't increasing by 7 days that I want too. Thought? I have only got one value that is appearing and that is going under every line. Its the value of jsonUser["start"] + 7 days...so `2017-
10-16T04:29:46-05:00`
This is what parse.next_day(7) gives me.
"Sr Chid
Handoff Date + Time: 2017-10-16T04:29:46-05:00
Ash A
Handoff Date + Time: 2017-10-16T04:29:46-05:00
Ven D
Handoff Date + Time: 2017-10-16T04:29:46-05:00
Abhi S
Handoff Date + Time: 2017-10-16T04:29:46-05:00"
The value of jsonUser["start"] is 2017-10-09T04:29:46-05:00 so the good thing is that it did increase by 7 but it only did it once.
Update for Amadan
require 'date'
date = DateTime.iso8601(jsonUser["start"])
jsonUser["users"].each do |user|
if user["user"]["self"] == nil
nil
else
somefile.puts user["user"]["summary"].gsub(/\w+/, &:capitalize).gsub(/[.]/, ' ')
somefile.print 'Handoff Date + Time: '
date = date.next_day(7)
somefile.puts date.iso8061
end
end
If I use Date#strptime to parse an Exif date like 2017:03:11 18:02:30 the time is ignored:
Date.strptime("2017:03:11 18:02:30", '%Y:%m:%d %H:%M:%S').strftime('%Y:%m:%d %H:%M:%S')
=> "2017:03:11 00:00:00"
What am I doing wrong?
Date doesn't contain information about the exact time, use DateTime instead:
DateTime.strptime("2017:03:11 18:02:30", '%Y:%m:%d %H:%M:%S').strftime('%Y:%m:%d %H:%M:%S')
=> "2017:03:11 18:02:30"
Using the boost library how would I convert a date object:
date d(2010,10,01);
to a string with the format: DD-mmm-YYYY, so that variable
d would become "01-Oct-2010".
Now there are number of functions for converting a date object to a
string such as
std::string to_simple_string(date d)
which returns a string in the format YYYY-mmm-DD. But I was unable
to find the format I need.
Thanks!
Have you read the documentation about date facet? The example appears like it should work for your scenario.
//example to customize output to be "LongWeekday LongMonthname day, year"
// "%A %b %d, %Y"
date d(2005,Jun,25);
date_facet* facet(new date_facet("%A %B %d, %Y"));
std::cout.imbue(std::locale(std::cout.getloc(), facet));
std::cout << d << std::endl;
// "Saturday June 25, 2005"