Page 1 of 1

Date Conversion?

Posted: Thu May 15, 2014 12:45 am
by A_SUSHMA
Hi All,

I have one requirement.

Requirement: Take the current date and check the date if day is >20 then take the month as current month Else take the previous month with day as 01 always.

Ex: 21-02-2014 .here day is greater then 20 output= 01-02-2014
02-03-2014 here day is less than 20 take the previous month date : 01-02-2014


Could you please help me the above?

Posted: Thu May 15, 2014 1:12 am
by ray.wurlod
Use functions MonthDayFromDate(), MonthFromDate() and YearFromDate() to extract the components, do your math, then use DateFromComponents() function to re-assemble the date.

Posted: Thu May 15, 2014 7:46 am
by A_SUSHMA
Thanks Ray.... working fine

Posted: Thu May 15, 2014 8:02 am
by qt_ky
MonthDayFromDate() would not be necessary, as the resulting day of the month is always day 1.

Make sure to test dates ranges including January 1st through 20th, to ensure it falls back to the previous year.

:idea: You may be able to avoid the "check the date if day is > 20" and similar "If Then Else" logic by doing the date math up front. Would this work?

Stage variable:

Code: Select all

svTempDate = DateOffsetByDays(incoming_date, -20)
Derivation:

Code: Select all

DateFromComponents(YearFromDate(svTempDate), MonthFromDate(svTempDate), 1)