I need to get the difference between two dates and I want it displayed in number of months(MS WORD), I am using this formula at the moment:
{QUOTE
{SET sy{ MERGEFIELD CASS_CALCULATED_LAST_PMT_DATE \# yyyy}}
{SET sm{ MERGEFIELD CASS_CALCULATED_LAST_PMT_DATE \# M}}
{SET sd{ MERGEFIELD CASS_CALCULATED_LAST_PMT_DATE \# d}}
{SET ey{ MERGEFIELD LETTER_DATE \# yyyy}}
{SET em{ MERGEFIELD LETTER_DATE \# M}}
{SET ed{ MERGEFIELD LETTER_DATE \# d}}
{SET md{=IF((em=2),28+(em=2)*((MOD(ey,4)=0)+(MOD(ey,400)=0)-(MOD(ey,100)=0)),31-((em=4)+(em=6)+(em=9)+(em=11)))}}
{Set Years{=ey-sy-(em<sm)-(em=sm)*(ed<sd)}}
{Set Months{=MOD(12+em-sm-(ed<sd),12) \# 0}}
{Set Days{=MOD(md+ed-sd,md) \# 0}}
"{Years} Year{IF{Years}= 1 "" s}, {Months} Month{IF{Months}= 1 "" s}, {Days} Day{IF{Days}= 1 "" s}."}
Right now im trying to calculate the difference between 01.10.2020 and 02.02.2021. the result given is:0 Years, 0 Months, 2 Days.
could you please let me know how can I adjust it so that it works and that it would display the difference only in months, leaving out years and days.
Thank you!
Delete:
{SET md{=IF((em=2),28+(em=2)*((MOD(ey,4)=0)+(MOD(ey,400)=0)-(MOD(ey,100)=0)),31-((em=4)+(em=6)+(em=9)+(em=11)))}}
Replace:
{Set Months{=MOD(12+em-sm-(ed<sd),12) \# 0}}
{Set Days{=MOD(md+ed-sd,md) \# 0}}
"{Years} Year{IF{Years}= 1 "" s}, {Months} Month{IF{Months}= 1 "" s}, {Days} Day{IF{Days}= 1 "" s}."}
with:
{Set Months{={=MOD(12+em-sm-(ed<sd),12)+Years*12} \# 0}}
"{Months} Month{IF{Months}= 1 "" s}"}
#macropod, please see my outcome here:
126 – Difference in days between CASS_CALCULATED_LAST_PMT_DATE and LETTER_DATE
0 - Difference in months between CASS_CALCULATED_LAST_PMT_DATE and LETTER_DATE (orriginal formula)
0 Months (new formula as per your suggestion)
2020 - CASS_CALCULATED_LAST_PMT_DATE # yyyy
10 - CASS_CALCULATED_LAST_PMT_DATE # M
1 - CASS_CALCULATED_LAST_PMT_DATE # d
2021 - LETTER_DATE # yyyy
2 - LETTER_DATE # M
4 - LETTER_DATE # d
Related
I have these to simple test files:
This is fil1, containing records I want to select some from
01
02
07
05
10
20
30
25
This is keepNR, containing the record numbers I want to extract from fil1
1
4
7
What I want are these records from fil1
01 (observation/record # 1)
05 (observation/record # 4)
30 (observation/record # 7)
I am a novice to AWK, but I have tried these programs:
using this, I can see that the observations are there
awk 'FNR==NR {a[$1]; next } { for (elem in a) { print "elem=",elem,"FNR=",FNR,"$1=",$1 }} ' keepNR fil1
I had hoped this would work, but I get more than the 2 records:
awk 'FNR==NR {a[$1]; next } { for (elem in a) { if (FNR ~ a[elem]) print elem,FNR,$1; next }} END{ for (elem in a) { print "END:", elem }}' keepNR fil1
1 1 01
1 2 02
1 3 07
1 4 05
1 5 10
1 6 20
1 7 30
1 8 25
I first tried using the == instead of the ~, but then no result ??
as you can see here:
gg#gg:~/bin/awktest$ awk 'FNR==NR {a[$1]; next } { for (elem in a) { if (FNR == a[elem]) print elem,FNR,$1; next }} ' keepNR fil1
gg#gg:~/bin/awktest$
I have also tried (FNR - a[elem])==0 with no output
So I have 2 questions
why does if (FNR ~ a[elem]) work, but if (FNR == a[elem]) does not ?
why do I get 8 lines of output, instead of 2 ?
If you have a better solution, I would love to see it
Kind Regards :)
You don't assign to a[$1] so its value is empty: FNR==NR { a[$1]; next }
for (elem in a) sets elem to the keys of a.
if (FNR == a[elem]) compares against the value in a[elem]. The value is empty, so there is no match.
if (FNR ~ a[elem]) tests if FNR matches the empty regex (//), so it always matches.
A simpler method is to test if FNR is a key of a:
awk '
FNR==NR { a[$1]; next }
FNR in a { print "FNR="FNR, "$1="$1 }
' keepNR fil1
which should output:
FNR=1 $1=01
FNR=4 $1=05
FNR=7 $1=30
I need to calculate the date between two dates, and tell me how many day are in between, if more than 30 days then I will target something.
in this script D is the date (in the past) that I want to calculate from today
set X to MYdatefromSafari -- "August 26th, 2016"
set D to ConvertDate(X)
log D
on ConvertDate(X) -- sub routine to convert string "english_month dayth/st, year" to real date
set MS to {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
set LW to every word of X
if (count of LW) is not 3 then return "" -- invalid format
set MI to 0 -- check month : should be in the list
repeat with I from 1 to 12
if item I of MS is item 1 of LW then set MI to I
end repeat
if MI is 0 then return "" -- the fisrt word is not in the list of months
try -- check day : it should be NNth of NNst
set DI to (text 1 thru -3 of item 2 of LW) as integer
end try
if not ((DI > 0) and (DI < 31)) then return "" -- invalid day
try -- check year
set YI to (item 3 of LW) as integer
end try
if not ((YI > 0) and (YI < 9999)) then return "" -- invalid year
return date ((DI & "/" & MI & "/" & YI) as string)
end ConvertDate
In the best scenario, that would calculate the number of date in between if less than a year, and month or year if more
EDIT :
set X to "August 26th, 2016"
set MyDate to ConvertDate(X)
set D to ConvertDate(X)
log D
set SecondDate to (current date) -- = system date
set ListDiff to DateDiff(D, CD) -- returns {diff days, diff months, diff years}
log "Days = " & item 1 of ListDiff
log "Months = " & item 2 of ListDiff
log "Years = " & item 3 of ListDiff
on DateDiff(D1, D2) -- return list with difference in days, in months, in years
-- depending if differences is less than month, or less than year or higher than a year
if D1 > D2 then -- set DStart as oldest date
copy {D1, D2} to {Dend, DStart}
else
copy {D1, D2} to {DStart, Dend}
end if
return {(Dend - DStart) div days, (Dend - DStart) div (30 * days), (Dend - DStart) div (365 * days)}
end DateDiff
on ConvertDate(X) -- sub routine to convert string "english_month dayth/st, year" to real date
set MS to {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
set LW to every word of X
if (count of LW) is not 3 then return "" -- invalid format
set MI to 0 -- check month : should be in the list
repeat with I from 1 to 12
if item I of MS is item 1 of LW then set MI to I
end repeat
if MI is 0 then return "" -- the fisrt word is not in the list of months
try -- check day : it should be NNth of NNst
set DI to (text 1 thru -3 of item 2 of LW) as integer
end try
if not ((DI > 0) and (DI < 31)) then return "" -- invalid day
try -- check year
set YI to (item 3 of LW) as integer
end try
if not ((YI > 0) and (YI < 9999)) then return "" -- invalid year
return date ((DI & "/" & MI & "/" & YI) as string)
end ConvertDate
Sub-routine "DateDiff" bellow gives back a list of 3 difference values : in days, months and years.
Set X to MyDatefrom Safari
Set MyDate to ConvertDate(X)
set SecondDate to (current date) -- = system date
set ListDiff to DateDiff(D, CD) -- returns {diff days, diff months, diff years}
log "Days = " & item 1 of ListDiff
log "Months = " & item 2 of ListDiff
log "Years = " & item 3 of ListDiff
on DateDiff(D1, D2) -- return list with difference in days, in months, in years
-- depending if differences is less than month, or less than year or higher than a year
if D1 > D2 then -- set DStart as oldest date
copy {D1, D2} to {Dend, DStart}
else
copy {D1, D2} to {DStart, Dend}
end if
return {(Dend - DStart) div days, (Dend - DStart) div (30 * days), (Dend - DStart) div (365 * days)}
end DateDiff
on ConvertDate(X) -- copy your existing sub-routine
end ConvertDate
For instance if MyDate = Jan 20th 2016 and we are August 26th 2016, it will return {219, 7, 0} because différence is 216 days or 7 months (Jan to August) or 0 year (2016 both dates !).
I have following list of splitters:
val splitd = list(" or ", " and ", " up to ")
and the following string:
val st = "You should eat 2 kg apples a week or 2 bananas everyday; up to a month you should eat 5g of ginger everyday"
I want following output:
val entry = List("You should eat 2 kg apples a week", "2 bananas everyday;", "a month you should eat 5g of ginger everyday")
If there is no entry in "splitd" matching the content in "st" then full string "st" should be returned. Thanks in advance for your help.
Dear #Shadowlands and #marstran, need your help again.
Check this out:
splitd.foldLeft(List(st)) {
case (acc, spl) => acc.flatMap(item => item.split(spl).toList)
}
I'm trying to get a person age in Years,Months,Days,Hours,Minutes and Seconds.
This is what I got so far (you can tell I'm a total powershell beginner):
$now = [datetime]::now
[datetime]$birthday = "12/22/2012 03:22:00"
$age = [datetime]$now - $birthday
Write-Host "My daughter's age is:" $age.Days "days" ($age.Hours) "hours" ($age.Minutes) "minutes" ($age.Milliseconds) "seconds"
The output is:
My daughter's age is: 59 days 10 hours 27 minutes 76 seconds
It's ok, but it would be more awesome have the output more 'human readable':
1 Month 19 Days 10 hours 28 minutes 16 seconds
This is actually way more difficult than I initially expected. The code must know how many days has each month in order to split days into months and days. And also consider that this year is leap year!
Maybe PowerShell can help here with some of its magic!
Thank you!
I believe this is what you're after.
[datetime]$birthday = "12/22/2012 03:22:00"
$span = [datetime]::Now - $birthday
$age = New-Object DateTime -ArgumentList $Span.Ticks
Write-Host "My daughter's age is:" $($age.Year -1) Years $($age.Month -1) Months $age.Day "days" ($age.Hour) "hours" ($age.Minute) "minutes" ($age.second) "seconds"
Check it with the day after your birthday. There is 1 day to much!
Correct are:
[datetime]$birthday = "12/22/2012 03:22:00"
$span = [datetime]::Now - $birthday
$age = New-Object DateTime -ArgumentList $Span.Ticks
Write-Host "My daughter's age is:" $($age.Year-1) Years $($age.Month-1) Months ($age.Day-1) "days" ($age.Hour) "hours" ($age.Minute) "minutes" ($age.second) "seconds"
how can I format the day from a date field to print like 1st 2nd 3rd and so on? Thanks
I found a solution for this.
NumberVar DayIn := Day (PrintDate);
Totext (DayIn , 0 )
& (if DayIn in 4 to 20 then 'th' else
if remainder (DayIn , 10) = 1 then 'st' else
if remainder (DayIn , 10) = 2 then 'nd' else
if remainder (DayIn , 10) = 3 then 'rd' else 'th')
from http://www.kenhamady.com/news0910.shtml