How to use Countdown Timer in NativeScript? - angular2-nativescript

How can I Implement Countdown Timer with date format in NativeScript ?
setInterval(() => {
}, 1000);

getRemainingTime(remainingMillis) {
setInterval(() => {
let seconds = Math.floor(remainingMillis / 1000);
let minute = Math.floor(seconds / 60);
seconds = seconds % 60;
let hour = Math.floor(minute / 60);
minute = minute % 60;
const day = Math.floor(hour / 24);
hour = hour % 24;
let remainTime
if (day > 0) {
remainTime = day + "d:" + hour + "h:" + minute + "m:" + seconds + "s";
} else {
remainTime = hour + "h:" + minute + "m:" + seconds + "s";
}
remainingMillis = remainingMillis - 1000;
console.log(remainTime);
}, 1000);
}

Related

calculate hours/minutes between two times in dart

I want to get/calculate hours and minutes between two times in dart.
Example: if start_time is 17:30 and end_time is 09:00 (day after) it should return total: 15 hours 30 minutes
My code:
check_two_times_is_before(String start_time, String end_time){
var format = DateFormat("HH:mm");
var start = format.parse(start_time);
var end = format.parse(end_time);
if(start.isAfter(end)) {
// do something here
}
}
You can use difference method on DateTime class.
check_two_times_is_before(String start_time, String end_time){
var format = DateFormat("HH:mm");
var start = format.parse(start_time);
var end = format.parse(end_time);
if(start.isAfter(end)) {
end = end.add(Duration(days: 1));
Duration diff = end.difference(start);
final hours = diff.inHours;
final minutes = diff.inMinutes % 60;
print('$hours hours $minutes minutes');
}
}
You can try it with Duration class
Duration duration = end.difference(start).abs();
final hours = duration.inHours;
final minutes = duration.inMinutes % 60;
print('$hours hours $minutes minutes');
The question is pretty old but here is another solution which might help someone.
Duration duration = DateTime.fromMillisecondsSinceEpoch(timestmap).difference(DateTime.now()).abs();
final days = duration.inDays;
final hours = duration.inHours - (days * 24);
final minutes = duration.inMinutes - (days * 24 * 60) - (hours * 60);
final seconds = duration.inSeconds - (days * 24 * 60 * 60) - (hours * 60 * 60) - (minutes * 60);
Nothing to explain here really. We're just using basic algebra. We're getting the total number of milliseconds between the two timestamps and working that down to days, hours, minutes and seconds.

How to display date-time like 6h ago format in Ionic4

In my database time store like 2019-02-14 06:13:03 how to display in formate of 6h ago or 2 day ago . I am using laravel api.
3d ago or
18h ago
First step Create pipe somewhere and your code will be
import { Pipe, PipeTransform } from '#angular/core';
#Pipe({ name: 'timeAgo' })
export class TimeAgo implements PipeTransform {
transform(d: any): string {
let currentDate = new Date(new Date().toUTCString());
let date = new Date(d + "Z");
let year = currentDate.getFullYear() - date.getFullYear();
let month = currentDate.getMonth() - date.getMonth();
let day = currentDate.getDate() - date.getDate();
let hour = currentDate.getHours() - date.getHours();
let minute = currentDate.getMinutes() - date.getMinutes();
let second = currentDate.getSeconds() - date.getSeconds();
let createdSecond = (year * 31556926) + (month * 2629746) + (day * 86400) + (hour * 3600) + (minute * 60) + second;
if (createdSecond >= 31556926) {
let yearAgo = Math.floor(createdSecond / 31556926);
return yearAgo > 1 ? yearAgo + " years ago" : yearAgo + " year ago";
} else if (createdSecond >= 2629746) {
let monthAgo = Math.floor(createdSecond / 2629746);
return monthAgo > 1 ? monthAgo + " months ago" : monthAgo + " month ago";
} else if (createdSecond >= 86400) {
let dayAgo = Math.floor(createdSecond / 86400);
return dayAgo > 1 ? dayAgo + " days ago" : dayAgo + " day ago";
} else if (createdSecond >= 3600) {
let hourAgo = Math.floor(createdSecond / 3600);
return hourAgo > 1 ? hourAgo + " hours ago" : hourAgo + " hour ago";
} else if (createdSecond >= 60) {
let minuteAgo = Math.floor(createdSecond / 60);
return minuteAgo > 1 ? minuteAgo + " minutes ago" : minuteAgo + " minute ago";
} else if (createdSecond < 60) {
return createdSecond > 1 ? createdSecond + " seconds ago" : createdSecond + " second ago";
} else if (createdSecond < 0) {
return "0 second ago";
}
}
}
And after that incluse timeAgo pipe in app.component.ts something like :
declarations: [
AppComponent,
TimeAgo,
...,
],
After that your html code will be something like
<p>{{timeValue | timeAgo}}</p>
Here i created Stackblitz project for you.

