如何使用jQuery UI Datepicker和Rails更改日期格式? [英] How to Change the Date formatting with jQuery UI Datepicker and Rails?

查看:179
本文介绍了如何使用jQuery UI Datepicker和Rails更改日期格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能相信日期选择器可以是多么复杂。我已经看了Stackoverflow上的各种问题,但无法使格式化工作。我使用Railscasts#213日历(已修改)设置jQuery Datepicker。

在南非,我们使用dd / mm / yyyy格式,而不是mm / dd / yyyy格式,如大家所说。



第一个问题是当我选择在mm / dd / yyyy格式显示的日期时。我不能改变这一点 - 尝试改变咖啡脚本,但没有做任何事情:

  jQuery  - > 
$('#post_date')。datepicker()
dateFormat:dd / mm / yy
minDate:1w
pre>

发布之后显示为mm / dd / yyyy,但是现在我希望日期显示在文字中,并且以下代码2013年4月1日发布截至2013年1月4日。

 <%= @ post.date.strftime(%b%d%Y)% > 

我试着安装I18n并在en.yml上设置了默认值,但是没有任何影响: / b>

 日期:
格式:
预设值:「%d /%m /%y」

请有人可以直接给出答案,以便如何做到这一点???? $ b

解决方案

我无法帮助您使用jQuery datepicker,因为我从来没有使用它。也许这篇文章是你需要的: jQuery UI DatePicker - 更改日期格式



基本上,这应该可以解决您的问题:
$ b $ pre $ $ $ $ $' #post_date')。datepicker(dateFormat:'dd-mm-yy');

至于格式化打印日期,而不是使用strftime来做到这一点,您可以快速设置通过在应用中的config / initializers文件夹中创建date_formats.rb并添加以下行来为您的日期提供一个通用的默认格式:

  Date :: DATE_FORMATS [:default] =%d-%m-%Y


I can't believe how complicated the date picker can be. I have looked at various questions on Stackoverflow, but can't get the formatting to work. I used the Railscasts #213 Calendars (revised) to set up the jQuery Datepicker.

Here in South Africa we use the dd/mm/yyyy formating and not the mm/dd/yyyy formatting as everyone says.

First problem is when I pick the date it shows up on the form as mm/dd/yyyy. I can't change this - tried changing the Coffee Script, but that doesn't do anything:

  jQuery ->
   $('#post_date').datepicker()
   dateFormat: "dd/mm/yy"
   minDate: "1w"

After it posted it shows up as mm/dd/yyyy, but now I want the date to show up in words and with the folowing code 1 April 2013 comes out as 4 January 2013.

        <%= @post.date.strftime("%b %d %Y") %>

Ive tried installing I18n and setting the default on the en.yml, but that has no effect:

        date:
         formats:
          default: "%d/%m/%y"

Please can someone please give a straight answer as to how to do this????

解决方案

I can't help you with the jQuery datepicker as I have never used it. Maybe this post here is what you need: jQuery UI DatePicker - Change Date Format

Basically, this should solve your problem:

$('#post_date').datepicker(dateFormat: 'dd-mm-yy');

As far as formatting the date when printing it, instead of using strftime to do so you can quickly set a universal default format for your dates by creating date_formats.rb in the config/initializers folder in your app and adding this line:

Date::DATE_FORMATS[:default] = "%d-%m-%Y"

这篇关于如何使用jQuery UI Datepicker和Rails更改日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