I need to assign a variable with the value in the format YYYYMM e.g for today run the previous month values should be generated as
Var1 = 201607
Is there any in build method available? Could you share the steps to generate this?
The trick in these cases is to subtract one month from the day 15 of the current month:
$ date --date="$(date +%Y-%m-15) - 1 month"
Fri Jul 15 00:00:00 CEST 2016
Then it is just a matter of using the proper format:
$ date --date="$(date +%Y-%m-15) - 1 month" "+%Y%m"
201607
To store the value in a var, just use the common var=$(command) syntax.
From GNU Coreutils → 28.7 Relative items in date strings:
The fuzz in units can cause problems with relative items. For example,
‘2003-07-31 -1 month’ might evaluate to 2003-07-01, because 2003-06-31
is an invalid date. To determine the previous month more reliably, you
can ask for the month before the 15th of the current month. For
example:
$ date -R
Thu, 31 Jul 2003 13:02:39 -0700
$ date --date='-1 month' +'Last month was %B?'
Last month was July?
$ date --date="$(date +%Y-%m-15) -1 month" +'Last month was %B!'
Last month was June!
Also, take care when manipulating dates around clock changes such as
daylight saving leaps. In a few cases these have added or subtracted
as much as 24 hours from the clock, so it is often wise to adopt
universal time by setting the TZ environment variable to ‘UTC0’ before
embarking on calendrical calculations.
Related
I'm a bit rubbish on Google Sheets formulas... would anyone be so kind to tell me how to achieve this ?
I'd like to have a cell that returns the last Friday's date on Sat to Wed, and instead the current date for Thursdays and Fridays.
Is it possible ?
e.g. this coming days:
Sat to Wed returns Friday 12th of March
Thu 18th returns Thu 18th
Fri 19th return Fri 19th
... and so on.
Thanks!
There are many ways to accomplish this, but try this:
=IF((WEEKDAY(TODAY())=5)+(WEEKDAY(TODAY())=6),TODAY(),TODAY()-VLOOKUP(WEEKDAY(TODAY()),{7,1; 1,2; 2,3; 3,4; 4,5},2,FALSE))
This formula is based on the us default numbers for weekdays, where Sunday = 1. If this formula produces unexpected results, your locale may be one where Monday = 1. In this case, you'll need to adjust as follows:
=IF((WEEKDAY(TODAY())=4)+(WEEKDAY(TODAY())=5),TODAY(),TODAY()-VLOOKUP(WEEKDAY(TODAY()),{6,1; 7,2; 1,3; 2,4; 3,5},2,FALSE))
Other things to keep in mind:
The + in (WEEKDAY(TODAY())=5)+(WEEKDAY(TODAY())=6) means OR (where * would mean AND).
The VLOOKUP is looking up the weekday of TODAY() within a simple virtual array, which is formed between the curly brackets and which instructs how many days to subtract from TODAY() given the current weekday in order to arrive at the previous Friday.
I want to get two fields: Begin date and End date of last month. For example, 14-04-2020 should give me the Begin date as 01-03-2020 and End_date as 31-03-2020. I have read the Nifi Expression language docs but all it can do with a date format is add or subtract in milliseconds. This is not helpful for my use case as the number of days in a month is not fixed and conversion to milliseconds won't help.
Is there a way to achieve my use case somehow using Nifi Expression language?
#AdarshKumar
NiFI Expression Language for this Use Case would be very clunky and unreliable for different timezones, months with <> 30 days, and leap years.
Please reference this post below which goes into detail for how to get "last month":
How to insert previous month of data into database Nifi?
In the Case of NiFi you kind of have to play with the dates to get the outcome
if you have the dates you easily convert a date and then just hard set a day to get the 1st day of the month with a hard coded day
${now():toNumber():format('yyyy-MM-01')}
to get the last day of the month you can either use the script or play with the calc using epoch time.
so to get the last day of the previous month you can just use the date and convert the day into epoch time and subtract it from the epoch date to get last day of previous month
example
${now():toNumber():format('yyyy-MM-dd'):toDate('yyyy-MM-dd', 'GMT'):toNumber():minus(${now():toNumber():format('dd'):toNumber():multiply(86400):multiply(1000)}):format('yyyy-MM-dd')}
in this example above we convert the date to epoch format it to convert again, conversion happens to remove default timestamp and then we use the same formula to get just the day as a number to multiply it with 86400 seconds in a day and multiply that by 1000 for the epoch number to subtract from the date which is then formatted back into a date.
Raw Date: Thursday, August 26, 2021 11:20:31 AM
formatted: Thursday, August 26, 2021 12:00:00 AM
epoch of formatted date: 1629936000000
Subtract Epoch: 2246400000 (86400 seconds * 26 days * 1000)
result: 2021-07-31
alteratively you could first add a month and the work back to get the current day of the given month
this example is just to give you an idea of ways you can use built in date functions with epoch time to calculate the correct date, removing the issues with months that end on specific numbers.
I try UpdateAttribute to minus month
test${now():toNumber():format('yyyyMM'):minus(1)}01
I am trying to get week numbers ( resetting at 1 for each month) as per ISO format for each month in 2019.For example I am interested in getting
All dates in July 2019: week 1 to 4,
All dates in Aug 2019 : week 1 to 4 and so on.
I first created the calculated field (Week_Number_ISO) to get the overall week number in year 2019.I used the following formula;
DATEPART('iso-week',[ Date]) which works as intended.
To get the monthly week number I used the following formula
INT((DATEPART('day',[Created Date])-DATEPART('iso-weekday',[Created Date])+7)/7)+1.
(Idea was to calculate the date of the first day of each week & then divide by 7 and take the integer part)
As per the ISO format, shouldn't July 29 to 31st be a part of week 4 for July?But the formula is showing it as week 5 for July 2019.I feel I am missing something in the formula or am missing something about ISO week number resetting at 1 for each month.
Can someone help me?
Here is an example of the dates in July 2019 and the associated week numbers.
Why would July 28th-July 31st 2019 be considered week 4?
I want to get the week number using bash.
The man entry for date shows the following:
%V ISO week number, with Monday as first day of week (01..53)
%W week number of year, with Monday as first day of week (00..53)
Which is the difference?
This is the output I get:
$ date "+%W"
48
$ date "+%V"
48
It's a bit more clear in the GNU docs (referenced in the man page):
https://www.gnu.org/software/coreutils/manual/html_node/Date-conversion-specifiers.html#Date-conversion-specifiers
‘%W’ week number of year, with Monday as first day of week (‘00’…‘53’). Days in a new year preceding the first Monday are in week zero.
‘%V’ ISO week number, that is, the week number of year, with Monday as the first day of the week (‘01’…‘53’). If the week containing January 1 has four or more days in the new year, then it is considered week 1; otherwise, it is week 53 of the previous year, and the next week is week 1. (See the ISO 8601 standard.)
Seems to depend on the year on this. I like JoseKilo's explanation.
$ date +%V
32
$ date +%W
31
$ date
Mon Aug 5 08:29:23 MDT 2019
$
$ date -v-2y +%W
31
$ date -v-2y +%V
31
$ date -v-2y
Sat Aug 5 08:29:22 MDT 2017
$
I would say there is a typo there:
$ date "+%W"
52
$ date "+%V"
52
$ date
Sat Dec 30 10:42:06 CET 2017
Best regards
My typical requirement is that I want to subtract two Dates cast as timestamp. The Minuend (First parameter) is the current date and the Subtrahend(second parameter) is stored separately as DD, MM and YYYY in three columns. The final output should be a discrete year as number. I am playing with something like :
SELECT (TO_DATE('05-DEC-2013') -
CASE LENGTH(CAND_DOB_DD)
WHEN 1 THEN
CAST(TO_DATE('0'||CAND_DOB_DD||'-'||CAND_DOB_MM||'- '||CAND_DOB_YYYY,'DD-MM-YY HH24:MI:SS')
AS TIMESTAMP)
ELSE
CAST(TO_DATE(CAND_DOB_DD||'-'||CAND_DOB_MM||'-'||CAND_DOB_YYYY,'DD-MM-YY HH24:MI:SS')
AS TIMESTAMP) / 365 END YEAR
FROM CANDIDATE
The Year part as Integer will be used for a very sensitive calculation. Please suggest if the above piece of SCRIPT will yield the desired result. Thanks in advance.
For most purposes you could just use the Months_Between() function to determine the number of months between two dates, and then divide by 12 etc.. Note that the number of months is an integer when comparing two dates that have the same day of the month or are both the last day of the month.
This is tricky when it comes to leap years.
Do you count 28th Feb 2015 to 28th Feb 2016 as exactly one year, when 28th Feb 2015 to 29th Feb 2016 is one day longer but plainly is a year?
What about 29th Feb 2016 to 28th Feb 2017, or 28th Feb 2016 to 28th Feb 2017?
Think carefully about these boundary cases, but Months_Between() is likely to be your best choice.