Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting Date/Time in Ruby to YYYY-MM-DD HH:MM:SS [duplicate]

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) }
like image 748
GLH Avatar asked Mar 09 '18 10:03

GLH


1 Answers

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")}"
like image 114
Aaron Christiansen Avatar answered Oct 16 '22 10:10

Aaron Christiansen