我想格式化Time.Now函数以显示YYYY-MM-DD HH:MM:SS而不是:“ 2018-03-09 09:47:19 +0000”该函数需要放在Time.Now函数内。
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“)做
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) }
您可以使用该Time#strftime
方法将时间格式化为多种不同的格式,包括所需的格式。它使用一个参数来指定日期输出的格式。有关此方法的更多说明,请参见此方法的文档。
Time.now.strftime("%F %T")
该%F
指定我们想在日期YYYY-MM-DD
格式。这之后是一个空格,然后是%T
说明符,该说明符产生HH:MM:SS
时间输出。
要将其合并到您的代码中:
"erstellzeit": "#{Time.now.strftime("%F %T")}"
或者,如果您获得其他+0000
输出,请尝试:
"erstellzeit": "#{Time.now.strftime("%F %H:%M:%S")}"
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句