0

Remaining to a date & style

Hi. I have this:

let Countdown := 'Data d''inici' - today();
styled(text(Countdown), if Countdown = -1 then
"red"
else
if Countdown < 0 then "Orange" else "white" end
end)

I want to format the f(x) like this: if 8 days remaining, color red. If 3 days remaining, color orange. And more options. 

and how to avoid this? I just want the days and not the "1:00"

Sin título

Thanks

16 replies

null
    • Simon.1
    • 3 yrs ago
    • Reported - view

    Try this:
    let x := date(ceil(number(Date) / 86400000) * 86400000);
    let y := date(ceil(number(today()) / 86400000) * 86400000);
    let countDown := x - y;
    if countDown = -1 then
    styled(text(day(x)), "Red")
    else
    if countDown < 0 then
    styled(text(day(x)), "Orange")
    else
    styled(text(day(x)), "White")
    end
    end
    end

    There were two problems with your formula – I don't think you can run an if then statement within the styled() function (although I didn't try it!). The other problem is that ninox handles dates very in an odd way, which means there is often a bit of time (in your case, 1 hour) in the date or today() function. The formula at the start of the script deals with this.

    • Simon.1
    • 3 yrs ago
    • Reported - view

    Try this:
    let x := date(ceil(number(Date) / 86400000) * 86400000);
    let y := date(ceil(number(today()) / 86400000) * 86400000);
    let countDown := x - y;
    if countDown = -1 then
    styled(text(day(x)), "Red")
    else
    if countDown < 0 then
    styled(text(day(x)), "Orange")
    else
    styled(text(day(x)), "White")
    end
    end
    end

    There were two problems with your formula – I don't think you can run an if then statement within the styled() function (although I didn't try it!). The other problem is that ninox handles dates very in an odd way, which means there is often a bit of time (in your case, 1 hour) in the date or today() function. The formula at the start of the script deals with this.

    • Simon.1
    • 3 yrs ago
    • Reported - view

    Sorry – change x to countDown in the output!

    • Simon.1
    • 3 yrs ago
    • Reported - view

    And the day() function is an error – this should be it!

    let x := date(ceil(number(Date) / 86400000) * 86400000);
    let y := date(ceil(number(today()) / 86400000) * 86400000);
    let countDown := x - y;
    if countDown = -1 then
    styled(text(countDown), "Red")
    else
    if countDown < 0 then
    styled(text(countDown), "Orange")
    else
    styled(text(countDown), "White")
    end
    end
    end

    • FAIB
    • Mateo_Dominguez_Fuentes
    • 3 yrs ago
    • Reported - view

    Thanks for the reply! But not working. Your code sends me the same result as mine (no color and 1:00 included).

    Thanks again

    • Simon.1
    • 3 yrs ago
    • Reported - view

    Strange. Having rechecked, mine shows everything correctly (with colour) except -1. Are you using the App or Web version?
    Dates are a real pain in Ninox.....If I come up with a solution I'll let you know..

    • Simon.1
    • 3 yrs ago
    • Reported - view

    Ok –this code is giving me the output in the image:

    let x := date(ceil(number(Date) / 86400000) * 86400000);
    let y := date(ceil(number(today()) / 86400000) * 86400000);
    let countDown := x - y;
    if countDown < -(86400000 * 8) then
    styled(text(countDown), "Red")
    else
    if countDown <= 0 then
    styled(text(countDown), "Orange")
    else
    styled(text(countDown), "White")
    end
    end

    I realised that Ninox is looking at the millisecond value of a day – so (8*86400000) for 8 days etc.) Hope this works for you – you should be able to modify to give you further variations.

    • FAIB
    • Mateo_Dominguez_Fuentes
    • 3 yrs ago
    • Reported - view

    I'm using the web app (Windows 10, Chrome). And I don't know why the new code does no work too...

    Thanks for your help!

    • FAIB
    • Mateo_Dominguez_Fuentes
    • 3 yrs ago
    • Reported - view

    OMG now works. Just changed < to =...

    But the 1:00 still there

    Thanks for your help

    • Alain_Fontaine
    • 3 yrs ago
    • Reported - view

    Where does this 1:00 come from? A possible explanation: "Countdown" is computed as the difference between two timestamps. The timestamp "today()" is represented internally as the number of milliseconds since 00:00:00 UTC on 1 January 1970. Please note the "UTC". It means that, if your time zone is not UTC, that number of milliseconds will be a multiple of 86400000, representing the number of full days, plus the offset between your timezone and UTC, at the rate of 3600000 units per hour.

    Now let's suppose you live in some region where daylight saving time is applied. If you store the value of "today()" during the winter, it will be a multiple of 86400000, plus, for example 3600000. If you compare it later with the value of "today()" during the summer, where the offset will be, in the same region, 7200000, you will get a multiple of 86400000, representing the number of days, plus or minus 3600000, which will give that pesky 01:00 when displayed.

    How to get rid of it? If you want a fully general solution, the best way would probably be to convert all the time stamps to UTC. A simpler solution is to simply round the number of days. Example:

    let Countdown := round((today() - 'Data d''inici') / 86400000);
    styled(text(Countdown) + " days", switch true do
    case Countdown >= 8:
         "Red"
    case Countdown >= 3:
         "Orange"
    default:
         "White"
    end)

    • Simon.1
    • 3 yrs ago
    • Reported - view

    Thanks Alain – that's a much better solution!

    • FAIB
    • Mateo_Dominguez_Fuentes
    • 3 yrs ago
    • Reported - view

    Alain Fontaine that was AMAZING. Exactly what I want. Thanks both of you!!!

    • FAIB
    • Mateo_Dominguez_Fuentes
    • 3 yrs ago
    • Reported - view

    I do not want to abuse, but I wanted to ask you how to add the icon to the new formula.

    My old code is:

    if countDown = 86400000 * 6 then
    styled(text(countDown), "#EC7063", "emergency")
    else

    "emergency" is tje icon.

    Thanks again!

    • Alain_Fontaine
    • 3 yrs ago
    • Reported - view

    If you mean that the colour and icon must be shown when Countdown is exactly equal to 6:

    let Countdown := round((today() - 'Data d''inici') / 86400000);
    styled(text(Countdown) + " days", switch true do
    case Countdown >= 8:
    "Red"
    case Countdown = 6:
    "#EC7063"
    case Countdown >= 3:
    "Orange"
    default:
    "White"
    end, if Countdown = 6 then "emergency" end)

    • FAIB
    • Mateo_Dominguez_Fuentes
    • 3 yrs ago
    • Reported - view

    Amazing. You save me from my boss hahaha.

    Thanks!

    • Sean
    • 3 yrs ago
    • Reported - view

    Alain, I don't think I would have ever considered setting the switch expression to true to handle ranges. I'll probably use it more now that I'm aware of this trick!

Content aside

  • 3 yrs agoLast active
  • 16Replies
  • 1426Views