0

I have the following date format defined in a Rails initializer:

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. 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.

What's the easiest way I can do the conversion? Or is there a way in which I don't even need to?

XåpplI'-I0llwlg'I -
  • 21,649
  • 28
  • 102
  • 151

3 Answers3

0

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"
});
Mostafa Hussein
  • 11,063
  • 3
  • 36
  • 61
  • That's not what I'm asking. I only want to define my date format in one place - the Rails initializer. And from there, pass it to my JavaScript code when I need to. I don't want to have it to re-declare it in my JavaScript code like you've done above, because then I would have to manage my date formats in two places. – XåpplI'-I0llwlg'I - Sep 02 '13 at 21:30
  • well i tried to help.. in my case i was not able to find a way to make rails accept another format and was my only solution is to change the format of the datepicker itself.. – Mostafa Hussein Sep 02 '13 at 21:32
  • see this [question](http://stackoverflow.com/questions/5256510/how-to-change-default-format-of-date-in-rails-3) and this [question](http://stackoverflow.com/questions/1610485/default-date-format-in-rails-need-it-to-be-ddmmyyyy), both have a different way to change the rails date format – Mostafa Hussein Sep 02 '13 at 21:34
  • Thanks for trying, but that's still not what I'm asking. – XåpplI'-I0llwlg'I - Sep 02 '13 at 23:42
  • check this in [another way](http://stackoverflow.com/questions/4675189/change-default-ruby-time-format/4675236#4675236) , also i have read that it is not a good thing to mess with these settings affects what is stored in the database i have found this [here](http://stackoverflow.com/questions/7145152/change-default-date-format-in-ruby-on-rails) – Mostafa Hussein Sep 03 '13 at 00:32
0

You can use the strftime() method eg.d1.strftime('%d/%m/%Y');

for further details you can check this link strftime

Sagar.Patil
  • 991
  • 8
  • 17
0

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:

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):

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:

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.

Steve
  • 6,618
  • 3
  • 44
  • 42