简体   繁体   English

如何更改 Rails 默认时间格式?

[英]How to change Rails default time format?

I am using this guide, http://blog.nicoschuele.com/posts/cheatsheet-to-set-app-wide-date-and-time-formats-in-rails , and it doesn't seem to be working.我正在使用本指南http://blog.nicoschuele.com/posts/cheatsheet-to-set-app-wide-date-and-time-formats-in-rails ,但它似乎不起作用。

Currently, whenever I make a JSON request and get back data my created_at and other time fields show iso8601 format.目前,每当我发出 JSON 请求并取回数据时,我的created_at和其他时间字段都会显示iso8601格式。

An example of the format is 2014-10-23T00:35:14.800Z .格式的一个例子是2014-10-23T00:35:14.800Z I would prefer something more readable like Sun, 3 Nov 2013 14:22:18 -0700我更喜欢像Sun, 3 Nov 2013 14:22:18 -0700这样更具可读性的东西

I did what the guide above said to do, but when I restart my server and fire off the request again, the JSON still returns the data in iso8601 format.我按照上面的指南做了,但是当我重新启动服务器并再次触发请求时,JSON 仍然以iso8601格式返回数据。

How do I remedy this?我该如何补救?

I don't recommend it but you can set it Rails wide as you've requested at this link: http://blog.nicoschuele.com/posts/cheatsheet-to-set-app-wide-date-and-time-formats-in-rails我不推荐它,但您可以按照您在此链接中的要求将其设置为 Rails 宽: http : //blog.nicoschuele.com/posts/cheatsheet-to-set-app-wide-date-and-time-轨道格式


I'll give you what I used for my html DateTime conversion.我会给你我用于我的 html DateTime 转换的内容。 I do not have a JSON answer, but I hope this'll give you the answer you're looking for.我没有 JSON 答案,但我希望这会给你你正在寻找的答案。 I agree with Nils Landt in我同意Nils Landt

You do not want to change the time format in the API.您不想更改 API 中的时间格式。 Keep it as ISO 8601, and change it on the client side.将其保留为 ISO 8601,并在客户端进行更改。 Way less hassle all around.周围少了很多麻烦。

Here's what I put in my controller:这是我在控制器中放入的内容:

before_action :correct_datetime, only: [:create, :update]
def correct_datetime
  params['event']['starts_at'] = DateTime.strptime(params['event']['starts_at'],'%m/%d/%Y %l:%M %p') unless params['event'].try {|i| if i.try(:has_key?, 'starts_at'); i['starts_at'].empty?; else; true; end }
  params['event']['ends_at'] = DateTime.strptime(params['event']['ends_at'],'%m/%d/%Y %l:%M %p') unless params['event'].try {|i| if i.try(:has_key?, 'ends_at'); i['ends_at'].empty?; else; true; end }
end

And in my view I'm using Bootstrap DateTimePicker for some text areas.在我看来,我在某些文本区域使用Bootstrap DateTimePicker

Starts at: <%= f.text_field :starts_at, value: (f.object.starts_at.strftime('%m/%d/%Y %l:%M %p') if f.object.starts_at), class: 'form-control', style: 'width:300px;' %>
Ends at: <%= f.text_field :ends_at, value: (f.object.ends_at.strftime('%m/%d/%Y %l:%M %p') if f.object.ends_at), class: 'form-control', style: 'width:300px;' %>

To allow the datetime picker to work I use the following in my event.js.coffee CoffeeScript file:为了让日期时间选择器工作,我在我的event.js.coffee CoffeeScript 文件中使用以下内容:

$ ->
  $("#event_starts_at").datetimepicker()
  $("#event_ends_at").datetimepicker()
  return

I apologize that I don't have the JSON results for this.我很抱歉没有 JSON 结果。 But I have given you the input and output formatting methods for accomplishing what you seek.但是我已经为您提供了完成您所寻求的输入和输出格式设置方法。

To convert from a DateTime object from the DB you use strftime .要从数据库中转换 DateTime 对象,请使用strftime And to format it back to save in the DB you use strptime .并将其格式化以保存在您使用的数据库中strptime

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM