I want to format the Time.Now function to show YYYY-MM-DD HH:MM:SS instead of: "2018-03-09 09:47:19 +0000" The function needs to be placed within the Time.Now function.
require ‘roo’
require ‘roo-xls’
require ‘byebug’
file_name = ARGV.first || “Template.xlsx”
excel_file = Roo::Spreadsheet.open(“./#{file_name}“, extension: :xlsx)
xml = Nokogiri::XML::Builder.new(encoding: ‘ISO-8859-15’) do |xml|
xml.LIEFERUNG
(“xmlns”: “http://testtest“, “xmlns:xsi”: “testtesttest”, “xsi:schemaLocation”: “testestest”, “version”: “1.0", “erstellzeit”: “#{Time.now.strftime(“%F %H:%M:%S”)}“, “stufe”: “Produktion”) do
xml.ABSENDER do
xml.KREDITGEBERNR “1206992”
xml.Name “ALL”
end
xml.MELDUNGGROMIO(“erstellzeit”: “#{Time.now.strftime(“%F %H:%M:%S”)}“) do
xml.MELDER do
xml.KREDITGEBERNR “55093629”
xml.Name “Finance”
end
File.open(“output.xml”, “w”) { |file| file.write(xml) }
You can use the Time#strftime
method to format the time into a variety of different formats, including the one which you need. It takes a parameter which specifies the format of the date output. Look at the documentation for this method for more instructions about how to use it.
Time.now.strftime("%F %T")
The %F
specifies that we would like the date in YYYY-MM-DD
format. This is followed by a space, then the %T
specifier, which produces a HH:MM:SS
time output.
To incorporate this into your code:
"erstellzeit": "#{Time.now.strftime("%F %T")}"
Alternatively, if you're getting an additional +0000
output, try:
"erstellzeit": "#{Time.now.strftime("%F %H:%M:%S")}"
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