Simple Way to Convert Hh:Mm:Ss (Hours:Minutes:Seconds.Split Seconds) to Seconds

Simple way to convert HH:MM:SS (hours:minutes:seconds.split seconds) to seconds

Try awk. As a bonus, you can keep the split seconds.

echo "00:20:40.25" | awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }'

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

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

How to convert a HH:MM:SS string to seconds with PHP?

The quick way:

echo strtotime('01:00:00') - strtotime('TODAY'); // 3600

Another converting hh:mm:ss to seconds


def getSec(s):
l = map(int, s.split(':')) # l = list(map(int, s.split(':'))) in Python 3.x
return sum(n * sec for n, sec in zip(l[::-1], (1, 60, 3600)))

getSec('20') # 20
getSec('1:20') # 80
getSec('1:30:01') # 5401

Convert time in HH:MM:SS format to seconds only?

No need to explode anything:

$str_time = "23:12:95";

$str_time = preg_replace("/^([\d]{1,2})\:([\d]{2})$/", "00:$1:$2", $str_time);

sscanf($str_time, "%d:%d:%d", $hours, $minutes, $seconds);

$time_seconds = $hours * 3600 + $minutes * 60 + $seconds;

And if you don't want to use regular expressions:

$str_time = "2:50";

sscanf($str_time, "%d:%d:%d", $hours, $minutes, $seconds);

$time_seconds = isset($seconds) ? $hours * 3600 + $minutes * 60 + $seconds : $hours * 60 + $minutes;

Convert HH:mm:ss in seconds

Based on Jesper's suggestion, here is the solution :

import java.sql.Time    

case class Transactions(creationSeconds: Int )

val formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")

def parseTransac(line: String): (Transactions) = {
val fields = line.split(',')
val fullDateTime = new Time(formatter.parse(fields(0)).getTime())
val creationTime = fullDateTime.toString.split(':')
val creationSeconds = creationTime(0).toInt*3600 + creationTime(1).toInt*60 + creationTime(2).toInt
val transactions = Transactions(creationSeconds)
(transactions)
}

and here is another solution based on Dipankar's hint:

import java.sql.Time
import org.joda.time.DateTime

case class Transactions(creationTime: Int)

val formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")

def parseTransac(line: String): (Transactions) = {
val fields = line.split(',')
val fullDatTime = new DateTime(formatter.parse(fields(0)).getTime())
val fromMidnight = fullDatTime.withTimeAtStartOfDay()
val duration = new Duration(fromMidnight, fullDatTime)
val creationTime = duration.toStandardSeconds().getSeconds()
val transactions = Transactions(creationTime)
(transactions)
}

How to Convert Hours, minutes and seconds (HH:mm:ss) to minutes and seconds (mm:ss) in SQL

Perhaps a lighter approach with less string manipulation

Example

Declare @T time = '02:47:10'

Select concat(datediff(MINUTE,0,@T),':',datepart(second,@T))

Results

167:10

JavaScript seconds to time string with format hh:mm:ss


String.prototype.toHHMMSS = function () {
var sec_num = parseInt(this, 10); // don't forget the second param
var hours = Math.floor(sec_num / 3600);
var minutes = Math.floor((sec_num - (hours * 3600)) / 60);
var seconds = sec_num - (hours * 3600) - (minutes * 60);

if (hours < 10) {hours = "0"+hours;}
if (minutes < 10) {minutes = "0"+minutes;}
if (seconds < 10) {seconds = "0"+seconds;}
return hours+':'+minutes+':'+seconds;
}

You can use it now like:

alert("5678".toHHMMSS());

Working snippet:





String.prototype.toHHMMSS = function () {

var sec_num = parseInt(this, 10); // don't forget the second param

var hours = Math.floor(sec_num / 3600);

var minutes = Math.floor((sec_num - (hours * 3600)) / 60);

var seconds = sec_num - (hours * 3600) - (minutes * 60);


if (hours < 10) {hours = "0"+hours;}

if (minutes < 10) {minutes = "0"+minutes;}

if (seconds < 10) {seconds = "0"+seconds;}

return hours + ':' + minutes + ':' + seconds;

}



console.log("5678".toHHMMSS());

Convert seconds to HH-MM-SS with JavaScript?

Updated (2020):

Please use @Frank's one line solution:

new Date(SECONDS * 1000).toISOString().substring(11, 16)

If SECONDS<3600 and if you want to show only MM:SS then use below code:

new Date(SECONDS * 1000).toISOString().substring(14, 19)

It is by far the best solution.


Old answer:

Use the Moment.js library.



Related Topics



Leave a reply



Submit