I want to convert seconds to days, hours and minutes
Currently, it works just for hours and minutes but not for days. Can you please support me tell me what I did wrong:
<cfscript>
seconds = '87400';
midnight = CreateTime(0,0,0);
time = DateAdd("s", seconds, variables.midnight);
date= xxxxxxxxxxxxxxxxxxxxx???
</cfscript>
<cfoutput>
#DateFormat(variables.date, 'd')# not working
#TimeFormat(variables.time, 'HH:mm')#
</cfoutput>
For the value 87400 the expected result is
1 Days, 0 hours, 16 minutes
If I take 94152 seconds it will be:
1 days, 3 hours, 22 minutes
The only issue i have is to get the correct days ... hours and minutes are diplayed but not the correct days
thank you for all the support
A simple way to calculate the intervals is by taking advantage of the modulus operator:
totalSeconds = 94152;
days = int(totalSeconds / 86400);
hours = totalSeconds / 3600 % 24;
minutes = totalSeconds / 60 % 60;
seconds = totalSeconds % 60;
For 94152 seconds, the results would be:
Interval
Value
DAYS
1
HOURS
2
MINUTES
9
SECONDS
12
TOTALSECONDS
94152
demo trycf.com
I understand from your question that you don't need to get a certain date and time along a timeline, but convert a total amount of seconds in days, hours and minutes. To do that you don't necessary need to use cfml time and date functions like CreateTime() or DateAdd(). You just may need these in order to get a reference point of time or date along a timeline, which doesn't seem to be the case, otherwise you would know the value of your starting date variable. Thus, you can solve this with plain rule of three. There may be simpler methods, so I'm posting an alternative only.
We know that:
60 seconds is equivalent to 1 minute
60 minutes is equivalent to 1 hour
24 hours is equivalent to 1 day
Thus, your calcualtion within cfml could be like so:
<cfscript>
//Constants for calculation
secondsPerDay= 60*60*24;
secondsPerHour= 60*60;
secondsPerMinute= 60;
//Seconds to convert
secondsTotal=87400;
// temp variable
secondsRemain= secondsTotal;
days= int( secondsRemain / secondsPerDay);
secondsRemain= secondsRemain - days * secondsPerDay;
hours= int( secondsRemain / secondsPerHour);
secondsRemain= secondsRemain - hours * secondsPerHour;
minutes= int( secondsRemain / secondsPerMinute);
secondsRemain= secondsRemain - minutes * secondsPerMinute;
writeoutput( "#secondsTotal# seconds are: #days# days, #hours# hours, #minutes# minutes and #secondsRemain# seconds." );
</cfscript>
That outputs:
87400 seconds are: 1 days, 0 hours, 16 minutes and 40 seconds.
Related
I have an application that stores date+time as int in a database.
I would like to get back from the int to the real date&time!
I have these 2 examples:
919326588 ---> 25.03.2022 09:46
919322562 ---> 23.03.2022 14:43
I don't get it, even though I tried with julian days, unix-epoch, seconds from 1.1.1970, ....
Is there anybody out there who can help?
What I got so far is that the beginning is something like:
( 919362588 / 2000 ) + 2000000 --> 2459663.2939999998
The int part is the julian day 2022-03-24. The fraction part should be the fraction of a day in hours, minutes, ...
But it must be more than 0.5 to get over the 00:00 to 2022-03-25, but is only .2939999998 which is less than 0.5 .
assuming that the people dealing with these dates do not count from 12 - 12 but instead from 0 - 0 we have to add 0.5 to get from 12 - 0 midnight.
So this would mean:
0.2939999998 + 0+5 = 0.7939999998
But is 2022-03-25 07:03:21 not 09:46 :-(
I need to sum the hours and minutes so I am doing this like ill convert hours in second and minutes in second then sum it
var totalServiceSeconds = minsSeconds + hoursSeconds;
var c = Duration(seconds: totalServiceSeconds);
print('c ${c.toString()}');
it's showing c 25:05:00.000000 which is correct
Know I need to show this as hours and minutes in the text widget. So I am converting to DateTime like this
var format = DateFormat("HH:mm");
DateTime totalServiceTime = format.parse(c.toString());
But it's printing it like this totalServiceTime 1970-01-02 01:05:00.000
This issue is only when the hours are 24 or more. If my hours are 24 then it's showing 0 and if greater than 24 then it's showing 1 2 so on. I know it because it's considering 24 as 0 but what can I do about this?
I want to show 24 if it's 24 hours or if greater than 24 like 26 need to show 26.
You do not want to convert it into a DateFormat because time steps of 24 hours is how they count a full day. Instead you should format var c as shown below
var totalServiceSeconds = minsSeconds + hoursSeconds;
var c = Duration(seconds: totalServiceSeconds);
print('c ${c.toString()}');
String FormatDuration = "${c.inHours}:${c.inMinutes.remainder(60)}:${(c.inSeconds.remainder(60))}";
print(FormatDuration);
String FormatDuration2 = "${c.inHours} hours ${c.inMinutes.remainder(60)} minutes ${(c.inSeconds.remainder(60))} seconds";
print(FormatDuration2);
The output will then be
c 25:05:00.000000 <-------previous
25:5:0 <-------new option 1
25 hours 5 minutes 0 seconds <-------new option 2
Example:
Time = 09:41:46
Duration = 0:00:17 (IE 17 seconds)
Start Time = Time - Duration
Clearly I can't just break this up into hours minutes and seconds and do a basic minus operation given the 60 minute hour and 60 second minute etc.
Can't seem to get my head around how to calculate this and hoping someone has come across this before :).
You can use the DateAdd function.
For example, this will subtract 17 seconds from the specified date/time.
DateAdd("s", -17, "1/1/2013 09:41:46")
Try this
Time = 09:41:46
Duration = 0:00:17
Start_Time = FormatDateTime(Time - Duration, 3)
wscript.echo hour(Start_Time)
wscript.echo minute(Start_Time)
wscript.echo second(Start_Time)
References:
https://www.w3schools.com/asp/asp_ref_vbscript_functions.asp#date
I want to use a countdown timer in my app.
I have this date
03-09-2011T20:54:18Z
Now I want to use countdown timer for this date. Can you please suggest if there is any method regarding this. I f I am not clear at any point please let me know. Thanks in advance.
Turn that timestamp into an NSDate* (see here)
Then
NSTimeInterval interval = [timestamp timeIntervalSinceNow];
will always give you the number of seconds between now and the date you care about. Then you can do something like:
#define SECONDS_IN_MINUTE 60
#define SECONDS_IN_HOUR (SECONDS_IN_MINUTE * 60)
#define SECONDS_IN_DAY (SECONDS_IN_HOUR * 24)
NSInteger days, hours, minutes, seconds;
days = interval / SECONDS_IN_DAY;
interval %= SECONDS_IN_DAY;
hours = interval / SECONDS_IN_HOUR;
interval %= SECONDS_IN_HOUR;
minutes = interval / SECONDS_IN_MINUTE;
seconds %= SECONDS_IN_MINUTE;
If you want it to update "live" every second then use performSelector:withObject:afterDelay: with a delay of whatever period you want to update the display (or just 0.0 to do it every run loop)
I have this
NSDate *date1Val = date1.date;
NSDate *date2Val = date2.date;
NSTimeInterval interval = [date2Val timeIntervalSinceDate:date1Val];
int hours = (int)interval / 3600; // integer division to get the hours part
int minutes = (interval - (hours*3600)) / 60; // interval minus hours part (in seconds) divided by 60 yields minutes
NSString *timeDiff = [NSString stringWithFormat:#"%d:%d", hours, minutes];
but it doesnt show me correct date - for 24 hours it says 23:57 no idea why
When selecting a date using a UIDatePicker in date only mode the time that was set in date + time mode is also present in the supplied date. You can go into IB, switch the UIDatePickers to Date & Time mode and make sure that the hour and minute are the same.