Convert 12 hour character time to 24 hour
This should work:
times <- c("9:06 AM", "4:42 PM", "3:05 PM", "12:00 PM", "3:38 AM")
strptime(times, "%I:%M %p")
## [1] "2015-04-23 09:06:00 EDT" "2015-04-23 16:42:00 EDT"
## [3] "2015-04-23 15:05:00 EDT" "2015-04-23 12:00:00 EDT"
## [5] "2015-04-23 03:38:00 EDT"
If you want just the times:
format(strptime(times, "%I:%M %p"), format="%H:%M:%S")
## [1] "09:06:00" "16:42:00" "15:05:00" "12:00:00" "03:38:00"
How to convert a 12 hour time stamp (NOT a datetime, time only) to 24 hour time?
Try
lab_data[,LAB_TM_test:=as.POSIXct(LAB_TM, format='%I:%M:%S %p')]
How to convert character date time if time mixed between 12 hour and 24 hour time?
Try lubridate
, it is a very robust package for dates and times:
library(lubridate)
ymd_hms(c("2019-01-20 10:25:00 PM", "2019-01-20 22:25:00"))
[1] "2019-01-20 22:25:00 UTC" "2019-01-20 22:25:00 UTC"
Check ?ymd_hms
for more options.
Convert 12 hour to 24 hour format in R
As commented @lmo, you need to use %I
parameter instead of %H
, from ?strptime
:
%H
Hours as decimal number (00–23). As a special exception strings
such as 24:00:00 are accepted for input, since ISO 8601 allows these.%I
Hours as decimal number (01–12).
strptime("April 21 2016 09:50:49 PM", "%B %d %Y %I:%M:%S %p")
# [1] "2016-04-21 21:50:49 EDT"
How to convert hour:minute (HH:MM) string to 24 hour time format in R
You could do:
transform(df, new=paste(as.numeric(substr(Time, 1, 2))%%12+12, substr(Time, 4, 5), sep=":"))
Time new
1 02:14 14:14
2 04:16 16:16
3 01:15 13:15
4 12:30 12:30
5 13:50 13:50
Using tidyverse:
library(tidyverse)
df %>%
separate(Time, c('hr', 'min'), remove = FALSE) %>%
mutate(hr = sprintf('%02d', as.numeric(hr) %% 12 + 12)) %>%
unite(Time_converted, hr, min, sep = ':')
Time Time_converted
1 02:14 14:14
2 04:16 16:16
3 01:15 13:15
4 12:30 12:30
5 13:50 13:50
convert 12 hour clock to 24 hour time in sparklyr
You can set spark.sql.legacy.timeParserPolicy to LEGACY as shown below:
spark.conf.set("spark.sql.legacy.timeParserPolicy","LEGACY")
After this, you should not get the error while parsing datetime.
Since there are some changes related to datetime parser in spark v3.0 (Read here), you are getting that error.
Read datetime patterns here. According to this you can use pattern 'a' for PM or AM parsing.
to_date("datetime_12", "yyyy-MM-dd hh:mm:ss a")
convert 12-hour hh:mm AM/PM to 24-hour hh:mm
Try this
var time = $("#starttime").val();
var hours = Number(time.match(/^(\d+)/)[1]);
var minutes = Number(time.match(/:(\d+)/)[1]);
var AMPM = time.match(/\s(.*)$/)[1];
if(AMPM == "PM" && hours<12) hours = hours+12;
if(AMPM == "AM" && hours==12) hours = hours-12;
var sHours = hours.toString();
var sMinutes = minutes.toString();
if(hours<10) sHours = "0" + sHours;
if(minutes<10) sMinutes = "0" + sMinutes;
alert(sHours + ":" + sMinutes);
12-hour AM/PM format to military (24-hour) time
You have special handling for midnight. You need special handling for noon as well, and you need to fix midnight handling, too.
By convention, 12 AM denotes midnight and 12 PM denotes noon. Your code does it the other way around, converting 12:00:00 AM to 12:00:00 (noon) and 12:00:00 PM to midnight.
One simple way of dealing with time conversion problems is to convert the input to a number of seconds from midnight, and then format this number as the desired output. This approach eliminates character manipulation (adding 12 one digit at a time) and make the code more readable in general.
Convert 12 hour time to 24 hour using Joda time
Your Pattern is incorrect. Please use the following:
DateTimeFormatter formatter= DateTimeFormat.forPattern("hh:mm aa");
Edit: Here is a link to the Documentation for DateTimeFormat.
Related Topics
Row Operations in Data.Table Using 'By = .I'
Extract File Extension from File Path
How to Annotate a Reference Line at the Same Angle as the Reference Line Itself
Change Path.Expand Location (Win 7)
How to Deal with Nas in Residuals in a Regression in R
Efficient Calculation of Matrix Cumulative Standard Deviation in R
Replicate Each Row of Data.Frame and Specify the Number of Replications for Each Row
If {...} Else {...}:Does the Line Break Between "}" and "Else" Really Matters
How to Increase the Limit for Max.Print in R
Specify Widths and Heights of Plots with Grid.Arrange
Ggplot Scale Color Gradient to Range Outside of Data Range
Combining S4 and S3 Methods in a Single Function
R Shiny Table Not Rendering HTML
Combining Duplicated Rows in R and Adding New Column Containing Ids of Duplicates
Time Series Plot Gets Offset by 2 Hours If Scale_X_Datetime Is Used