Swift Convert decimal coordinate into degrees, minutes, seconds, direction

How can I covert this to swift? My best guess is that all the int get changed to a var. Removing all the # that lead ". Also if any can point me to a good source to learn how things convert that would great.
- (NSString*)coordinateString {
int latSeconds = (int)(self.latitude * 3600);
int latDegrees = latSeconds / 3600;
latSeconds = ABS(latSeconds % 3600);
int latMinutes = latSeconds / 60;
latSeconds %= 60;
int longSeconds = (int)(self.longitude * 3600);
int longDegrees = longSeconds / 3600;
longSeconds = ABS(longSeconds % 3600);
int longMinutes = longSeconds / 60;
longSeconds %= 60;
NSString* result = [NSString stringWithFormat:#"%d°%d'%d\"%# %d°%d'%d\"%#",
ABS(latDegrees),
latMinutes,
latSeconds,
latDegrees >= 0 ? #"N" : #"S",
ABS(longDegrees),
longMinutes,
longSeconds,
longDegrees >= 0 ? #"E" : #"W"];
return result;
}
My attempt to convert it but Xcode proves me wrong. Reposted the fix suggest with the ABS. Does it look correct now?
func coordinateString {
var latSeconds = (Int8)(self.latitude * 3600);
var latDegrees = latSeconds / 3600;
latSeconds = abs(latSeconds % 3600);
var latMinutes = latSeconds / 60;
latSeconds %= 60;
var longSeconds = (Int8)(self.longitude * 3600);
var longDegrees = longSeconds / 3600;
longSeconds = abs(longSeconds % 3600);
var longMinutes = longSeconds / 60;
longSeconds %= 60;
var result = (String(format: "%d°%d'%d\"%# %d°%d'%d\"%#"),
abs(latDegrees),
latMinutes,
latSeconds,
latDegrees >= 0 ? "N" : "S",
abs(longDegrees),
longMinutes,
longSeconds,
longDegrees >= 0 ? "E" : "W",
return result;
}
Xcode 12 • Swift 5 or later
extension BinaryFloatingPoint {
var dms: (degrees: Int, minutes: Int, seconds: Int) {
var seconds = Int(self * 3600)
let degrees = seconds / 3600
seconds = abs(seconds % 3600)
return (degrees, seconds / 60, seconds % 60)
}
}
extension CLLocation {
var dms: String { latitude + " " + longitude }
var latitude: String {
let (degrees, minutes, seconds) = coordinate.latitude.dms
return String(format: "%d°%d'%d\"%#", abs(degrees), minutes, seconds, degrees >= 0 ? "N" : "S")
}
var longitude: String {
let (degrees, minutes, seconds) = coordinate.longitude.dms
return String(format: "%d°%d'%d\"%#", abs(degrees), minutes, seconds, degrees >= 0 ? "E" : "W")
}
}
let latitude = -22.9133950
let longitude = -43.2007100
let location = CLLocation(latitude: latitude, longitude: longitude)
location.latitude // "22°54'48"S"
location.longitude // "43°12'2"W"
location.dms // "22°54'48"S 43°12'2"W"
I have just edited Leo Dabus answer to get those String as separate values. In case it is helpful for others:
Swift 3 and Swift 4
func getLocationDegreesFrom(latitude: Double) -> String {
var latSeconds = Int(latitude * 3600)
let latDegrees = latSeconds / 3600
latSeconds = abs(latSeconds % 3600)
let latMinutes = latSeconds / 60
latSeconds %= 60
return String(
format: "%d°%d'%d\"%#",
abs(latDegrees),
latMinutes,
latSeconds,
latDegrees >= 0 ? "N" : "S"
)
}
func getLocationDegreesFrom(longitude: Double) -> String {
var longSeconds = Int(longitude * 3600)
let longDegrees = longSeconds / 3600
longSeconds = abs(longSeconds % 3600)
let longMinutes = longSeconds / 60
longSeconds %= 60
return String(
format: "%d°%d'%d\"%#",
abs(longDegrees),
longMinutes,
longSeconds,
longDegrees >= 0 ? "E" : "W"
)
}
I took the code of Leo Dabus and fixed a bug related with the detection of the cardinal directions when the values of the latitude or longitude are less than zero, I mean when we are at southern hemisphere or west of the prime meridian.
We can't represent a negative zero with Swift, so we lose the negative sign at the following expression:
let latDegrees = latSeconds / 3600
this bug also makes the expresion:
latDegrees >= 0 ? "N" : "S"
always return N (North) and never S (South).
Here is my version of the code (Swift 5 and Xcode 10):
func coordinate() -> (latitude: String, longitude: String) {
// This function converts from DD (decimal degrees) to DMS (degrees, minutes and seconds)
// Calculating the degrees, minutes and seconds for the given latitude value (DD)
var latitudeSeconds = latitude * 3600
let latitudeDegrees = latitudeSeconds / 3600
latitudeSeconds = latitudeSeconds.truncatingRemainder(dividingBy: 3600)
let latitudeMinutes = latitudeSeconds / 60
latitudeSeconds = latitudeSeconds.truncatingRemainder(dividingBy: 60)
// Calculating the degrees, minutes and seconds for the given longitude value (DD)
var longitudeSeconds = longitude * 3600
let longitudeDegrees = longitudeSeconds / 3600
longitudeSeconds = longitudeSeconds.truncatingRemainder(dividingBy: 3600)
let longitudeMinutes = longitudeSeconds / 60
longitudeSeconds = longitudeSeconds.truncatingRemainder(dividingBy: 60)
// Analyzing if it's North or South. (Latitude)
let latitudeCardinalDirection = latitudeDegrees >= 0 ? "N" : "S"
// Analyzing if it's East or West. (Longitude)
let longitudeCardinalDirection = longitudeDegrees >= 0 ? "E" : "W"
// Final strings with format <degrees>°<minutes>'<seconds>"<cardinal direction>
let latitudeDescription = String(format:"%.2f°%.2f'%.2f\"%#",
abs(latitudeDegrees), abs(latitudeMinutes),
abs(latitudeSeconds), latitudeCardinalDirection)
let longitudeDescription = String(format:"%.2f°%.2f'%.2f\"%#",
abs(longitudeDegrees), abs(longitudeMinutes),
abs(longitudeSeconds), longitudeCardinalDirection)
return (latitudeDescription, longitudeDescription)
} // coordinate
I also chose to work with Double in order to handle the precision, for example, I prefer to show two decimal places.
The screen output of this code would be something like:
0.17°10.34'20.53"S 78.48°28.59'35.52"W
Based on David Seek and Josué V. Herrera code
func latlon2DMS(latitude: Double) -> String {
var latitudeSeconds = latitude * 3600
let latitudeDegrees = latitudeSeconds / 3600
latitudeSeconds = latitudeSeconds.truncatingRemainder(dividingBy: 3600)
let latitudeMinutes = latitudeSeconds / 60
latitudeSeconds = latitudeSeconds.truncatingRemainder(dividingBy: 60)
let latitudeCardinalDirection = latitudeDegrees >= 0 ? "N" : "S"
let latitudeDescription = String(format: "%.2f° %.2f' %.2f\" %#",
abs(latitudeDegrees), abs(latitudeMinutes),
abs(latitudeSeconds), latitudeCardinalDirection)
return latitudeDescription
}
func latlon2DMS(longitude: Double) -> String {
var longitudeSeconds = longitude * 3600
let longitudeDegrees = longitudeSeconds / 3600
longitudeSeconds = longitudeSeconds.truncatingRemainder(dividingBy: 3600)
let longitudeMinutes = longitudeSeconds / 60
longitudeSeconds = longitudeSeconds.truncatingRemainder(dividingBy: 60)
let longitudeCardinalDirection = longitudeDegrees >= 0 ? "E" : "W"
let longitudeDescription = String(format: "%.2f° %.2f' %.2f\" %#",
abs(longitudeDegrees), abs(longitudeMinutes),
abs(longitudeSeconds), longitudeCardinalDirection)
return longitudeDescription
}
Call to it with
print(latlon2DMS(latitude: coordLat))
print(latlon2DMS(longitude: coordLong))
latitude.text = latlon2DMS(latitude: coordLat)
longitude.text = latlon2DMS(longitude: coordLong)
I slightly modified Josue V.'s code to work in a timer that updates the location frequently
if( CLLocationManager.authorizationStatus() == .authorizedWhenInUse ||
CLLocationManager.authorizationStatus() == .authorizedAlways){
let currentLocation = locationManager.location
var latitudeSeconds = currentLocation!.coordinate.latitude * 3600
let latitudeDegrees = latitudeSeconds / 3600
latitudeSeconds = latitudeSeconds.truncatingRemainder(dividingBy: 3600)
let latitudeMinutes = latitudeSeconds / 60
latitudeSeconds = latitudeSeconds.truncatingRemainder(dividingBy: 60)
var longitudeSeconds = currentLocation!.coordinate.longitude * 3600
let longitudeDegrees = longitudeSeconds / 3600
longitudeSeconds = longitudeSeconds.truncatingRemainder(dividingBy: 3600)
let longitudeMinutes = longitudeSeconds / 60
longitudeSeconds = longitudeSeconds.truncatingRemainder(dividingBy: 60)
let latitudeCardinalDirection = latitudeDegrees >= 0 ? "N" : "S"
let longitudeCardinalDirection = longitudeDegrees >= 0 ? "E" : "W"
let latitudeDescription = String(format:"%.2f°%.2f'%.2f\"%#",
abs(latitudeDegrees), abs(latitudeMinutes),
abs(latitudeSeconds), latitudeCardinalDirection)
let longitudeDescription = String(format:"%.2f°%.2f'%.2f\"%#",
abs(longitudeDegrees), abs(longitudeMinutes),
abs(longitudeSeconds), longitudeCardinalDirection)
cell5.text = latitudeDescription + " " + longitudeDescription
}
}
and the timer
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(coordinate), userInfo: nil, repeats: true)

How to calculate the time difference between 2 date time values

I am trying to calculate the time difference between 2 date time strings.
I have 2 inputs where the input string is something like this "1:00 PM" and the second one "3:15 PM". I want to know the time difference. So for the above example I want to display 3.15
What I have done:
Converted the time to a 24 hours format. So "1:00 PM" becomes "13:00:00"
Appended the new time to a date like so: new Date("1970-1-1 13:00:00")
Calculated the difference like so:
Code:
var total = Math.round(((new Date("1970-1-1 " + end_time) -
new Date("1970-1-1 " + start_time) ) / 1000 / 3600) , 2 )
But the total is always returning integers and not decimals, so the difference between "1:00 PM" and "3:15 PM" is 2 not 2.15.
I have also tried this (using jQuery, but that is irrelevant):
$('#to_ad,#from_ad').change(function(){
$('#total_ad').val( getDiffTime() );
});
function fixTimeString(time){
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;
return sHours + ':' + sMinutes + ':00';
}
function getDiffTime(){
var start_time = fixTimeString($('#from_ad').val());
var end_time = fixTimeString($('#to_ad').val());
var start = new Date("1970-1-1 " + end_time).getTime(),
end = new Date("1970-1-1 " + start_time).getTime();
return parseInt(((start - end) / 1000 / 3600, 10)*100) / 100;
}
But the total_ad input is displaying only integer values.
How can I fix this problem?
Math.round rounds to the nearest integer, multiply and divide instead
var start = new Date("1970-1-1 " + start_time).getTime(),
end = new Date("1970-1-1 " + end_time).getTime();
var total = (parseInt(((start-end) / 1000 / 3600)*100, 10)) / 100;
FIDDLE
When you take the time 15:15:00 and subtract 13:00:00, you're left with 2.15 hours, not 3.15, and this example would return 2.15 even without making sure there is only two decimals, but for other times that might not be the case.
You could also use toFixed(2), but that would leave you with 3.00 and not 3 etc.
This is how I calculate it:
calculateDiff();
function calculateDiff(){
_start = "7:00 AM";
_end = "1:00 PM";
_start_time = parseAMDate(_start);
_end_time = parseAMDate(_end);
if (_end_time < _start_time){
_end_time = parseAMDate(_end,1);
}
var difference= _end_time - _start_time;
var hours = Math.floor(difference / 36e5),
minutes = Math.floor(difference % 36e5 / 60000);
if (parseInt(hours) >= 0 ){
if (minutes == 0){
minutes = "00";
}
alert(hours+":"+minutes);
}
}
function parseAMDate(input, next_day) {
var dateReg = /(\d{1,2}):(\d{2})\s*(AM|PM)/;
var hour, minute, result = dateReg.exec(input);
if (result) {
hour = +result[1];
minute = +result[2];
if (result[3] === 'PM' && hour !== 12) {
hour += 12;
}
}
if (!next_day) {
return new Date(1970, 01, 01, hour, minute).getTime();
}else{
return new Date(1970, 01, 02, hour, minute).getTime();
}
}

