简体   繁体   English

如何将Rails%d /%m /%Y日期格式字符串转换为dd / mm / yyyy?

[英]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"
});

You can use the strftime() method eg. 您可以使用strftime()方法,例如。 d1.strftime('%d/%m/%Y');

for further details you can check this link strftime 有关更多详细信息,您可以检查此链接strftime

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.

相关问题 以离子/角度格式将字符串日期'd / m / y'设置为'dd / mm / yyyy' - Format string date 'd/m/y' to 'dd/mm/yyyy' in ionic / angular 在javascript中将日期格式从d / m / yyyy转换为yyyy-mm-dd - convert date format from d/m/yyyy to yyyy-mm-dd in javascript jQuery 屏蔽输入 - 将日期格式设置为 m/d/yyyy 或 m/dd/yyyy 或 mm/dd/yyyy 或 mm/d/yyyy - jQuery masked input - format date as m/d/yyyy or m/dd/yyyy or mm/dd/yyyy or mm/d/yyyy 如何将日期作为字符串转换为“dd/mm/yyyy”? - How to convert a date as a string to "dd/mm/yyyy"? 格式日期从 "M/D/YYYY HH:mm:ss" 到 "YYYY/MM/dd HH:mm" vanilla javascript - format date from "M/D/YYYY HH:mm:ss" to "YYYY/MM/dd HH:mm" vanilla javascript 如何检查允许m / d / y和yyyy-mm-dd生效的日期是否有效 - How can I check if a date is valid that allows m/d/y and yyyy-mm-dd to be valid 如何在js中将MM/dd/yyyy HH:mm:ss zzz格式的字符串转换为日期 - How to convert string of MM/dd/yyyy HH:mm:ss zzz format to date in js 打字稿将对象字符串转换为日期格式mm / dd / yyyy - Typescript convert object string to date format mm/dd/yyyy Javascript将字符串转换为日期格式yyyy-MM-dd - Javascript convert string to date format yyyy-MM-dd 将'yymmdd'格式的日期字符串转换为'MM-DD-YYYY' - Convert date string of 'yymmdd' format to 'MM-DD-YYYY'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM