Time comparison in html using NGIF Condition - Angular - time

I am in requirement for solution where I have one Admin posts application for an Android app. I have placed a delete button for post. The requirement is that delete button must be shown for 5 minutes from time of posting.
Here is the ngif condition which I have used..
*ngIf="((((post.CreatedDate | date:'dd/MM/yyyy') == (PresentDate | date:'dd/MM/yyyy')) && ((post.CreatedDate | date:'HH') == (post.CreatedDate | date:'HH')))&&((post.CreatedDate | date:'mm')< (time)))"
Code in TS page for present time + 5 minutes
const d: Date = new Date();
this.PresentDate = d;
var x = new Date();
d.getHours(); // => 9
d.getMinutes(); // => 30
this.time = d.getMinutes() +5;
this.hours = d.getHours();
Please help with the solution

Long expression in html is not good practice.
*ngIf="canDeletePost(post)"
canDeletePost(post) {
return Date.now() - post.CreatedDate.getTime() < 5 * 60 * 1000;
}
If CreatedDate is Js date. 5 * 60 * 1000 - 5 min in milliseconds. Actually, have a method in ngIf is not good practice also.
Anyway, you don't need date pipe. Pipes is used for changing view.

Related

WooCommerce Bookings AJAX codes for selection of dates

My current project need to customisely showing some extra information base on calculations of the dates that has been selected. I have writen a function, and I can't find the AJAX code that the booking system returns the data and calculations base on the valid date to trigger my function.
My function is easy:
function calculate_date_duration() {
var year_1 = document.querySelector('.selection-start-date').getAttribute('data-year');
var month_1 = document.querySelector('.selection-start-date').getAttribute('data-month');
var date_1 = document.querySelector('.selection-start-date a').textContent;
var year_2 = document.querySelector('.selection-end-date').getAttribute('data-year');
var month_2 = document.querySelector('.selection-end-date').getAttribute('data-month');
var date_2 = document.querySelector('.selection-end-date a').textContent;
var day_1 = new Date(year_1, month_1, date_1);
var day_2 = new Date(year_2, month_2, date_2);
console.log(day_1);
console.log(day_2);
var day_selected = day_2.getTime() - day_1.getTime();
var date_duration = parseInt(day_selected / (1000 * 60 * 60 * 24)) +1;
console.log(date_duration);
var display_pane = document.querySelector('.wc-bookings-booking-cost');
display_pane.innerHTML = display_pane.innerHTML + '<br>Total booking cost:<strong><span class="woocommerce-Price-amount amount"><bdi><span class="woocommerce-Price-currencySymbol">$</span>' + (date_duration*120) + '</bdi></span></strong>';}
The date select duration ajax is in wp-content/plugins/woocommerce-bookings/dist/frontend.js, search success, the last one in this file is the codes after validating the selected duration.

getting hours and day difference between two dates in codeigniter

i am getting two dates as
$date1 = 2020-07-16 03:50:32
$date2 = 2017-01-25 09:43:53
i want to get the difference between thes two dates.
THe difference count hours until 24 hours and then days plus hours.
eg. 2 days and 5 hours.
THe code i tried is this
$createddate = date("d-m-Y H:i:s", strtotime($application['created_at']));
$approvedisapprovedate = date("d-m-Y H:i:s", strtotime($application['approved_at']));
$created = strtotime($createddate);
$approvedisapprove = strtotime($approvedisapprovedate);
$diff = $approvedisapprove - $created;
$days = floor($diff / (60 * 60 * 24));
$hours = round(($diff - $days * 60 * 60 * 24) / (60 * 60));
but it won't work.anybody suggest a solution.
You can use carbon for this. Carbon can make it simple to manipulate dates very efficiently not for only this task but many other related date.
Install carbon
{
"require": {
"nesbot/carbon": "^1.22"
}
}
Usage
$date->diffForHumans();
OR
$date->diffInDays();
In your case, you can try below code -
$createddate = Carbon::parse($application['created_at']);
$approvedisapprovedate = Carbon::parse($application['approved_at']);
//here is carbon magic
$createddate->diffInDays($approvedisapprovedate); //This will give you diff in number of days.
$createddate->diffForHumans($approvedisapprovedate); //This will give you diff which is readable by human. ex- 1 day 2 hours
You can refer Carbon.
Hope this will help for you.
I propose you the DateInterval::format function of php
like this:
$createddate = new DateTime('12-07-2020 12:10:01');
$approvedisapprovedate = new DateTime('16-07-2020 13:15:40');
$interval = $approvedisapprovedate->diff($createddate);
echo $interval->format('%a days %h hours %i minutes')."\n";
It goes back to this:
4 days 1 hours 5 minutes
I don't know the format of the date $application['created_at'] and $application['approved_at'] but I just put it there:
$createddate = new DateTime($application['created_at']);
$approvedisapprovedate = new DateTime($application['approved_at']);
$interval = $approvedisapprovedate->diff($createddate);
echo $interval->format('%a days %h hours %i minutes')."\n";
voici le lien de DateInterval::format :
https://www.php.net/manual/fr/dateinterval.format.php

