How to get date range between two date in Google AppMaker? - date

I have two datebox that capture a start date & end date. I tried do following binding to get the date range between the two date but it return a negative value
#widget.root.children.DateBox1.value - #widget.root.children.DateBox2.value
following is my form example

// Binding (option 1 - no datasource)
getValidity(#widget.root.children.StartDate.value, #widget.root.children.EndDate.value);
// Binding (option 2 - with datasource)
getValidity(#datasource.item.StartDate, #datasource.item.EndDate);
// Client script
function getValidity(start, end) {
if (start && end) {
var milliseconds = end - start;
// days: 1000ms * 60s * 60m * 24h
return milliseconds / (1000 * 60 * 60 * 24);
// years: 1000ms * 60s * 60m * 24h * 365d
// return milliseconds / (1000 * 60 * 60 * 24 * 365);
}
return 'n/a';
}

Related

Convert text column into time format column and calculate the sum

I have a one time column in format 00:01:30 (HH:MM:SS). This column is a text format column.
this text column format convert to a time format column and create a new measure for the total time sum.
You can easily convert your Text column into Time using
Time = TIMEVALUE('Table'[Text])
But the problem is that the Time format doesn't support more than 24 hours, so your SUM will potentially lead to an overflow. Here's a workaround:
Create a calculated "Seconds" Column
Seconds =
VAR Time =
TIMEVALUE('Table'[Text])
RETURN
HOUR(Time) * 3600 + MINUTE(Time) * 60 + SECOND(Time)
Aggregate the Seconds with this Measure and convert back to a "Duration-like" format:
Total Duration =
VAR total_seconds =
SUM('Table'[Seconds])
VAR days =
QUOTIENT(total_seconds, 24 * 60 *60)
VAR rest1 =
MOD(total_seconds, 24 * 60 * 60)
VAR hours =
QUOTIENT(rest1, 60 * 60)
VAR rest2 =
MOD(total_seconds, 60 * 60)
VAR minutes =
QUOTIENT(rest2, 60)
VAR seconds =
MOD(rest2, 60)
RETURN
days & "." & FORMAT(hours, "0#") & ":" & FORMAT(minutes, "0#") & ":" & FORMAT(seconds, "0#")

Firestore security rules for birthdate

I would like to have a Firestore security rule for bithdate field in my users collections where user age should be >=18 and <=80. I tried the following rule but I know it is not going to work especially for ages that are close to 18 or 80. Any idea how to make this work.
let now = request.time;
let thisYear = now.year();
let thisMonth = now.month();
let thisDay = now.day();
request.resource.data.birthdate >= timestamp.date(thisYear-80,thisMonth,thisDay) &&
request.resource.data.birthdate <= timestamp.date(thisYear-18,thisMonth,thisDay)
You can achieve the same result by comparing it with the request time but in seconds. here’s one example on how this will work:
function isAbove18(date) {
return date.seconds <= request.time.seconds - 18 * 365.25 * 24 * 60 * 60; //considering leap year with 0.25 🙃
}
function isBelow80(date) {
return date.seconds >= request.time.seconds - 80 * 365.25 * 24 * 60 * 60;
}
allow read, write: if isAbove18(resource.data.birthdate) && isBelow80(resource.data.birthdate);
For more about this you can go through this docs which takes some different approaches.

Dart | Convert seconds to DateTime without calculate

Sometimes we need day, hour, minute, second to countdown, but what we know just second.
For example:
1s -> 0day 0hour 0minute 1s
10s -> 0day 0hour 0minute 10s
100s -> 0day 0hour 1minute 40s
Here is my solution:
void _test() {
var diffseconds = 100;
var seconds = diffseconds % 60;
var minute = diffseconds ~/ 60 % 60;
var hour = diffseconds ~/ 60 ~/ 60 % 24;
var day = diffseconds ~/ 60 ~/ 60 ~/ 24;
print('day=$day');
print('hour=$hour');
print('minute=$minute');
print('second=$seconds');
}
If I can change second to DateTime, I can get day easily with date.day and so on and I don't need to calculate anymore.
So I wonder is there any api or simple way to achieve it without any calculate.
I wonder better solution.
I don't think this functionality is built into Dart but you can somewhat easy add the following extension to Duration which are more or less what you are already are doing:
extension RestTimeOnDuration on Duration {
int get inDaysRest => inDays;
int get inHoursRest => inHours - (inDays * 24);
int get inMinutesRest => inMinutes - (inHours * 60);
int get inSecondsRest => inSeconds - (inMinutes * 60);
int get inMillisecondsRest => inMilliseconds - (inSeconds * 1000);
int get inMicrosecondsRest => inMicroseconds - (inMilliseconds * 1000);
}
void main() {
const duration = Duration(seconds: 123);
print('Days: ${duration.inDaysRest}'); // 0
print('Hours: ${duration.inHoursRest}'); // 0
print('Minutes: ${duration.inMinutesRest}'); // 2
print('Seconds: ${duration.inSecondsRest}'); // 3
print('Milliseconds: ${duration.inMillisecondsRest}'); // 0
print('Microseconds: ${duration.inMicrosecondsRest}'); // 0
}
Just use Duration(seconds: seconds), it handles values more than 60.
Duration(seconds: 123) // 2 minutes and 3 seconds

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.

Why Cookies will not be stored if setting Timeout = 30 days in GWT?

This is very weird. Ok, the below code works fine
public void setCookie(String cookiesName, String cookiesValue){
final int COOKIE_TIMEOUT = 1000 * 60 * 60 * 24;//1 days
Date expires = new Date((new Date()).getTime() + COOKIE_TIMEOUT);
Cookies.setCookie(cookiesName, cookiesValue, expires);
}
//then
setCookie("currentLang","de");
Collection<String> cookies = Cookies.getCookieNames();
for(String cookie : cookies){
if("currentLang".equals(cookie)){
System.out.println("got currentlang");
}
}
If i run the above code then I can see output: "got currentlang"
However, if I set timeout=30 days final int COOKIE_TIMEOUT = 1000 * 60 * 60 * 24 * 30;//30 days, then nothing got printed out, so "currentLang" has not even been stored if we set 30 days?
Why is that? does Gwt prevent that to happen?
You're using integer arithmetic for a sum that overflows beyond the maximum positive value for an integer. So 1000*60*60*24*30 is negative.
You could try 1000L*60*60*24*30