2
如何将moment()
格式化为Rails日期时间?将MomentJS格式化为Rails日期时间
Im使用react-datepicker和Rails 5作为后端。
阵营:
let newDate = this.state.startDate.format('YYYY-MM-DD HH:mm')
console.log(newDate); #=> 2016-10-11 08:46
在momentjs formats看,我什么也看不到接近轨日期时间格式,即:Mon, 10 Oct 2016 15:16:29 UTC +00:00
目的是通过newDate
为params
到Rails控制器:params[:new_date]
更新:
cema-sp的答案是我的答案的最后一部分,但我得到的错误。为了解决这样的:
# JS:
let newDate = this.state.startDate.format(); # no format type
# Controller:
DateTime.strptime(params[:new_date], '%Y-%m-%dT%H:%M:%S%z')
如果您重写'#new_date =',当您尝试为其分配实际的'DateTime'时会出错。可能要添加'if value.is_a? String'。此外,您需要将解析的值传递给'super'或调用'write_attribute',只是分配给'@ new_date'将不会保留它。 – gmcnaughton
@gmcnaughton感谢您的评论!我修复了回答 –
感谢您的修复! :) – gmcnaughton