Calculating Previous Month(s) From Current Date

Greeting JavaScripting Gurus!
I would like to ask for some assistance on Date Calculation.
Here is my current scenario:
The user inputs a date mm/dd/yyyy ("DateField")
The powers that be over here are asking for the "DateField" to then calculate these items:
"DateField" minus(-) 12 months = mm/yyyy ("12MonthsAgoDateField")
"DateField" minus(-) 18months = mm/yyyy ("18MonthsAgoDateField")
"DateField" minus(-) 36 months = mm/yyyy ("36MonthsAgoDateField")
and so on...
I've read some of the Date Calculation posts, but could no seem to find anything that fit this scenario.
I freely confess my javascripting ability is very minor.
I would greatly appreciate any help in scripting this.
Thank you!

Unfortunately the numbers of days in a month or year are not the same for all months or years, so the get and set date methods might not work. Fortunately there are the getMonth() and setFullYear() methods for getting or setting the month or year for a given date.
For the "On Blur" action for the "DateField" you can use:
function GetField(cName) {
// get a field object with error catching;
var oField = this.getField(cName);
if(oField ==  null) app.alert("Error accessing field named: " + cName, 0, 0);
return oField;
} // end GetField function;
function AddMonths(oDate, nMonths) {
// add nMonths to oDate object;
oDate.setMonth(oDate.getMonth() + nMonths);
return oDate; // return adjusted date object;
} // end AddMonths;
function Scand(cFormat, cDate) {
var oDate = util.scand(cDateFormat, event.value);
if(oDate == null) app.alert("Error converting " + oDate.valueAsString + " with format: " + cDateFormat, 0, 0);
return oDate;
} // end Scand functon;
var cDateFormat = "mm/dd/yyyy"; // format for date strings;
// event value is the start date string;
// 12 months ago;
var o12MonthsAgo = GetField("12MothsAgoDateField")
var oDate = Scand(cDateFormat, event.value);
oDate = AddMonths(oDate, -12) // subtract 12 months;
o12MonthsAgo.value = util.printd(cDateFormat,oDate);
// 18 months ago;
var o18MonthsAgo = GetField("18MothsAgoDateField")
oDate = Scand(cDateFormat, event.value);
oDate = AddMonths(oDate, -18) // subtract 18 months;
o18MonthsAgo.value = util.printd(cDateFormat, oDate);
// 36 months ago;
var o36MonthsAgo = GetField("36MothsAgoDateField")
oDate = Scand(cDateFormat, event.value);
oDate = AddMonths(oDate, -36); // subtract 36 months;
o36MonthsAgo.value = util.printd(cDateFormat, oDate);
// and so on;
Since getting a field object, converting a date string to a date object and adjusting the date object using the getMonth and setMonth methods are repeated several times I have used functions so the repeated code could be reused.

