This topic contains 9 replies, has 0 voices, and was last updated by CREECE 7 years, 7 months ago.

  • Author
    Posts
  • #1140

    ssilveri77

    There doesn’t appear to an addmonth in native javascript. Has anyone found an equivalent in 2.0 ?
    This is a cached copy. Click here to see the original post.

  • #1141

    karenn

    Yeah I was having the same issue. Using the following

    var today = new Date();

    var sixmonthsago = new Date();

    sixmonthsago.setMonth(sixmonthsago.getMonth() – 6);

  • #1142

    ssilveri77

    Originally posted by karenn

    View Post

    Yeah I was having the same issue. Using the following

    var today = new Date();

    var sixmonthsago = new Date();

    sixmonthsago.setMonth(sixmonthsago.getMonth() – 6);

    Yes. I found set month only sets the month number. It does not increment/decrement the year. Many seemed to be using Javascript libraries with addmonth functions

  • #1143

    cblackburn

    We recommend that people have a look at MomentJS for date arithmetic.

    http://momentjs.com/

  • #1144

    ssilveri77

    Originally posted by cblackburn

    View Post

    We recommend that people have a look at MomentJS for date arithmetic.

    http://momentjs.com/

    Interesting. I will try it. Thanks

  • #1145

    ssilveri77

    Originally posted by cblackburn

    View Post

    We recommend that people have a look at MomentJS for date arithmetic.

    http://momentjs.com/

    Do I need to update the moment.js with define and return statements or can I use it as is…

  • #1146

    erictgrubaugh

    moment.js is AMD-compatible as-is. I am currently using it in some 2.0 scripts just fine. Uploaded the .min version to the File Cabinet, and included it in my 2.0 portlets with no issue or modifications needed.

  • #1147

    david.smith

    I like momentsjs but I’m not a big fan of loading unnecessary code. Here are a couple functions that you might be able to use.

    Code:
    function addMonths(date,months){
    var d= new Date(date.getFullYear(), date.getMonth()+months, date.getDate());
    return d;
    }

    function daysInMonth(date) {
    return new Date(date.getYear(), date.getMonth()+1, 0).getDate();
    }


    ironside replied on 02/23/2017, 10:40 PM: Not to be contrarian, I disagree with david here.

    I’m not a fan of _writing_ unnecessary code. My recommendation is to use momentjs instead of writing buggy code ones self.

  • #1148

    cblackburn

    Originally posted by ssilveri77

    View Post

    Do I need to update the moment.js with define and return statements or can I use it as is…

    It should work as-is, MomentJS will detect the presence of an AMD module loader and register with it if present.

  • #1149

    CREECE

    +1 for MomentJS. If you need to go back to date objects or strings formatted to your user prefs/company prefs (which is a lot of the time), be sure to use the N/format module and its format.format / format.parse methods. Also be sure to always use date ob

You must be logged in to reply to this topic.