Convert Time Interval Given in Seconds into More Human Readable Form

Convert time interval given in seconds into more human readable form

With help of Royi we've got code that outputs time interval in a human readable form:

function millisecondsToStr (milliseconds) {
// TIP: to find current time in milliseconds, use:
// var current_time_milliseconds = new Date().getTime();

function numberEnding (number) {
return (number > 1) ? 's' : '';
}

var temp = Math.floor(milliseconds / 1000);
var years = Math.floor(temp / 31536000);
if (years) {
return years + ' year' + numberEnding(years);
}
//TODO: Months! Maybe weeks?
var days = Math.floor((temp %= 31536000) / 86400);
if (days) {
return days + ' day' + numberEnding(days);
}
var hours = Math.floor((temp %= 86400) / 3600);
if (hours) {
return hours + ' hour' + numberEnding(hours);
}
var minutes = Math.floor((temp %= 3600) / 60);
if (minutes) {
return minutes + ' minute' + numberEnding(minutes);
}
var seconds = temp % 60;
if (seconds) {
return seconds + ' second' + numberEnding(seconds);
}
return 'less than a second'; //'just now' //or other string you like;
}

Laravel - How to convert time in seconds to human readable

Usually you can use Carbon for all actions with date in laravel (https://carbon.nesbot.com/)

For your task:

CarbonInterval::seconds($duration['time_spent'])->cascade()->forHumans();

UPD:
blade file

\Carbon\CarbonInterval::seconds($duration['time_spent'])->cascade()->forHumans();

What date format is this? How do I convert it to a human readable date format in Javascript?

It's based on the Unix timestamp, but it's counting milliseconds rather than seconds. (ES2015 calls this the "Time Value".) The Date object in Javascript uses this value underneath the surface. If you use the integer value as a parameter in the Date constructor, you'll get a Date object which should be handled quite well by most browsers.

const happyDateObject = new Date(1540920856937);

If you want a bit more control over what's going on, or want some more utilities that help you customize what the date looks like and how to manipulate it, I'd recommend the moment.js library. It's widely used because it's so useful. Since it's really just a wrapper for the standard Javascript Date object, moment objects convert quite easily to Date objects (when you need to do so). You'd construct the value in a similar way:

const happyMoment = moment(1540920856937)

Converting seconds (unix time) to human readable (to different variables)

1) how it works

The number of seconds is converted to a structure that contains elements for year, month, day etc. Next this structure is printed to a string buffer in a standard format. Then you print this string buffer.

2) how to get to year, month, day etc.

Well, that is this struct tm. localtime returns a pointer to a struct tm and you copy the data of that structure to your own ts. Now you need to access the fields. You do need to know about this, but I'll help you:

struct tm {    // definition of struct tm
int tm_sec; /* seconds after the minute - [0,59] */
int tm_min; /* minutes after the hour - [0,59] */
int tm_hour; /* hours since midnight - [0,23] */
int tm_mday; /* day of the month - [1,31] */
int tm_mon; /* months since January - [0,11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday - [0,6] */
int tm_yday; /* days since January 1 - [0,365] */
int tm_isdst; /* daylight savings time flag */
};

To pass the proper fields to your function prevFullMoon ( int y, int m, int d... do:

prevFullMoon ( ts.tm_year+1900, ts.tm_mon+1, ts.tm_mday, ...

Converting Unix Timestamp into a Custom Human Readable time using mySQL

The timestamps contain milliseconds.

You can remove them if you divide by 1000:

SELECT id, firstName, lastName, tstamp, 
FROM_UNIXTIME(tstamp / 1000, '%Y %D %M %H:%i:%s') AS timeAndDate
FROM myTable;

See the demo.

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 human readable time duration

TIME_FORMAT(SEC_TO_TIME(task_records.time_spent),'%Hh %im')

Documentation is your friend:

  • http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

According to comment:

DROP FUNCTION IF EXISTS GET_HOUR_MINUTES;
CREATE FUNCTION GET_HOUR_MINUTES(seconds INT)
RETURNS VARCHAR(16)

BEGIN
DECLARE result VARCHAR(16);
IF seconds >= 3600 THEN SET result = TIME_FORMAT(SEC_TO_TIME(seconds),'%kh %lm');
ELSE SET result = TIME_FORMAT(SEC_TO_TIME(seconds),'%lm');
RETURN result;
END

DELIMETER ;

Usage:

SELECT GET_HOUR_MINUTES(task_records.time_spent) FROM table

How to convert milliseconds into human readable form?

Well, since nobody else has stepped up, I'll write the easy code to do this:

x = ms / 1000
seconds = x % 60
x /= 60
minutes = x % 60
x /= 60
hours = x % 24
x /= 24
days = x

I'm just glad you stopped at days and didn't ask for months. :)

Note that in the above, it is assumed that / represents truncating integer division. If you use this code in a language where / represents floating point division, you will need to manually truncate the results of the division as needed.

How to convert seconds to HH:mm:ss in moment.js

From this post I would try this to avoid leap issues

moment("2015-01-01").startOf('day')
.seconds(s)
.format('H:mm:ss');

I did not run jsPerf, but I would think this is faster than creating new date objects a million times

function pad(num) {
return ("0"+num).slice(-2);
}
function hhmmss(secs) {
var minutes = Math.floor(secs / 60);
secs = secs%60;
var hours = Math.floor(minutes/60)
minutes = minutes%60;
return `${pad(hours)}:${pad(minutes)}:${pad(secs)}`;
// return pad(hours)+":"+pad(minutes)+":"+pad(secs); for old browsers
}

function pad(num) {    return ("0"+num).slice(-2);}function hhmmss(secs) {  var minutes = Math.floor(secs / 60);  secs = secs%60;  var hours = Math.floor(minutes/60)  minutes = minutes%60;  return `${pad(hours)}:${pad(minutes)}:${pad(secs)}`;  // return pad(hours)+":"+pad(minutes)+":"+pad(secs); for old browsers}
for (var i=60;i<=60*60*5;i++) { document.write(hhmmss(i)+'<br/>');}

/* function show(s) { var d = new Date(); var d1 = new Date(d.getTime()+s*1000); var hms = hhmmss(s); return (s+"s = "+ hms + " - "+ Math.floor((d1-d)/1000)+"\n"+d.toString().split("GMT")[0]+"\n"+d1.toString().split("GMT")[0]);} */


Related Topics



Leave a reply



Submit