Similar Messages

  • How to get previous 2 months from current date in a dropdown?

    Hello Experts ,
                           In the application which I am developing, there is a requirement wherein , I want to Populate current month and previous two month in a drop down depending upon today's date. please help.
    Thanks & Regards,
    Pratbha Shukla

    The internal format for dates is yyyymmdd.  Therefore you can just grab the month value from the date:
    data lv_month type FCMNR.
    lv_month = lv_date+4(2).
    Just subtract from the month to get the two previous.
    data lv_previous1 type FCMNR.
    data lv_previous2 type FCMNR.
    if lv_month = 1.
    lv_previous1 = '12'.
    else.
    lv_previous1 - lv_month - 1.
    endif.
    if lv_previous1 = 1.
    lv_previous2 = '12'.
    else.
    lv_previous2 - lv_previous1 - 1.
    endif.
    Then do your lookup for the month name by reading the table returned from function module MONTH_NAMES_GET.

  • Last 6 months of data from current date

    Hi Experts,
    I have a requirement in WebI to display last 6 months of data based on current date.Actually I have a column called "Employee Contract Start date" in my report.Suppose the end user executes the report today,then he should be able to see the Employee's data whose contract started today and in the last 6months from current date.
    Also I have dimension object name "Employee Contract Start Date".
    Please be noted that I am getting the data from Bex Query and I am working on BO 4.1 version.
    I have Objects "Calender day" , "Cal.year/Month" , "Calender month" , "Employee Contract Start Date" in the BEx Query.
    Could anyone please propose me, what are the ways to meet this requirement?

    Hi Cris,
    We can get the 6 Months date in webi.
    Check the below blog , will help you.
    http://scn.sap.com/community/businessobjects-web-intelligence/blog/2014/01/21/time-variablesdimensions
    Regards,
    Javed

  • 2 month old date from current date in red color only

    Hi all,
    How can I Highlight 2 month old date from current date in red color only at report level with using Alertr.
    Example:-
    Date
    3/27/2014
    4/3/2014
    3/5/2014
    4/1/2014
    3/31/2014
    5/24/2013
    2/10/2014
    4/11/2014
    12/11/2013
    9/25/2013
    1/30/2014
    2/18/2014
    2/24/2014
    1/6/2014
    3/3/2014
    Thanks
    Sam

    Hello Sam,
    are you using Universe for your report..if Yes..pls try below option.
    Create two objects in the universe
    Object1 syntax: current date() (Note:this object you can create at webi level)
    Object 2 syntax:addmonths(sysdate;-2)
    Now create a alert in webi report by using below conditions
    Day is less than Object1
    and
    Day is greater than Object2
    Please try this and let me know if any issues.
    Regards,
    Naveen D

  • How to get name of the month from current date.

    Hi,
       How to get the name of the month from current date.
    Thanks,
    Senthil

    Sethil,
    Use your date(let us say Date1) instead of sy-datum.
    CALL FUNCTION 'MONTH_NAMES_GET'
    EXPORTING
    LANGUAGE = SY-LANGU
    IMPORTING
    RETURN_CODE =
    TABLES
    MONTH_NAMES = itab_month
    EXCEPTIONS
    MONTH_NAMES_NOT_FOUND = 1
    OTHERS = 2
    IF SY-SUBRC <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
    WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.
    READ TABLE itab_month
    WITH KEY MNR = date1+4(2).
    itab_month-LTX will contain the value you are looking for
    Good luck
    Raghava

  • Subtracting months from current date

    how to subtract months from current date in mysql

    You are in an Oracle forum, so the Oracle answer is: use add_months(sysdate,-2) to subtract two months from the current date.
    Regards,
    Rob.

  • Get last august month from current date

    Hi,
    I need to get last august month from current date.
    e.g if current date is 1-OCT-2013 need to get last August date i.e.1-AUG-2013
    e.g. if current date is 1-MAY-2013 need to get last August date i.e.1-AUG-2012

    Something like this?
    SQL> WITH table_x AS(
      2     SELECT SYSDATE dt from dual UNION ALL
      3     SELECT TO_DATE('20-05-2013','dd-mm-yyyy') from dual
      4  )
      5  --
      6  ---
      7  --
      8  SELECT  dt,
      9    CASE
    10      WHEN (dt >= Add_Months(TRUNC(dt,'YEAR'),7)) THEN
    11          Add_Months(TRUNC(dt,'YEAR'),7)
    12      WHEN (dt < Add_Months(TRUNC(dt,'YEAR'),7)) THEN
    13          Add_Months(TRUNC(dt,'YEAR'),7) - 365
    14    END cs
    15  FROM table_x;
    DT        CS
    24-OCT-13 01-AUG-13
    20-MAY-13 01-AUG-12

  • SQL Query to add previous month amount to current month amount

    Beginner - Using SQL 2008 R2 - Having a difficult time adding previous month amount to current month amount on a continual running basis. Table fields and example data are:
    FunctionID    
    UnitID     
    Dateof           
    Result       YTD
    A                   AA           01/01/2014    10             10
    A                   AA           02/01/2014    10            
    20
    A                   AA           03/01/2014     15           
    35
    B                  BB            01/01/2014     20          
    20
    B                  BB            02/01/2014     10           30
    The YTD field would be a calculated field. I would need to be able start a new YTD in Jan of the next year. Really could use some help and direction on this one. Easy to do in an Excel spreadsheet. Can't seem to get it in SQL. Thanks in advance.

    As of 2008 R2 the choices are:
    - Self join
    - Correlated subquery (in the SELECT or APPLY clauses)
    - Cursor
    - SQLCLR aggregation function (winner)
    The first two options yield a poor performance for tables with considerable number of rows.
    Example:
    select
        A.*, 
        select
            sum(B.Result)
        from
            T as B
        where
            B.FunctionID = A.FunctionID
            and B.UnitID = A.UnitID
            and B.Dateof >= dateadd(year, datediff(year, '19000101', A.Dateof), '19000101') --bofyear
            and B.Dateof <= A.Dateof
        ) as YTD
    from
        T as A;
    Check these old but very interesting posts from Adam Machanic.
    http://sqlblog.com/blogs/adam_machanic/archive/2006/07/12/running-sums-redux.aspx
    http://sqlblog.com/blogs/adam_machanic/archive/2006/07/12/running-sums-yet-again-sqlclr-saves-the-day.aspx
    The things get easier from SS 2012 with the enhanced OVER clause (introduction of window frame extent).
    select
        sum(Result) over(
        partition by FunctionID, UnitID, year(Dateof) 
        order by Dateof
        rows between unbounded preceding and current row
        ) as YTD
    from
        T;
    http://sqlmag.com/sql-server-2012/sql-server-2012-how-write-t-sql-window-functions-part-3
    AMB
    Some guidelines for posting questions...
    AYÚDANOS A AYUDARTE, guía básica de consejos para formular preguntas

  • Display three prior months starting from current month

    Hello All,
    I came across the requiremnt:
    The report looks like:
    2014-Jan
    2014-Feb
    2014-Mar
    2014-Apr
    2014-May
    2014-June
    2014-July
    2014-Aug
    2014-Sep
    2014-oct
    2014-Nov
    2014-Dec
    A
    122
    233
    233
    233
    233
    233
    233
    233
    233
    233
    233
    233
    B
    22
    33
    33
    33
    33
    33
    33
    33
    33
    33
    33
    33
    C
    33
    33
    33
    33
    33
    33
    33
    33
    33
    33
    33
    33
    D
    333
    3333
    3333
    3333
    3333
    3333
    3333
    3333
    3333
    3333
    3333
    3333
    The requirement is to display the prior 3 months ,current month, next 3 months.
    Assume that current month is SEPTEMBER, the report should look like:
    2014-June
    2014-July
    2014-Aug
    2014-Sep
    2014-oct
    2014-Nov
    2014-Dec
    A
    233
    233
    233
    233
    233
    233
    233
    B
    33
    33
    33
    33
    33
    33
    33
    C
    33
    33
    33
    33
    33
    33
    33
    D
    3333
    3333
    3333
    3333
    3333
    3333
    3333
    Share your thoughts.
    Thanks

    Hello All,
    I got this resolved using the below formula to get either prior 3 months or the next 3 months from current date:
    3rd month from current date:
    =LastDayOfMonth(RelativeDate(LastDayOfMonth(RelativeDate(LastDayOfMonth(RelativeDate(LastDayOfMonth(CurrentDate()); 1));1));1))
    Prior 3rd month from current date:
    =ToDate(FormatDate(RelativeDate(ToDate(FormatDate(RelativeDate(ToDate(FormatDate(RelativeDate(ToDate(FormatDate(CurrentDate();"MM/yyyy");"MM/yyyy");-1);"MM/yyyy");"MM/yyyy");-1);"MM/yyyy");"MM/yyyy");-1);"MM/yyyy");"MM/yyyy")
    and used these objects to restrict my date field(date object between "Prior 3rd month from current date:" and
    "Prior 3rd month from current date:"
    Thanks,
    Vijay

  • Minus age from current date

    Hi experts,
    I have an Age field coming from sender system.
    I need to minus the age from current date and provide the output in form of date towards the target system.
    PLS help me out..

    Hi
    have a look at these
    (How do I calculate the difference between two dates?)
    http://joda-time.sourceforge.net/faq.html
    Calculating the Difference Between Two Datetime Stamps
    http://www.xmission.com/~goodhill/dates/deltaDates.html
    Get difference in days
    http://javaalmanac.com/egs/java.util/CompDates.html
    Thanks
    Gaurav

  • Extract data on report between last 30 days from current date.

    Hi Experts,
    Ealrier i had provided user promt to select the date range, now i need to schedule the report for this i have to set the date between last 30 days from current date.
    How can i add this in formaula on record selection.
    before:
    {pm_process.pm_creation_date} in {@Start Date to UTC} to {@End Date to UTC}
    I tried:
    {pm_process.pm_creation_date} in CurrentDate() - 30 to CurrentDate()
    But this is diplaying me only data of 30th date from current date.
    Please advice.

    Hi Brian,
    Thank you!
    1. I have not created any function for {pm_process.pm_creation_date} in [CurrentDate() - 30 to CurrentDate()] i am just adding this on Record Selection and its not helping.
    2. {pm_process.pm_creation_date} in Last30Days; this is throwing below error.
    please advice what to be done?

  • How to calulate previous fiscal year from current year and periods as input

    hi all,
    i have a report where i have 2 select options as year and period.
    my period can be a range  like( Ex: 1 to 6).
    i like to know previous fiscal year from the following inputs.
    Tel me a suitable conversion routine to calculate
    depending on periods and fiscal year.
    regards
    sivaram.

    Hi
    U need to check the lower period, if it's 1 the previous fyscal year will be the year before:
    IF PERIOD = '1'.
      PREV_YEAR = YEAR - 1.
    ENDIF.
    U can also use the fm FI_PERIOD_DETERMINE, in this case you need a date, u can get it from lower current period:
    Get the first day of the period
    BUDAT(4)   = YEAR.
    BUDAT+4(2) = PERIDO_LOW.
    BUDAT+6(2) = '01'.
    Now get the last day of the previous month:
    BUDAT = BUDAT - 1.
    Use this date to get its period by fm FI_PERIOD_DETERMINE
    Max

  • To extend SAP user date and to add months to the current date

    Hi All,
    I need to extent the user date in SAP based on one condition.
    If the condition is true extend the date by 12 monthd if false 6 months from the current date.
    Can i have the FM for user date extentions and FM for getting the date.
    Thanks,
    Chandu.

    Hi ,
    You can use this FM for exteding the date.
    CALL FUNCTION 'BAPI_USER_CHANGE'
        EXPORTING
          username   = us_username
          logondata  = e_bapilogond
          logondatax = e_bapilogondx
        TABLES
          return     = i_return.
    u need to pass date and correspoing flag also.
    For extension of the date by months u can use this FM.
          CALL FUNCTION 'MONTH_PLUS_DETERMINE'
            EXPORTING
              months  = 6 or 12 ( depending up on ur logic)
              olddate = sy-datum
            IMPORTING
              newdate = us_expdate.
    Thanks and regards.
    Sham

  • I want to add 2 months to the current date

    actally i want to add 2 montsh fro teh current date.but i have to consider the month of february for the calculation and even the leap year.
    so can u guys help me out .
    waiting for the reply
    thanks in advance.

    Hi Rajeev ,
    Welcome to SDN.
    Check this and execute the code.
    This will cover the leap year also.
    data: date like sy-datum,
          date1 like sy-datum.
    date = sy-datum.
    CALL FUNCTION 'RP_CALC_DATE_IN_INTERVAL'
      EXPORTING
        date            = date
        days            = 00      
        months          = 02     "no of months
       SIGNUM           = '+'  "months added
        years           = 00
    IMPORTING
       CALC_DATE       = date1. " new date
    write:/ 'new date',  date1.
    regards.
    vijay

  • Sum from current date to the last date of sales

    Hi 
    It's been a while since I used MDX last time. 
    I have a simple question (probably). I need a measure which sums up from the current date to the last sale date. 
    It's like a reversal of Year To date.  And I need to do it date level. 
    Let's say is the sales goes like
    1/May/2000     $1000
    2/May/2000     $1000
    3/May/2000     $1000
    4/May/2000     $1000
    5/May/2000     $1000
    I need to create a measure which displays sum of current sales and last sales
    1/May/2000     $5000  
    2/May/2000     $4000
    3/May/2000     $3000
    4/May/2000     $2000
    5/May/2000     $1000
    I have a date hierarchy and hope it can be rolled up. 
    And I tried like something like ( suggested in another posting)
    [Date].[Calendar].CurrentMember:NULL}
    But this does not work for me.
    The actual MDx I wrote is 
    With member MEASURES.ActiveLicences as 
    sum ( 
    {[License Expiry Date].[FinancialYear].[YY-MMM].CurrentMember: null},
    [Measures].[No of Students]
    select {
    MEASURES.ActiveLicences,
    [Measures].[No of Students] } on columns, 
    [License Expiry Date].[FinancialYear].[YY-MMM].members on rows 
    from [Sales];
    Can anyone help me with this please? 
    Kind regards
    Mark Kim

    Hi SQLMa ,
    I was trying a similar code, using a hierarchy expression instead of a member expression for the CurrentMember functionality . Here is an Adventure Works example :
    With member MEASURES.ActiveLicences as 
    sum ( {[Delivery Date].[Fiscal].CurrentMember: null}
    , [Measures].[Internet Sales Amount]) 
    select {MEASURES.ActiveLicences,[Measures].[Internet Sales Amount]} on columns
    , [Delivery Date].[Fiscal].[Month].members on rows 
    from [Adventure Works]
    Hope it helps :)
    Regards, David .

Maybe you are looking for