FreeMarker - add days to dates - freemarker

I am trying to add 1 day to the current date. I do know ${.now?date} = The Current Date. But how do I add a day to that? I read that you have to use milliseconds (86,400,000 milliseconds) but I'm unsure what the operation should be.

The detailed answer is here:
<#assign currentDate = .now>
Current Date : ${currentDate?date}<br>
<#assign numberOfDays = 1?long>
<#assign timeInMillisecond = (1000 * 60 * 60 * 24 * numberOfDays) >
<#assign aDate = currentDate?long + timeInMillisecond?long>
<#assign Diff = aDate?long>
<#assign OneDayAfterDate = Diff?number_to_date>
<br>Date after ${numberOfDays} Days : ${OneDayAfterDate}<br>
<br>Date after ${numberOfDays} Days in UTC format : ${OneDayAfterDate?iso_utc}<br>

Figured it out!
${(.now?long + 86400000)?number_to_date }

Related

How to create a proper calculation of a due date in a dynamic manner in laravel

I been analyzing the proper calculation of the duedate for my app. I am working with a lending app where I need to display the due date for the borrower.
let say the approved date of their loan is 2019-10-27 and today is 11-5-2019. the payment scheme is dynamic. depending on what the admin set. in this example, the payment scheme weekly so I just need to say;
$duedate = date('M d Y', strtotime($loan_application->date_approval. ' + '.$loan_application->scheme->num_days.' days') );
from my example above the due date is supposedly on Nov 3, 2019. Now how can I make it say that the next due date is on Nov 10, 2019?
I really tried to analyze, here's my thought.
I get the difference between the approval date and NOW()
$diff = Carbon::createFromTimestamp(strtotime($loan_application->date_approval))->diff(Carbon::now())->days;
now I compared the $diff with $loan_application->scheme->num_days. so let say the $diff=9 and $loan_application->scheme->num_days = 7
now I created the condition
if ($diff > $loan_application->scheme->num_days) {
//should display
Nov 10, 2019
//because the current date is already Nov 5
}
and after Nov 10, + 7 days again, and + 7 days again and so on.
here is the complete code I already have;
$dateApproved = Carbon::parse($loan_application->date_approval)->toFormattedDateString();
$now = Carbon::today('M d Y');
$duedate = date('M d Y', strtotime($loan_application->date_approval. ' + '.$loan_application->scheme->num_days.' days') );
$diff = Carbon::createFromTimestamp(strtotime($loan_application->date_approval))->diff(Carbon::now())->days;
$numOfScheme = $loan_application->loanDuration->num_days / $loan_application->scheme->num_days;
if ($diff > $loan_application->scheme->num_days) {
}
I hope you understand my question. Please help. Thank you so much in advance.
this will add number of days from payment scheme. in my example 7 days. but how can I tell my code that today is already 2 days late, add additional 7 days
It took some time but I understood your question.
$date_approval = Carbon::createFromTimestamp(strtotime($loan_application->date_approval));
$scheme_numdays = $loan_application->scheme->num_days;
$days = (intdiv($date_approval->diff(Carbon::now())->days , $scheme_numdays) + 1) * $scheme_numdays
$due_date = $date_approval->addDays($days)->format('M d Y');
intdiv is just php's integer division.

Ruby: how to convert VARIANT DATE to datetime

In my project i get from external system date&time in VARIANT DATE type and need to convert it to datetime (i.e. 43347.6625 => 04/09/2018 16:29:59).
Do you know how to do it in ruby? what is the best approach? I did not find any ruby built-in method to do such a conversion...
here a method to do the calculation, the date you give is not correct, it should be what this method is returning, check with https://planetcalc.com/7027/
def variant2datetime variant
# number of days after 1-1-1900 minus 2 days for starting with 0
# and having a day that didn't exist because 1900 wasn't a leap year
date = Time.new("1900-01-01") + (variant.to_i - 2) * 24 * 60 * 60
fraction = variant % 1
hours = (fraction - fraction.to_i) * 24
minutes = (hours - hours.to_i) * 60
seconds = (minutes - minutes.to_i) * 60
Time.new(date.year, date.month, date.day, hours.to_i, minutes.to_i, seconds.to_i)
end
variant2datetime 43347.6625 # 2018-09-04 15:53:59 +0200

Ruby - Integer to Time conversion for specific format (y m d)

given
today = 20150307
and
t= Time.at(today).strftime("%Y%m%d")
why this does not return
20150307
but instead
19700822
I ma triyng to check if the difference of thwo date is more than 7 days but those two values are converted into integer in the first place
example
a = 20150227 #(25th February 2015)
x = 20150307 #(7tharch 2015)
if (x-a > 7)
puts "This Item is overdue"
else
puts "All good"
end
my original today is given by this
today = Time.now.strftime("%Y%m%d").to_i
oneweek = (Time.now + (60 * 60 * 24 * 7)).strftime("%Y%m%d").to_i
if i do oneweek - today it will be an integer difference not a date one...
how can i achieve this???
Time.at spect the number of seconds from 1970-01-01 (Epoch).
To do what you want, try something like: t = Date.strptime("20150307", "%Y%m%d")

Local system based timestamp on 00:00:00 of particular day

How do i get time in seconds from epoch at 00:00:00 of a particular day on local machine.I want to trigger a service on 00:00:00 of a particular day and so i wanted to know time in seconds from epoch on local machine when system time reach to 00:00:00.
I have to do it using C language .
Any help will be appreciated .
That's what the mktime function is for. See the documentation here.
struct tm time_str;
time_str.tm_year = 2014 - 1900; /* year minus 1900 */
time_str.tm_mon = 8 - 1; /* month minus 1 */
time_str.tm_mday = 11;
time_str.tm_hour = 0;
time_str.tm_min = 0;
time_str.tm_sec = 0;
time_str.tm_isdst = -1;
time_t seconds_since_epoch = mktime(&time_str);

how to do Date Comparision in javascript?

I need to create the javascript Function based on Date Comparison.which means,To compare the Currentday with Currentday+7.
function a(value)
{
//here how to get current date
//here how to add 7 days with current date
return addedvalue;
}
function b(value)
{
var s=a(value);
//again getting current date here
//check this function current date with s.then if its true means show,
alert('next 7th day is'+s);
}
How to do this?
var now = new Date();// gives you the current date.
var sevenDaysFromNow = new Date();
sevenDaysFromNow.setDate(now.getDate() + 7); // adds seven days.
function compareDates(d, dd) {
return d - dd;
};
Date comparison
If you have two date objects, in this case now and sevenDaysFromNow, subtracting them now - sevenDaysFromNow will return the difference in milliseconds between the two dates. You can convert the difference to a different time unit using simple maths.
var ms = sevenDaysFromNow - now;
var hoursDiff = ms / 1000 / 60 / 60;
var daysDif = ms / 1000 / 60 / 60 / 24;
Working fiddle
This will give you the current date:
new Date()
This will transform it into milliseconds since EPOCH:
(new Date()).valueOf()
This will calculate how much time 7 days are:
7 * 24 * 60 * 60 * 1000
This will transform your integer back to a date:
new Date(any number of millisecond since EPOCH);
To complete the example:
function a() {
return new Date((new Date()).valueOf() + (7 * 60 * 60 * 24 * 1000));
}
Date comparison is done using the DateDiff function.
We dont have any comparison function for Date

Resources