Observable Interval without drift

I am using Observable.interval to schedule code execuiton at specified times:
let obs = Observable.interval(50).subscribe(()=>{
console.log(this.currentFrame+" "+new Date().getTime());
this.currentFrame++
});
This is the output. As you can see, after 6 iterations I already have a 10ms drift. How can I use Observable.interval, but also specify that it needs to recalculate next iteration based on the current drift?
0 1513972329849
1 1513972329901
2 1513972329952
3 1513972330004
4 1513972330057
5 1513972330110
Until #cartant's fix gets repulled, you could use expand and create the behavior yourself. Assuming delay will always drift forward, try the following:
function correcting_interval(interval) {
const start_time = new Date().getTime();
return Observable.of(-1)
.expand(v => Observable.of(v + 1).delay(interval - (new Date().getTime() - start_time) % interval))
.skip(1);
}

How to perform complex bulk update in RethinkDB

I am trying to periodically calculate complex top score for all items in post table.
const {log10, max, abs, round} = Math;
const topScore = post => { // from Reddit
const {score, createdAt} = post;
const order = log10(max(abs(score), 1));
const sign = score > 0 ? 1 : (score < 0 ? -1 : 0);
const seconds = Date.now() - createdAt;
return sign * order + seconds / 45000;
};
With the above function, I want to perform something like this:
// Update topScore every 60 seconds.
setInterval(() =>
r.table('post').update(post => post.topScore = topScore(post)).run();
, 60000);
How do I do this with RethinkDB javascript driver?
You can write r.table('post').update(r.js('(function(post) { ... })'), {nonAtomic: true}) where ... is arbitrary JS code. Otherwise you'd either have to translate that code into ReQL or pull down the documents to your client, update them, and then write them back to the server.

Xpages view string type ratio

I have a view with 2 columns, both type of String. One is always "08:00" and one depends on the user input. It could be 02:30 or 13:45, etc...
I want to convert all the column of 08:00's and all the other column of values, add them together and then divide the total...
Example:
Column 1 | Column 2
02:50 | 08:00
05:15 | 08:00
15:25 | 08:00
03:15 | 08:00
It would be something like:(~26)/(8*4)=~0.8%.
It's a timekeeping application. I have the normal-time of 8 hours and the time the employee worked, and I want to add a button that will calculate the ratio between the 2 columns.
Thank you,
Florin
This is the code I used to calculate the ration between 2 columns of strings:
var vec:NotesViewEntryCollection = viewSearch.getAllEntries();
var c = vec.getCount()
var data = #DbColumn(#DbName(),"vwSearch",4);
var totalH = 0;
var totalM = 0;
for(var i=0;i<c;i++){
var str = data[i].toString();
var a = str.split(":",2)[0];
var b = str.split(":",2)[1];
var hour = parseInt(a);
var minutes = parseInt(b);
totalH += hour;
totalM += minutes;
if(totalM >= 60)
{
totalH++;
totalM-=60;
}
}
var h_necesare = c*8;
getComponent("h_lucrate").setValue(totalH+" ore, si "+totalM+" minute.");
getComponent("h_necesare").setValue(h_necesare);
getComponent("raport").setValue(totalH-h_necesare);

Resources