Calculate end date based on # of days and start date - variables

I am working on script where users can make certain type of orders. Now when users make an order they can choose how long they wont it to last in # of days. Once the order is placed I need to approve their order and that approval date is recorded inside my database. Now what I need is to show inside their user panel how much days their package will last since the day of my approval. So for example if I approved their order September 08, 2013 and they choosed for the order to last 7 days, I wont them to see inside they panel for every next day they login how much days they have left, so 7days, 6days, 5days, etc... all the way to "0 Days" when they come to their panel on September 16, 2013.
I have following variables for those two values:
$row_ordersResults['date'] - the date I approved the order
$row_ordersResults['drip_feed'] - # of days they wont for their order to last
I did tried to lots of combinations by myself but I am totally stuck with this and cant make it work.
Thanks for help!

The libraries at momentjs.com is pretty cool. But if you just wanted something simple to calculate the "date difference" between two time values, there is a relatively simple way to do it. I assume you wanted it done in Javascript.
All you need to do is to clear out the hour/minute/second/millisecond fields of the two dates, calculate their differences in days. Put the script below in any web browser and you'll see how it works.
<script>
function foo() {
var d1 = new Date(2013, 8, 12, 13, 40, 1, 333); // any date value, last 4 params can be anything
var d2 = new Date(2013, 9, 3, 11, 42, 32, 533);
d1.setHours(0); d1.setMinutes(0); d1.setSeconds(0); d1.setMilliseconds(0);
d2.setHours(0); d2.setMinutes(0); d2.setSeconds(0); d2.setMilliseconds(0);
daysLeft = (d2.getTime() - d1.getTime())/(24*60*60*1000);
alert('Dear customer, there is(are) ' + daysLeft + ' day(s) left on your order!' );
}
</script>
Show Remaining Days on Order
EDIT: adding PHP version
<?php
$d1 = New DateTime('2013-08-28 06:25:00');
$d2 = new DateTime(); // now
$drip = 55;
$interval = $d2->diff($d1); // time difference
$days_left = $drip - $interval->format('%a'); // in days, subtract from drip
echo "There are $days_left days left\n";
?>

I hope I don't get marked down for not suggesting a specific answer, but time and date calculations are very tedious and JavaScript's Date() provides limited options. So rather than offer some ugly code, I suggest you take a look at moment.js at momentjs.com. Once you attach the script to your pages, you can easily manage all kind of date formats, and set up a function that will allow you to do math on dates and automatically generate your date ranges - it will even let you format them in to user friendly formats like "in 3 days", which I think is what you want. If your app has anything to do with time, and most do, I can't recommend Moment highly enough.

Related

Report builder (SSRS) SWITCH statement blank values

For a report I'm building, there are fields that are dependent on a user selecting to add additional people to their policy (named in the dataset as dependent1, dependent2, etc).
We need to determine whether these dependants are adults or children for reporting. This will be done via DOB and a DateDiff which I understand
Currently, I can get the report to show ADULT or CHILD if data is present, however if there isn't any data I can't return a "" value, it just defaults to ADULT.
I'm using:
=Switch(DateDiff("yyyy", Fields!Dependant1DoB.Value, Fields!Policy_Start_Date.Value) < 18, "CHILD",
DateDiff("yyyy", Fields!Dependant1DoB.Value, Fields!Policy_Start_Date.Value) >= 18, "ADULT",
DateDiff("yyyy", Fields!Dependant1DoB.Value, Fields!Policy_Start_Date.Value), "")
I also created another column which uses the DateDiff between Fields!Dependant1DoB.Value & Fields!Policy_Start_Date.Value to show the number of years. This works and returns a number. however for some unknown reason where there's no data it still returns "2019" which may be effecting it? I've tried including this in the Switch statement (DateDiff("yyyy",Fields!Dependant1DoB.Value,Fields!Policy_Start_Date.Value)=2019, "")) but this still doesn't work.
Someone please help as this is driving me loopy!
You need to first check for nothing and then apply the switch function, example below:
=IIF(IsNothing(Fields!Dependent1DoB.Value),"",
Switch(
DateDiff("yyyy",Fields!Dependent1DoB.Value,Fields!Policy_Start_Date.Value)<18, "CHILD",
DateDiff("yyyy",Fields!Dependent1DoB.Value,Fields!Policy_Start_Date.Value)>=18, "ADULT"
)
)

How to format Date received from Element-UI

