Flutter hours and minute count issue - flutter

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

Related

Can't figure out date time conversion from int to datetime

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 :-(

Coldfusion calculate seconds in days, hours, min

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.

Swift Number Of Weeks in Month

I'm using Swift 5. I'm trying to get the number of weeks in a month. Jan 2021 prints 6 weeks, but May prints 5 weeks. I'm thinking the problem is firstWeekday is set to 1. Is there a way to get the number of weeks in a given month without setting firstWeekday?
var localCalendar = Calendar.current
localCalendar.firstWeekday = 1
let weekRange = localCalendar.range(of: .weekOfMonth, in: .month, for: dateFormatter.date(from: monthName)!)
if let weekRange = weekRange {
print("\(monthName) has \(weekRange.count) weeks.")
}
It seems you do not want the number of weeks in a month at all. You want the number of rows in a calendar printout.
The formula for the number of rows needed to represent a month on a standard Gregorian-style calendar is as follows.
Start with the number of days in the month. Add to that the number of blank days before the start of the month, beginning with Sunday. For example, January 2021 is 31 + 5 = 36, because it's 31 days long but 5 days (Sunday thru Thursday) are not part of it before the start. To put it another way: the first day of January 2021 is a Friday; that is day 5 of the week if we call Sunday day 0, so we get 31 + 5.
Now integer-divide by 7. We need at least that number of rows. So (31+5)/7 using integer division is 5. The question is: is that all the rows we need?
To find out, get the remainder of that division. If it is not zero, add another row. So (31+5)%7 is 1, which tells us that one more day needs to be accommodated so we need another row. That makes 6.
Thus:
// `startingOn` pretends that Sunday is day 0
func rowsNeededForMonthWith(numberOfDays n: Int, startingOn i: Int) -> Int {
let (quot,rem) = (n+i).quotientAndRemainder(dividingBy: 7)
return quot + (rem == 0 ? 0 : 1)
}
Here are some quick sanity tests:
// January 2021
rowsNeededForMonthWith(numberOfDays: 31, startingOn: 5) // 6
// But suppose it had 30 days?
rowsNeededForMonthWith(numberOfDays: 30, startingOn: 5) // 5
// Or suppose it had started on Thursday? (cf July 2021)
rowsNeededForMonthWith(numberOfDays: 31, startingOn: 4) // 5

Formatting milliseconds into hh:mm:ss - extraneous hours added

I'm using momentJS to find the difference between two times and format to hh:mm:ss. Ver: "moment": "~2.10.6"
The problem I'm having is an extraneous 7 hours being added to a time whose difference is only 1 minute, 1 second.
var diff_ms = moment('2016-07-29 10:35:18').diff('2016-07-29 10:34:17');
61000
var diff = moment(diff_ms).format('hh mm ss')
"07 01 01"
Convert milliseconds to hours and minutes using Momentjs says to specify the time unit as milliseconds, moment.duration(x, 'milliseconds');, but since it's returning the correct minutes and seconds (01 01), it seems it's correctly defaulting to milliseconds.
I can do this the "un-momentJS" way:
var a = moment.duration(61000, 'milliseconds');
a: n {_milliseconds: 61000, _days: 0, _months: 0, _data: Object,
_locale: l}
var b = a.hours() + ':' + a.minutes() + ':' + a.seconds();
b: 0:1:1
But don't have an easy way of formatting this output to hh:mm:ss using string concatenation like this.

NSDate timeIntervalSinceDate calculates incorrectly by 60 minutes

Trying to figure out why the following code results in 90,000 which is exactly 1 hour over a day. The created dates are both set to noon of consecutive days so the answer should be 86,400 (24*60*60).
let currentCalendar = NSCalendar.currentCalendar();
let components1 = NSDateComponents()
components1.year = 2015
components1.month = 11
components1.day = 1
components1.hour = 12
components1.minute = 0
components1.second = 0
let myDate1 = currentCalendar.dateFromComponents(components1)!
let components2 = NSDateComponents()
components2.year = 2015
components2.month = 10
components2.day = 31
components2.hour = 12
components2.minute = 0
components2.second = 0
let myDate2 = currentCalendar.dateFromComponents(components2)!
let difference = myDate1.timeIntervalSinceDate(myDate2)
Running swift 2 (Xcode 7.0)
Daylight savings time ends on Nov 1, 2015. The 25-hour difference is correct. There will also be a 23-hour day in the spring when DST begins.
This is why, if you want to be sure to get the same clock time on another date, you can’t just add or subtract multiples of 24 hours.
This only goes for places that observe DST, which is probably why some commenters on the original question are seeing a 24-hour difference in their local time zone.