-1

How can I check within my Rails app what datetime format the user currently uses as his default?

I have this method:

def local_date(date, am_pm = false)
  unless am_pm
    date&.localtime&.strftime('(%d.%m.%Y, %H:%M)')
  else
    date&.localtime&.strftime('(%d.%m.%Y, %I:%M %p)')
  end
end

I need to set am_pm accordingly to users local machines datetime format WITHOUT relying on the :locale parameter as not everyone who speaks english uses am/pm

Ilja KO
  • 1,272
  • 12
  • 26
  • This answer might be helpful [Link](https://stackoverflow.com/questions/16210058/javascript-get-system-short-date-format) – Sikandar Tariq Sep 16 '18 at 05:21
  • so reading out the ``:locale`` parameter it is then? Isnt there anything? Like reading out the users local UTC and decide then if am or pm? – Ilja KO Sep 16 '18 at 05:31
  • use `Time.current.strftime("%p")` Time.current returns time according to the time zone – Sikandar Tariq Sep 16 '18 at 05:41
  • ``localtime`` does the same but I need to find out if the users machine, the pc he is on visiting my website has a 12-hour or 24-hour clock – Ilja KO Sep 16 '18 at 06:37

1 Answers1

0

This is achievable in Rails only with the help of a bit of client side JavaScript code. The client side code would detect whether the user is using 24 hours time format or 12 hours time format, and then store that information of a cookie.

Your server side code should then read that information from the cookie and set your time format accordingly.

Add this to your app/assets/javascript/application.js file.

function getCookie(cname) {
    var name = cname + "=";
    var decodedCookie = decodeURIComponent(document.cookie);
    var ca = decodedCookie.split(';');
    for(var i = 0; i <ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}






var date = new Date(Date.UTC(2012, 11, 12, 3, 0, 0));
var dateString = date.toLocaleTimeString();

//apparently toLocaleTimeString() has a bug in Chrome. toString() however returns 12/24 hour formats. If one of two contains AM/PM execute 12 hour coding.
if (dateString.match(/am|pm/i) || date.toString().match(/am|pm/i) )
{
    //12 hour clock

    //check if we are already rendering in 12 hours format
    if(getCookie("time_format") != "twelve") 
    {
       document.cookie = "time_format=twelve";

       /***
         Now force the browser to reload current page from server.
         Since we had set the the cookie, the server will now render 
         all pages in 12 hours format
       ****/

       location.reload(true).

    }
}
else
{
    //24 hour clock
    document.cookie = "time_format=twenty_four";
}

In your ApplicationController

class SomeController < ApplicationController
  around_faction :set_time_format

  def set_time_format
    if cookie[:time_format]=="twelve"
      #Set your desired time format string with 12 hour style
    else
      #default
      #Set your desired time format string with 24 hour style
    end
  end

end
Vishnuprasad R
  • 1,682
  • 13
  • 23