I'm making use of the datepicker from Element Ui where the user can select a range in dates. When the user fills in the datepicker, this is what I get back: ['2018-01', '2019-02'] . So just an array with two String elements. NOT a Date() object.
When dynamically outputting this to the user, I would like to show it as: January 2018 - February 2019
I got it somewhat working in my project but the code just really sucks and also doesn't work properly either. Checkout this gif of my current project and somewhat desired result. I was wondering if someone knows a good and easy way of achieving my desired result without too much hassle. I cannot use methods like toLocaleDateString() because it isn't a Date() object. I've got my code working in a Codesandbox. If any if you guys knows a solution feel free to edit the listed Codesandbox, I would highly highly appreciate it!
In the Codesandbox I got a computed property formateDate. The basic idea of the computed property is to cut the elements of the array I get back from the datepicker before and after the - effectively giving me the first & second month and first & second year. That data I store in these variables:
let monthOne;
let monthTwo;
let yearOne;
let yearTwo;
Since I get the months back as '01' and '02' for example, I created an array of objects to transform the '01' and '02' to January and February with a for loop. I then store the result in a new variable with:
displayDate = monthOne + ' ' + yearOne + ' - ' + monthTwo + ' ' + yearTwo;
Lastly, I tried to store that result into the timeperiodDisplayString at the correct index in the experience array of objects. Anyway, I'm probably overcomplicating this way to hard so I would REALLY appreciate any help with this.
Maybe you could map over the result from the datepicker component to create an array of Date objects. So you can use toLocaleDateString()
Something like this should work:
['2018-01', '2019-02'].map((date) => {
const split = date.split('-');
return new Date(split[0], +split[1] - 1);
});
You can use parse() function from javascript.
var d = Date.parse("2019-01");
console.log(new Date(d));

How to handle to a certain Date scenario in Selenium script

i have a scenario where on opening the calendar three dates are enabled for the current week lets' say '21,22,23' and then the next day '21' gets disabled and the next dates shown are 22 ,23 ,24 so i would like to handle this in a way so that it would pick the next date automatically the next day. I don"t want to hard code it as the script will fail next day. How should i handle this?Any suggestions. Thanks
Sounds like you need to know the current date, and then the date of tomorrow.
This has been addressed many many times already on Stack Overflow, so search for more details. Also, this seems to be completely unrelated to Selenium, so please edit your Question and its title to correct and clarify.
ZoneId z = ZoneId.of( "Pacific/Auckland" );
LocalDate today = LocalDate.now( z );
LocalDate tomorrow = today.plusDays( 1 );

Grouping Elements on Conditions in Penthao Report Designer

I want to make a Report in Penthao Report Designer
My SQL query tells me the SUM of some variables and another variable is already the same SUM.
So for Example
DATE Name Expected SUM
Today A 20 20
Today B 10 5
Inside the Report Designer i select a Text field which is OK if Expected-SUM=0 else Error.
Inside my Report i know want to display if for "Today" all those Textfields are OK (than one row should display OK) or if at least one of them is Error than only Display one Row with Error for a given Date
I'm thankfull for every suggestion.
As here are no suggestions so far, maybe this is one solution: I would do this with an JavaScript-Step, but as a tranformation with a field and a filter-step:
Javascript:
var testing = "";
if(expected - sum == 0){
testing = "ok";
}
else {
testing = date;
}
And after that a filter-step, which sends rows to an "error"-output-report (with given date): filter is "testing NOT ok".
Sorry if thats not the solution for your problem, I have no experiences with Reports ... But maybe it's a hint and you asked for "every suggestion" :)
Hope it works!

Rails show different object every day

I want to match my user to a different user in his/her community every day. Currently, I use code like this:
#matched_user = User.near(#user).order("RANDOM()").first
But I want to have a different #matched_user on a daily basis. I haven't been able to find anything in Stack or in the APIs that has given me insight on how to do it. I feel it should be simpler than having to resort to a rake task with cron. (I'm on postgres.)
Whenever I find myself hankering for shared 'memory' or transient state, I think to myself "this is what (distributed) caches were invented for".
#matched_user = Rails.cache.fetch(#user.cache_key + '/daily_match', expires_in: 1.day) {
User.near(#user).order("RANDOM()").first
}
NOTE: While specifying a TTL for cache entry tells Rails/the cache system to try and keep that value for the given timeframe, there's NO guarantee that it will. In particular, a cache that aggressively tries to reclaim memory may expire an entry well before its desired expires_in time.
For this particular use case, it shouldn't be a big deal but in cases where the business/domain logic demands periodically generated values that are durable then you really have to factor that into your database.
How about using PostgreSQL's SETSEED function? I used the date to seed so that every day the seed will change, but within a day, the seed will be consistent.:
User.connection.execute "SELECT SETSEED(#{Date.today.strftime("%y%d%m").to_i/1000000.0})"
#matched_user = User.near(#user).order("RANDOM()").first
You may want to seed a random value after using this so that any future calls to random aren't biased:
random = User.connection.execute("SELECT RANDOM()").to_a.first["random"]
# Same code as above:
User.connection.execute "SELECT SETSEED(#{Date.today.strftime("%y%d%m").to_i/1000000.0})"
#matched_user = User.near(#user).order("RANDOM()").first
# Use random value before seed to make new seed:
User.connection.execute "SELECT SETSEED(#{random})"
I have split these steps in different sections just for readability. you can optimise query later.
1) Find all user records till today morning. so that the count will freeze.
usrs_till_today_morning = User.where("created_at <?", DateTime.now.in_time_zone(Time.zone).beginning_of_day)
2) Pluck all ID's
user_ids = usr_till_today_morning.pluck(:id)
3) Today date it will be a range (1..30) but will remain constant throughout the day.
day_today = Time.now.day
4) Select the same ID for the day
todays_user_id = user_ids[day_today % user_ids.count]
#matched_user = User.find(todays_user_id)
So it will give you random user records by maintaining same record throughout the day!!