[英]How to convert Rails %d/%m/%Y date format string to dd/mm/yyyy?
I have the following date format defined in a Rails initializer: 我在Rails初始化程序中定义了以下日期格式:
Date::DATE_FORMATS[:default] = '%d/%m/%Y'
I want to pass this date format string (NOT an actual date object) to a JavaScript-based datepicker widget . 我想将此日期格式字符串 (不是实际的日期对象)传递给基于JavaScript的datepicker小部件 。 However, the datepicker only accepts format strings without
%
's. 但是,日期选择器仅接受不带
%
的格式字符串。 For example, I would need the date format string above to be converted to dd/mm/YYYY
if I were to pass it to the widget. 例如,如果要将日期格式字符串传递给小部件,则需要将其转换为
dd/mm/YYYY
。
What's the easiest way I can do the conversion? 我进行转换的最简单方法是什么? Or is there a way in which I don't even need to?
还是有我不需要的方式?
This should make you able to save the dates which you choose from the datapicker to your database because it will use date rails format 这应该使您能够将从数据选择器中选择的日期保存到数据库中,因为它将使用日期栏格式
$(".date-single-view").datepicker({
dateFormat: "yyyy-mm-dd"
});
This kind of thing can be done most easily with a mapping and a regex method that supports replacements from a function. 通过支持功能替换的映射和正则表达式方法,可以最轻松地完成这种事情。
From the datepicker docs : 从datepicker文档 :
format
String. Default: “mm/dd/yyyy”
The date format, combination of d, dd, D, DD, m, mm, M, MM, yy, yyyy.
d, dd: Numeric date, no leading zero and leading zero, respectively. Eg, 5, 05.
D, DD: Abbreviated and full weekday names, respectively. Eg, Mon, Monday.
m, mm: Numeric month, no leading zero and leading zero, respectively. Eg, 7, 07.
M, MM: Abbreviated and full month names, respectively. Eg, Jan, January
yy, yyyy: 2- and 4-digit years, respectively. Eg, 12, 2012.
In Ruby (see strftime formats): 在Ruby中(请参阅strftime格式):
STRFTIME_TO_DATEPICKER = {
'-d' => 'd', # numeric date, no leading zero
'd' => 'dd', # numeric date, with leading zero
'a' => 'D', # abbreviated weekday name (e.g., Mon)
'A' => 'DD', # full weekday name (e.g., Monday)
'-m' => 'm', # numeric month, no leading zero
'm' => 'mm', # numeric month, with leading zero
'b' => 'M', # abbreviated month name (e.g., Jan)
'B' => 'MM', # full month name (e.g., January)
'y' => 'yy', # 2-digit year
'Y' => 'yyyy' # 4-digit year
}
puts '%d/%m/%Y'.gsub(/%(-?[dmyab])/i) { |match| STRFTIME_TO_DATEPICKER[$1] || match }
# => dd/mm/yyyy
puts '%d %-d %a %A %-m %m %b %B %y %Y %M %w'.gsub(/%(-?[dmyab])/i) { |match| STRFTIME_TO_DATEPICKER[$1] || match }
# => dd d D DD m mm M MM yy yyyy %M %w
In JavaScript: 在JavaScript中:
var convert_ruby_strftime_to_datepicker_format = (function () {
var format_conversions = {
'-d': 'd', // numeric date, no leading zero
'd' : 'dd', // numeric date, with leading zero
'a' : 'D', // abbreviated weekday name (e.g., Mon)
'A' : 'DD', // full weekday name (e.g., Monday)
'-m': 'm', // numeric month, no leading zero
'm' : 'mm', // numeric month, with leading zero
'b' : 'M', // abbreviated month name (e.g., Jan)
'B' : 'MM', // full month name (e.g., January)
'y' : 'yy', // 2-digit year
'Y' : 'yyyy' // 4-digit year
};
function convert_strftime(match, format) {
return format_conversions[format] || match;
}
return function (strftime) {
return strftime.replace(/%(-?[dmyab])/gi, convert_strftime);
};
})();
console.log(convert_ruby_strftime_to_datepicker_format('%d/%m/%Y'));
// dd/mm/yyyy
console.log(convert_ruby_strftime_to_datepicker_format('%d %-d %a %A %-m %m %b %B %y %Y %M %w'));
// dd d D DD m mm M MM yy yyyy %M %w
Known formats are converted, and unknown formats are passed through. 转换已知格式,并传递未知格式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.