0

Get Previous Month

Hi,

Hopefully a simple one. Im editing the body of when the email field is clicked.

Whats the syntax to get the previous month. I have tried everything with no luck.

Thanks

10 replies

null
    • Fred
    • 2 yrs ago
    • Reported - view

    I'm not sure what you want done.

    A simple formula to get the previous month as a month name could look something like in a formula field:

    let x := month(DateField) - 1;
    if x = 0 then "December" else monthName(x) end
    

    I don't know if you are basing it off a date field or just using the current month.

      • John_Halls
      • 2 yrs ago
      • Reported - view

      Fred Ah yes, he might not be looking for the date...

    • John_Halls
    • 2 yrs ago
    • Reported - view

    To calculate a previous month you also need to allow for the fact that this might move the date to the December of the previous year. Use something like

    let t := today();
    let d := day(t);
    let m := month(t);
    let y := year(t);
    if m = 1 then
        m := 12;
        y := y - 1
    else
        m := m - 1
    end;
    date(y,m,d)
    

    Use whatever field you need to in the first line.

    Regards John

      • Fred
      • 2 yrs ago
      • Reported - view

      think this is the first time we posted at the same time.

      • John_Halls
      • 2 yrs ago
      • Reported - view

      Fred Yup, bound to happen at some stage. Happy Ninoxing Fred!

    • John_Halls
    • 2 yrs ago
    • Reported - view

    A mathematical alternative...

    let t := today();
    let d := day(t);
    let m := (12 + month(t) - 1) % 12;
    let y := year(t) - floor(m / 12);
    date(y,m,d)
    
    • Paul_Chappell
    • 2 yrs ago
    • Reported - view

    Why not just use:

    date(year(DateField), month(DateField)  -1, day(DateField))

      • John_Halls
      • 2 yrs ago
      • Reported - view

      Paul Chappell Hi Paul. How did you find that one out? Counter-intuitive to set a date with a 0 or 13 as the month, yet so useful.

      • Paul_Chappell
      • 2 yrs ago
      • Reported - view

      John Halls Yes. In other programming languages you might have to do the calculations manually.  But Ninox is clever enough to know that date(2022,0,1) is 01/12/2021 and date(2022,-1,1) is 01/11/2021, and also date(2022,13,1) is 01/01/2023.

      • Fred
      • 2 yrs ago
      • Reported - view

      John Halls I found out that Ninox does something similar when I created a solution to this post. You can just add to months and Ninox will increment the year as you pass 12, 24, 36, etc. months.

Content aside

  • 2 yrs agoLast active
  • 10Replies
  • 197Views
  • 4 Following