使用 datejs 插件根据文化格式化日期
我正在使用 datejs 库,但日期格式有问题。 我需要在屏幕上打印今天的日期,不带小时。 当我运行这一行时:
Date.today().toString('d')
我得到了日期的日期部分(即 15)。
如果您查看文档,他们会指定“d”作为标准日期应该取决于文化 - 这正是我想要的。 http://code.google.com/p/datejs/wiki/FormatSpecifiers
如果
<script type="text/javascript" src="/scripts/date.js"></script>
我使用 en-GB
<script type="text/javascript" src="/scripts/date-en-GB.js"></script>
来打印 dd/mm/yyyy
我知道我可以在 tostring() 中指定 formatstring 或类似的内容:
(Date.today().toString(Date.CultureInfo.formatPatterns.shortDate)
但我更喜欢使用 d 选项。
感谢您的帮助, 皮尼。
i'm using datejs library and i have a problem with the date format.
I need to print to the screen todays date without the hour.
When i run this line:
Date.today().toString('d')
i'm getting the date portion of the date(i.e 15).
If you look at the documentation they specify that 'd' as standard date should be depanded on the culture - and this is exactly what i want.
http://code.google.com/p/datejs/wiki/FormatSpecifiers
i expect it to print mm/dd/yyyy if i'm using
<script type="text/javascript" src="/scripts/date.js"></script>
while if i'm using en-GB
<script type="text/javascript" src="/scripts/date-en-GB.js"></script>
to print dd/mm/yyyy
i know that i can specify formatstring in the tostring() or something like this:
(Date.today().toString(Date.CultureInfo.formatPatterns.shortDate)
But I much rather prefer to use the d option.
Thanks for any help,
Pini.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有几个问题:
1) wiki 文档与 http://code 主页上的文档不同.google.com/p/datejs/ 中,主页将您想要的说明符描述为自定义说明符,而不是标准说明符。维基上的文档似乎建议使用多个字符应该触发自定义模式,但正如您所指出的那样,这种情况不会发生。我没有看到任何报告此问题的错误,因此也许您想自己这样做并收到响应通知:http://code.google.com/p/datejs/issues/list
2) 您应该能够执行
Date.today().toShortDateString();
但这里似乎有一个错误(在所有文化文件中)。源列表:...但格式模式应为 Date.CultureInfo.formatPatterns.shortDate (或者模式应以不同方式定义或使用同义词定义)。请参阅 http://code.google.com/p/datejs/issues/详细信息?id=116 。
3)我发现唯一可以获得“自定义”说明符及其缩写字母支持的地方是还包含 test/scripts/date-functions.js 文件。
如果您报告问题,您可能会提出这个问题,因为仅包含位于“测试”文件夹中的文件来获得您想要的功能似乎不可靠。
A couple issues here:
1) The wiki docs differ from the docs on the main page of http://code.google.com/p/datejs/ in that the main page describes the specifier you want as a custom specifier, not a standard one. The docs on the wiki appear to suggest that using multiple characters should trigger the custom mode, but this does not occur as you also indicate. I didn't see any bugs reporting this, so maybe you'd like to do so yourself and get notified of responses: http://code.google.com/p/datejs/issues/list
2) You should be able to do
Date.today().toShortDateString();
but there appears to be a bug here (in all culture files). The source lists:... but the format pattern should be Date.CultureInfo.formatPatterns.shortDate (or the patterns should be defined differently or with synonyms). See http://code.google.com/p/datejs/issues/detail?id=116 .
3) The only place I found one could get support for the "custom" specifiers with their abbreviated letters was by also including the test/scripts/date-functions.js file.
You might bring this up if you report the issue, as it seems unreliable to include a file located only in a "test" folder to get the functionality you want.