How to convert milliseconds into human readable form?

I need to convert an arbitrary amount of milliseconds into Days, Hours, Minutes Second.
For example: 10 Days, 5 hours, 13 minutes, 1 second.
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.
Let A be the amount of milliseconds. Then you have:
seconds=(A/1000)%60
minutes=(A/(1000*60))%60
hours=(A/(1000*60*60))%24
and so on (% is the modulus operator).
Hope this helps.
Both solutions below use javascript (I had no idea the solution was language agnostic!). Both solutions will need to be extended if capturing durations > 1 month.
Solution 1: Use the Date object
var date = new Date(536643021);
var str = '';
str += date.getUTCDate()-1 + " days, ";
str += date.getUTCHours() + " hours, ";
str += date.getUTCMinutes() + " minutes, ";
str += date.getUTCSeconds() + " seconds, ";
str += date.getUTCMilliseconds() + " millis";
console.log(str);
Gives:
"6 days, 5 hours, 4 minutes, 3 seconds, 21 millis"
Libraries are helpful, but why use a library when you can re-invent the wheel! :)
Solution 2: Write your own parser
var getDuration = function(millis){
var dur = {};
var units = [
{label:"millis", mod:1000},
{label:"seconds", mod:60},
{label:"minutes", mod:60},
{label:"hours", mod:24},
{label:"days", mod:31}
];
// calculate the individual unit values...
units.forEach(function(u){
millis = (millis - (dur[u.label] = (millis % u.mod))) / u.mod;
});
// convert object to a string representation...
var nonZero = function(u){ return dur[u.label]; };
dur.toString = function(){
return units
.reverse()
.filter(nonZero)
.map(function(u){
return dur[u.label] + " " + (dur[u.label]==1?u.label.slice(0,-1):u.label);
})
.join(', ');
};
return dur;
};
Creates a "duration" object, with whatever fields you require.
Formatting a timestamp then becomes simple...
console.log(getDuration(536643021).toString());
Gives:
"6 days, 5 hours, 4 minutes, 3 seconds, 21 millis"
Apache Commons Lang has a DurationFormatUtils that has very helpful methods like formatDurationWords.
You should use the datetime functions of whatever language you're using, but, just for fun here's the code:
int milliseconds = someNumber;
int seconds = milliseconds / 1000;
int minutes = seconds / 60;
seconds %= 60;
int hours = minutes / 60;
minutes %= 60;
int days = hours / 24;
hours %= 24;
This is a method I wrote. It takes an integer milliseconds value and returns a human-readable String:
public String convertMS(int ms) {
int seconds = (int) ((ms / 1000) % 60);
int minutes = (int) (((ms / 1000) / 60) % 60);
int hours = (int) ((((ms / 1000) / 60) / 60) % 24);
String sec, min, hrs;
if(seconds<10) sec="0"+seconds;
else sec= ""+seconds;
if(minutes<10) min="0"+minutes;
else min= ""+minutes;
if(hours<10) hrs="0"+hours;
else hrs= ""+hours;
if(hours == 0) return min+":"+sec;
else return hrs+":"+min+":"+sec;
}
function convertTime(time) {
var millis= time % 1000;
time = parseInt(time/1000);
var seconds = time % 60;
time = parseInt(time/60);
var minutes = time % 60;
time = parseInt(time/60);
var hours = time % 24;
var out = "";
if(hours && hours > 0) out += hours + " " + ((hours == 1)?"hr":"hrs") + " ";
if(minutes && minutes > 0) out += minutes + " " + ((minutes == 1)?"min":"mins") + " ";
if(seconds && seconds > 0) out += seconds + " " + ((seconds == 1)?"sec":"secs") + " ";
if(millis&& millis> 0) out += millis+ " " + ((millis== 1)?"msec":"msecs") + " ";
return out.trim();
}
In java
public static String formatMs(long millis) {
long hours = TimeUnit.MILLISECONDS.toHours(millis);
long mins = TimeUnit.MILLISECONDS.toMinutes(millis);
long secs = TimeUnit.MILLISECONDS.toSeconds(millis);
return String.format("%dh %d min, %d sec",
hours,
mins - TimeUnit.HOURS.toMinutes(hours),
secs - TimeUnit.MINUTES.toSeconds(mins)
);
}
Gives something like this:
12h 1 min, 34 sec
I would suggest using whatever date/time functions/libraries your language/framework of choice provides. Also check out string formatting functions as they often provide easy ways to pass date/timestamps and output a human readable string format.
Your choices are simple:
Write the code to do the conversion (ie, divide by milliSecondsPerDay to get days and use the modulus to divide by milliSecondsPerHour to get hours and use the modulus to divide by milliSecondsPerMinute and divide by 1000 for seconds. milliSecondsPerMinute = 60000, milliSecondsPerHour = 60 * milliSecondsPerMinute, milliSecondsPerDay = 24 * milliSecondsPerHour.
Use an operating routine of some kind. UNIX and Windows both have structures that you can get from a Ticks or seconds type value.
Long serverUptimeSeconds =
(System.currentTimeMillis() - SINCE_TIME_IN_MILLISECONDS) / 1000;
String serverUptimeText =
String.format("%d days %d hours %d minutes %d seconds",
serverUptimeSeconds / 86400,
( serverUptimeSeconds % 86400) / 3600 ,
((serverUptimeSeconds % 86400) % 3600 ) / 60,
((serverUptimeSeconds % 86400) % 3600 ) % 60
);
Why just don't do something like this:
var ms = 86400;
var seconds = ms / 1000; //86.4
var minutes = seconds / 60; //1.4400000000000002
var hours = minutes / 60; //0.024000000000000004
var days = hours / 24; //0.0010000000000000002
And dealing with float precision e.g. Number(minutes.toFixed(5)) //1.44
I'm not able to comment first answer to your question, but there's a small mistake. You should use parseInt or Math.floor to convert floating point numbers to integer, i
var days, hours, minutes, seconds, x;
x = ms / 1000;
seconds = Math.floor(x % 60);
x /= 60;
minutes = Math.floor(x % 60);
x /= 60;
hours = Math.floor(x % 24);
x /= 24;
days = Math.floor(x);
Personally, I use CoffeeScript in my projects and my code looks like that:
getFormattedTime : (ms)->
x = ms / 1000
seconds = Math.floor x % 60
x /= 60
minutes = Math.floor x % 60
x /= 60
hours = Math.floor x % 24
x /= 24
days = Math.floor x
formattedTime = "#{seconds}s"
if minutes then formattedTime = "#{minutes}m " + formattedTime
if hours then formattedTime = "#{hours}h " + formattedTime
formattedTime
This is a solution. Later you can split by ":" and take the values of the array
/**
* Converts milliseconds to human readeable language separated by ":"
* Example: 190980000 --> 2:05:3 --> 2days 5hours 3min
*/
function dhm(t){
var cd = 24 * 60 * 60 * 1000,
ch = 60 * 60 * 1000,
d = Math.floor(t / cd),
h = '0' + Math.floor( (t - d * cd) / ch),
m = '0' + Math.round( (t - d * cd - h * ch) / 60000);
return [d, h.substr(-2), m.substr(-2)].join(':');
}
var delay = 190980000;
var fullTime = dhm(delay);
console.log(fullTime);
Long expireTime = 69l;
Long tempParam = 0l;
Long seconds = math.mod(expireTime, 60);
tempParam = expireTime - seconds;
expireTime = tempParam/60;
Long minutes = math.mod(expireTime, 60);
tempParam = expireTime - minutes;
expireTime = expireTime/60;
Long hours = math.mod(expireTime, 24);
tempParam = expireTime - hours;
expireTime = expireTime/24;
Long days = math.mod(expireTime, 30);
system.debug(days + '.' + hours + ':' + minutes + ':' + seconds);
This should print: 0.0:1:9
Here's my solution using TimeUnit.
UPDATE: I should point out that this is written in groovy, but Java is almost identical.
def remainingStr = ""
/* Days */
int days = MILLISECONDS.toDays(remainingTime) as int
remainingStr += (days == 1) ? '1 Day : ' : "${days} Days : "
remainingTime -= DAYS.toMillis(days)
/* Hours */
int hours = MILLISECONDS.toHours(remainingTime) as int
remainingStr += (hours == 1) ? '1 Hour : ' : "${hours} Hours : "
remainingTime -= HOURS.toMillis(hours)
/* Minutes */
int minutes = MILLISECONDS.toMinutes(remainingTime) as int
remainingStr += (minutes == 1) ? '1 Minute : ' : "${minutes} Minutes : "
remainingTime -= MINUTES.toMillis(minutes)
/* Seconds */
int seconds = MILLISECONDS.toSeconds(remainingTime) as int
remainingStr += (seconds == 1) ? '1 Second' : "${seconds} Seconds"
A flexible way to do it :
(Not made for current date but good enough for durations)
/**
convert duration to a ms/sec/min/hour/day/week array
#param {int} msTime : time in milliseconds
#param {bool} fillEmpty(optional) : fill array values even when they are 0.
#param {string[]} suffixes(optional) : add suffixes to returned values.
values are filled with missings '0'
#return {int[]/string[]} : time values from higher to lower(ms) range.
*/
var msToTimeList=function(msTime,fillEmpty,suffixes){
suffixes=(suffixes instanceof Array)?suffixes:[]; //suffixes is optional
var timeSteps=[1000,60,60,24,7]; // time ranges : ms/sec/min/hour/day/week
timeSteps.push(1000000); //add very big time at the end to stop cutting
var result=[];
for(var i=0;(msTime>0||i<1||fillEmpty)&&i<timeSteps.length;i++){
var timerange = msTime%timeSteps[i];
if(typeof(suffixes[i])=="string"){
timerange+=suffixes[i]; // add suffix (converting )
// and fill zeros :
while( i<timeSteps.length-1 &&
timerange.length<((timeSteps[i]-1)+suffixes[i]).length )
timerange="0"+timerange;
}
result.unshift(timerange); // stack time range from higher to lower
msTime = Math.floor(msTime/timeSteps[i]);
}
return result;
};
NB : you could also set timeSteps as parameter if you want to control the time ranges.
how to use (copy an test):
var elsapsed = Math.floor(Math.random()*3000000000);
console.log( "elsapsed (labels) = "+
msToTimeList(elsapsed,false,["ms","sec","min","h","days","weeks"]).join("/") );
console.log( "half hour : "+msToTimeList(elsapsed,true)[3]<30?"first":"second" );
console.log( "elsapsed (classic) = "+
msToTimeList(elsapsed,false,["","","","","",""]).join(" : ") );
I suggest to use http://www.ocpsoft.org/prettytime/ library..
it's very simple to get time interval in human readable form like
PrettyTime p = new PrettyTime();
System.out.println(p.format(new Date()));
it will print like "moments from now"
other example
PrettyTime p = new PrettyTime());
Date d = new Date(System.currentTimeMillis());
d.setHours(d.getHours() - 1);
String ago = p.format(d);
then string ago = "1 hour ago"
In python 3 you could achieve your goal by using the following snippet:
from datetime import timedelta
ms = 536643021
td = timedelta(milliseconds=ms)
print(str(td))
# --> 6 days, 5:04:03.021000
Timedelta documentation: https://docs.python.org/3/library/datetime.html#datetime.timedelta
Source of the __str__ method of timedelta str: https://github.com/python/cpython/blob/33922cb0aa0c81ebff91ab4e938a58dfec2acf19/Lib/datetime.py#L607
Here is more precise method in JAVA , I have implemented this simple logic , hope this will help you:
public String getDuration(String _currentTimemilliSecond)
{
long _currentTimeMiles = 1;
int x = 0;
int seconds = 0;
int minutes = 0;
int hours = 0;
int days = 0;
int month = 0;
int year = 0;
try
{
_currentTimeMiles = Long.parseLong(_currentTimemilliSecond);
/** x in seconds **/
x = (int) (_currentTimeMiles / 1000) ;
seconds = x ;
if(seconds >59)
{
minutes = seconds/60 ;
if(minutes > 59)
{
hours = minutes/60;
if(hours > 23)
{
days = hours/24 ;
if(days > 30)
{
month = days/30;
if(month > 11)
{
year = month/12;
Log.d("Year", year);
Log.d("Month", month%12);
Log.d("Days", days % 30);
Log.d("hours ", hours % 24);
Log.d("Minutes ", minutes % 60);
Log.d("Seconds ", seconds % 60);
return "Year "+year + " Month "+month%12 +" Days " +days%30 +" hours "+hours%24 +" Minutes "+minutes %60+" Seconds "+seconds%60;
}
else
{
Log.d("Month", month);
Log.d("Days", days % 30);
Log.d("hours ", hours % 24);
Log.d("Minutes ", minutes % 60);
Log.d("Seconds ", seconds % 60);
return "Month "+month +" Days " +days%30 +" hours "+hours%24 +" Minutes "+minutes %60+" Seconds "+seconds%60;
}
}
else
{
Log.d("Days", days );
Log.d("hours ", hours % 24);
Log.d("Minutes ", minutes % 60);
Log.d("Seconds ", seconds % 60);
return "Days " +days +" hours "+hours%24 +" Minutes "+minutes %60+" Seconds "+seconds%60;
}
}
else
{
Log.d("hours ", hours);
Log.d("Minutes ", minutes % 60);
Log.d("Seconds ", seconds % 60);
return "hours "+hours+" Minutes "+minutes %60+" Seconds "+seconds%60;
}
}
else
{
Log.d("Minutes ", minutes);
Log.d("Seconds ", seconds % 60);
return "Minutes "+minutes +" Seconds "+seconds%60;
}
}
else
{
Log.d("Seconds ", x);
return " Seconds "+seconds;
}
}
catch (Exception e)
{
Log.e(getClass().getName().toString(), e.toString());
}
return "";
}
private Class Log
{
public static void d(String tag , int value)
{
System.out.println("##### [ Debug ] ## "+tag +" :: "+value);
}
}
A solution using awk:
$ ms=10000001; awk -v ms=$ms 'BEGIN {x=ms/1000;
s=x%60; x/=60;
m=x%60; x/=60;
h=x%60;
printf("%02d:%02d:%02d.%03d\n", h, m, s, ms%1000)}'
02:46:40.001
This one leaves out 0 values. With tests.
const toTimeString = (value, singularName) =>
`${value} ${singularName}${value !== 1 ? 's' : ''}`;
const readableTime = (ms) => {
const days = Math.floor(ms / (24 * 60 * 60 * 1000));
const daysMs = ms % (24 * 60 * 60 * 1000);
const hours = Math.floor(daysMs / (60 * 60 * 1000));
const hoursMs = ms % (60 * 60 * 1000);
const minutes = Math.floor(hoursMs / (60 * 1000));
const minutesMs = ms % (60 * 1000);
const seconds = Math.round(minutesMs / 1000);
const data = [
[days, 'day'],
[hours, 'hour'],
[minutes, 'minute'],
[seconds, 'second'],
];
return data
.filter(([value]) => value > 0)
.map(([value, name]) => toTimeString(value, name))
.join(', ');
};
// Tests
const hundredDaysTwentyHoursFiftyMinutesThirtySeconds = 8715030000;
const oneDayTwoHoursEightMinutesTwelveSeconds = 94092000;
const twoHoursFiftyMinutes = 10200000;
const oneMinute = 60000;
const fortySeconds = 40000;
const oneSecond = 1000;
const oneDayTwelveSeconds = 86412000;
const test = (result, expected) => {
console.log(expected, '- ' + (result === expected));
};
test(readableTime(
hundredDaysTwentyHoursFiftyMinutesThirtySeconds
), '100 days, 20 hours, 50 minutes, 30 seconds');
test(readableTime(
oneDayTwoHoursEightMinutesTwelveSeconds
), '1 day, 2 hours, 8 minutes, 12 seconds');
test(readableTime(
twoHoursFiftyMinutes
), '2 hours, 50 minutes');
test(readableTime(
oneMinute
), '1 minute');
test(readableTime(
fortySeconds
), '40 seconds');
test(readableTime(
oneSecond
), '1 second');
test(readableTime(
oneDayTwelveSeconds
), '1 day, 12 seconds');