How to convert an H:MM:SS time string to seconds in Python?
def get_sec(time_str):
"""Get seconds from time."""
h, m, s = time_str.split(':')
return int(h) * 3600 + int(m) * 60 + int(s)
print(get_sec('1:23:45'))
print(get_sec('0:04:15'))
print(get_sec('0:00:25'))
Convert a duration %H:%M:%S to seconds
After we convert it to POSIXlt
, extract the hour
, min
and sec
and do the arithmetic
v1 <- strptime(durations, format='%H:%M:%S')
v1$hour * 3600 + v1$min * 60 + v1$sec
#[1] 3661
Or
c(unlist(unclass(v1)[c("hour", "min", "sec")]) %*% c(3600, 60, 1))
How to Convert time in minutes:seconds format to just seconds?
Here is an option with period_to_seconds
after converting the Time column to Period
class with ms
. Then, we multiply the 'Seconds' and 'Period' to create the 'TimePassed'
library(lubridate)
df1$Seconds <- period_to_seconds(ms(df1$Time))
df1$TimePassed <- df1$Seconds * df1$Period
-output
df1
# Time Period Seconds TimePassed
#1 6:21 1 381 381
#2 8:52 2 532 1064
#3 15:00 2 900 1800
data
df1 <- structure(list(Time = c("6:21", "8:52", "15:00"), Period = c(1L,
2L, 2L)), class = "data.frame", row.names = c(NA, -3L))
Convert HH:MM:SS string to seconds only in javascript
Try this:
var hms = '02:04:33'; // your input string
var a = hms.split(':'); // split it at the colons
// minutes are worth 60 seconds. Hours are worth 60 minutes.
var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]);
console.log(seconds);
Parsing seconds from mm.ss.00 string
Please try:
=RIGHT(A1,LEN(A1)-FIND(".",A1))+60*LEFT(A1,FIND(".",A1))
This treats the likes of 1.44.72
as a string and manipulates it to strip off the numeric characters up to and including the first full stop (period in USA). The part including and after the plus sign may not be required if the minutes are to be ignored as it takes the number before the first full stop (minutes) converts to seconds and adds that to the seconds and hundredths of seconds from the first part of the formula.
The addition process converts the text result from string manipulation into a numeric format, though this could be achieved for seconds and hundredths of seconds in other ways.
Related Topics
Ctree() - How to Get the List of Splitting Conditions for Each Terminal Node
How to Subset Data.Frames Stored in a List
R View() Does Not Display All Columns of Data Frame
Ggplot Us State Map; Colors Are Fine, Polygons Jagged - R
What's My User Agent When I Parse Website with Rvest Package in R
How to Use Grid to Edit a Ggplot2 Object to Add Math Expressions to Facet Labels
The Result of Rpart Is Just with 1 Root
How to Name the List of the Group_Split Output in Dplyr
Find the Probability Density of a New Data Point Using "Density" Function in R
Sum Nlayers of a Rasterstack in R
How to Change a Value Coded as "Yes" to a Value of 1 in R
Geom_Line - Different Colour in the Same Line
Filter Each Column of a Data.Frame Based on a Specific Value