Rails 12 Hour Am/Pm Range for a Day

Converting a 12 hour time string with AM/PM to 24 hour time

Have you tried this?
time = "12:00 PM"

Time.strptime(time, "%I:%M %P").strftime("%H:%M")

Rails : simple_for_for time with 12 hour format

As per the documentation

HTML 5 date / time inputs are not generated by Simple Form by default, so using date, time or datetime will all generate select boxes using normal Rails helpers. We believe browsers are not totally ready for these yet, but you can easily opt-in on a per-input basis by passing the html5 option:

<%= f.input :time, as: :time, html5: true %>

This should fix your problem.

Convert 24 integer to 12 hour with am/pm

[14, 0, 23].map { |hour| Time.parse("#{hour}:00").strftime("%l %P") }
=> [" 2 pm", "12 am", "11 pm"]

Change 24 hour clock to 12 hour for created_at

Look up strftime (for any language), and build the actual template yourself. Per Time::DATE_FORMATS, the offending template is '%B %d, %Y %H:%M', so change the end to %I:%M %p.

(You also might want to add a new format to DATE_FORMATS...)

Converting 24 hour time to 12 hour time w/ AM & PM using Javascript

UPDATE 2: without seconds option

UPDATE: AM after noon corrected, tested: http://jsfiddle.net/aorcsik/xbtjE/

I created this function to do this:

function formatDate(date) {  var d = new Date(date);  var hh = d.getHours();  var m = d.getMinutes();  var s = d.getSeconds();  var dd = "AM";  var h = hh;  if (h >= 12) {    h = hh - 12;    dd = "PM";  }  if (h == 0) {    h = 12;  }  m = m < 10 ? "0" + m : m;
s = s < 10 ? "0" + s : s;
/* if you want 2 digit hours: h = h<10?"0"+h:h; */
var pattern = new RegExp("0?" + hh + ":" + m + ":" + s);
var replacement = h + ":" + m; /* if you want to add seconds replacement += ":"+s; */ replacement += " " + dd;
return date.replace(pattern, replacement);}
alert(formatDate("February 04, 2011 12:00:00"));

Rails get time in HH:MM AM/PM

I eproduced the error :

DateTime.parse(Time.now).strftime("%I:%M %p")
# TypeError: no implicit conversion of Time into String

This is because parse methods accept only String objects, not any other objects. But it is Time object in your case. What you have to do is :

DateTime.parse(time_object.to_s).strftime("%I:%M %p")
# or directly
time_object.strftime("%I:%M %p")

Rails group by hour of the day for created_at column

There was a little gotch with Groupdate Gem. In order to fix this I have to convert created_at to respective time zone.

range: start_date.to_datetime.in_time_zone(time_zone)..end_date.to_datetime.in_time_zone(time_zone).end_of_day).count


Related Topics



Leave a reply



Submit